Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to schedule two tasks?

I am novice in process/task management. I would like to schedule two tasks. suppose,

fun1()  
{  
    printf("It will be printed in every 1 min \n");  
}  
fun2()  
{  
    printf("It will be printed in every 2 min \n");  
}  
main()  
{  
    fun1();  
    fun2();  
}

So how to schedule them, so that I will get my desired output.

I want it to run in Code::Blocks (Windows). I want fun1 to run 1 min and fun2 to run every 2 mins. If I can do it in two separate process also then tell me how can I do it. Do I need to use semaphore, mutex and all?

like image 538
Rasmi Ranjan Nayak Avatar asked Jan 11 '12 07:01

Rasmi Ranjan Nayak


People also ask

How do I link two tasks in MS Project?

Choose View > Gantt Chart. Hold down Ctrl and select the two tasks you want to link (in the Task Name column). Choose Task > Link Tasks.


1 Answers

Edit: This is getting upvoted, so I'd like to add a clarification for posterity. This isn't a good way to solve this problem - you would never want to do this by hand. Co-operative user threads are nice, and can be used to implement clever things like coroutines, but if you want to do that you should use a library like libcoroutine that handles the hairy bits for you. However, while this isn't a practical solution, it still presents an interesting idea and is an interesting example of scheduling and the limitations of pure C99.

This is a bad answer. However, it is platform-independent, and moreover, only uses functions that are defined in the C99 standard.

On the other hand, it hogs the CPU (there are no sleep functions in C99, so we have to busy-wait), uses what I can only call magic to reserve space on the stack, and completely abuses setjmp. It even uses global variables! And yet, it works.

The technique is named co-operative user threads, also called fibers. I implemented it, as I mentioned, using setjmp and longjmp. The context_switch does simple Round Robin scheduling.

This is the code:

#include <stdio.h>
#include <setjmp.h>
#include <time.h>

static jmp_buf jmp[2];
static int cur;

void context_switch()
{
    /* sleep(1) */ /* C99 doesn't have any sleeping functions */
    if (!setjmp(jmp[cur])) {
        if ((sizeof(jmp)/sizeof(*jmp)) == ++cur)
            cur = 0;
        longjmp(jmp[cur], 1);
    }
}

void fun2()
{
    char cushion[1000]; /* reserve some stack space */
    time_t old_time, new_time;
    cushion[0] = '@'; /* don't optimize my cushion away */
    old_time = time(NULL);
        cur = 1; /* the first thread to context switch is this one */
    setjmp(jmp[1]);
    while (1) {
        context_switch();
        new_time = time(NULL);
        if ((new_time - old_time) > (2 * 60)) {
            old_time = new_time;
            printf("Printed every 2 minutes\n");
        }
    }
}

void fun1()
{
    char cushion[1000]; /* reserve some stack space */
    time_t old_time, new_time;
    cushion[0] = '@'; /* don't optimize my cushion away */
    if (!setjmp(jmp[0]))
        fun2();
    old_time = time(NULL);
    while (1) {
        context_switch();
        new_time = time(NULL);
        if ((new_time - old_time) > (1 * 60)) {
            old_time = new_time;
            printf("Printed every 1 minute\n");
        }
    }
}

int main(int argc, char **argv)
{
    fun1();
    return 0;
}

And this is the output I get:

$ gcc -ggdb -std=c99 -o silly silly_setjmp.c 
$ ./silly
Printed every 1 minute
Printed every 2 minutes
Printed every 1 minute
Printed every 1 minute
...
like image 109
cha0site Avatar answered Oct 10 '22 06:10

cha0site