0

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 ;)

oemer_1907
  • 21
  • 3
  • Why do you think you need pthreads for this? Combining pthreads with forking is a bad idea in general, and although it doesn't look like your program would actually trigger any of the bad behavior associated with that combination, it also doesn't look like your program is gaining anything useful from it. – John Bollinger May 17 '22 at 20:12
  • Also, why do you think you need `setsid()`? It's not necessarily wrong to use that, but it produces a completely different sense of "independent" than I would ordinarily interpret the question's title to be describing. – John Bollinger May 17 '22 at 20:18

0 Answers0