主要过程 - > pthread->叉 - >让分叉的过程运行并杀死Main
过程A应启动过程B然后终止。过程B应该继续作为一个独立过程
请对我有一点怜悯 - 我正在尝试教育自己,我想了解更多信息,
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;
}
请提出一个“干净”的解决方案 - 非常感谢您;)
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 ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论