'main process --> pthread --> fork --> lets the forked process run and kill main

Process A should start process B and then terminate. Process B should continue as an independent process

please have a little mercy on me - i am trying to educate myself and i like to learn more

int parallel_thread1;
pthread_t thread1;

static void *performer(void) {
    pid_t pid;
    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)            // if success then let the parent terminate
        exit(EXIT_SUCCESS);

    if (setsid() < 0)
        exit(EXIT_FAILURE);

    pid = fork();
    if (pid < 0)
        exit(EXIT_FAILURE);

    if (pid > 0)
        exit(EXIT_SUCCESS);

    switch(pid)
    {
    case -1: // Fork() has failed
        perror ("fork");
        break;
    case 0: // This is processed by the child
        system("/home/performerprocess/bin/performer");
        exit(EXIT_FAILURE);
        break;

    default: // This is processed by the parrent
        break;
    }

    return(0);

}

int main(int argc, char *argv[])
{
 parallel_thread1 = pthread_create(&thread1, NULL, &performer, NULL);
 if(parallel_thread1 == 0)
   {
     pthread_detach(updateThread);
     usleep(500000);
    }

 ...

puts("Process should already be terminated :(   I don't want to see this msg");
return 0;
}

please suggest a "clean" solution - thank you very much ;)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source