C-线程和进程-防止僵尸

标签 c multithreading process fork

我正在实现一个接收解析为数组的命令行的函数("./waiter 20 &" 将被解析,并且该函数将接收数组 例如 {"./waiter","20","&"})。 如果最后一个参数是 &,则该进程应在​​后台运行。 为了防止僵尸,我需要使用一个新线程来等待子进程。 所附代码是我的工作程序,我添加等待子进程的新线程的所有努力都失败了。 有人可以指导我吗? 附上代码,以及我不成功尝试的一些剩余内容。 (该函数为process_arglist)

更新:经过多次尝试使用此处的建议后,它仍然失败,我不确定为什么。附上更新的代码。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

void func(void* ptr) {
    pid_t* mypid = (pid_t*)ptr;
    waitpid(*mypid);
    pthread_detach(pthread_self());
}

int process_arglist(int count, char** arglist){
    int isBackground = 0;
    pid_t  pid;
    int    status;
    char** parsedList;
    if (strcmp(arglist[count-1],"&") == 0) {
        printf("###we are in the & situation\n");
        parsedList = (char**)malloc((count-1)*sizeof(char*));
        if (parsedList == NULL) {
            printf( "Error: malloc failed - %s\n", strerror(errno));
        }
        int i;
        for (i=0;i<count-1;i++){
            parsedList[i] = arglist[i];
        }
        /*printf("parsed list:\n");
        for (i=0;i<count-1;i++) {
            printf(" %d: %s\n", i,parsedList[i]);
        }*/
        if ((pid = fork()) < 0) {     /* fork a child process           */
            printf( "Error: fork failed");
            exit(0);
        } else if (pid == 0) {          /* for the child process:         */
            if (execvp(*parsedList,parsedList) < 0) {     /* execute the command  */
                printf( "Error: execvp failed - %s\n", strerror(errno));
                exit(0);
            }
        } else {
            pthread_t thread;
            pthread_create(&thread, NULL, (void*) &func, (void*) &pid);
        }
    } else {
        if ((pid = fork()) < 0) {     /* fork a child process           */
            printf( "Error: forking child process failed - %s\n", strerror(errno));
            exit(0);
        }
        else if (pid == 0) {          /* for the child process:         */
            if (execvp(*arglist,arglist) < 0) {     /* execute the command  */
                printf( "Error: execvp failed - %s\n", strerror(errno));
                exit(0);
            }
        }
        else {                                  /* for the parent:      */
            while (waitpid(&status) != pid);       /* wait for completion  */
        }
    }
}

最佳答案

首先,从调用 wait 切换到调用 waitpid。否则,如果有多个线程在等待,它们就会窃取彼此的通知。

其次,将对 waitpid 的调用分解为它自己的函数,该函数将要等待的 PID 作为参数。通过 void * 进行转换,因为这就是线程参数所使用的。

第三,将对函数的调用更改为对 pthread_create 的调用,将要等待的 PID 转换为 void * 以传递给新创建的线程。

最后,让线程自行分离,因为不会有任何东西等待线程终止。

关于C-线程和进程-防止僵尸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34467014/

相关文章:

c# - 简单的多线程问题

C++ GUI 应用程序 : Starting a child process in WndProc ( no MFC )

Android ICS FFMPEG 比例视频不起作用

c - 在 Mac 上哪里可以找到 C 源代码?

c - 递归不听 Return 而是 Exit(0)

c - 为什么使用函数指针会导致我的以下程序崩溃?

c# - 检测线程数组的完成

Java ScheduledFuture 获取列表

java - 使用控制组管理 Java 进程

c - 线程 : value stored in heap 问题