Tasks , Processes and Threads

Start with the following functions:

void task1(void *p)
{
    while(1)
    {
        puts("task1");
        sleep(10);
    }
}

void task2(void *p)
{
    while(1)
    {
        puts("task2");
        sleep(10);
    }
}

void task3(void)
{
    while(1)
    {
        puts("task3");
        sleep(10);
    }
}

void task4(void)
{
    while(1)
    {
        puts("task4");
        sleep(10);
    }
}

void task5(void)
{
    int c=0;
    while(1)
    {
        c++;
        if(c==5)
            exit(12);
        puts("task5");
        sleep(3);
    }
}

Write 2 different programs:

  1. Create the above tasks as threads
  2. Create the above tasks as processes

Test your code

On part 2 , make the main process watch the children and recreate a child while it exited

reference:

  • To create thread, use pthread_create
  • To create process use fork
  • Wait for a process to exit with the function wait
  • To compile without threads use:    # gcc -o myapp mytest.c
  • To compile with threads use:         # gcc -pthread -o myapp mytest.c