Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用FreeBSD和C语言获取子进程完成后的PID_C_Process_Freebsd_Sigchld - Fatal编程技术网

使用FreeBSD和C语言获取子进程完成后的PID

使用FreeBSD和C语言获取子进程完成后的PID,c,process,freebsd,sigchld,C,Process,Freebsd,Sigchld,我试图正确地处理SIGCHLD,但我无法在处理程序中获得进程的PID,因此我可以更改结构中一个参数的值 代码如下: typedef struct { int active_state; int pid; }Worker; typedef struct { Worker worker; int used_state;//this can be USED or UNUSED }WorkersTable; WorkersTable *table; //I put t

我试图正确地处理SIGCHLD,但我无法在处理程序中获得进程的PID,因此我可以更改结构中一个参数的值

代码如下:

typedef struct
{
    int active_state;
    int pid;
}Worker;

typedef struct
{
    Worker worker;
    int used_state;//this can be USED or UNUSED
}WorkersTable;

WorkersTable *table; //I put this in a shared memory map and works correctly
这是处理程序的代码。在这个文件中有一个名为dead\u child\u pid的全局变量,我想存储要使用的dead child的pid

void handler_SIGCHLD(int signal)
{
    pid_t child_pid;
    int e;
    do
    {
        child_pid=wait3(&e,WNOHANG,NULL);
    }while(child_pid>(pid_t)0);
    mark_unused=1;
}
当调用handler_SIGCHLD并在最后将mark_unused=1时,将访问以下代码:

if(mark_unused)
    {
        /*put the current position at the table as unused*/
        int counter_find_pid=0;
        while(counter_find_pid<MAX_WORKERS&&table[contador_find_pid].used_state==USED&&table[counter_find_pid].worker.pid!=dead_child_pid)
        {
            counter_find_pid++;
        }
        table[counter_find_pid].used_state=UNUSED;
    }
if(标记未使用)
{
/*将表中的当前位置设置为未使用*/
int计数器_find_pid=0;
while(计数器查找)
有效的
wait3
返回值为

  • 孩子的性格
  • 如果有未更改状态的子级,则为0
  • -1关于错误
  • child\u pid
    为0或-1之前,您将无法退出该循环,在这种情况下,(死亡的子pid的)先前值已被覆盖。您需要找到一种方法,在仍在循环中时将死去的子项的有效pid保存在某个位置或更改循环。您可以将全局
    死去的子项pid
    放入处理程序中,但要使其不稳定

    编辑

    你想要更像这样的东西,但是如果这是一个严肃的应用程序,这也需要增强,因为当你处理死的孩子数组时,处理程序可能被调用。然后你必须考虑在操作时阻止SigCHLD。

    // somewhere, probably global scope
    volatile currentlyDeadChilren = 0;
    volatile pid_t dead_children[MAX_DEAD_CHILDREN];    
    
    void handler_SIGCHLD(int signal)
    {
        pid_t child_pid;
    
        int e;
    
        do
        {
            child_pid=wait3(&e,WNOHANG,NULL);
    
            if (child_pid > 0)
                dead_children[currentlyDeadChildren++] = child_pid;        
    
        }while(child_pid>(pid_t)0);
    
        mark_unused=1;
    }
    

    那么你的问题是什么呢?例如类似这样的东西?'pid\u child\u pid=getpid()'之前的处理程序中的'dead\u child\u pid'@Javier,不,孩子已经死了,
    getpid()
    将只获取当前运行的父级的pid。
    // somewhere, probably global scope
    volatile currentlyDeadChilren = 0;
    volatile pid_t dead_children[MAX_DEAD_CHILDREN];    
    
    void handler_SIGCHLD(int signal)
    {
        pid_t child_pid;
    
        int e;
    
        do
        {
            child_pid=wait3(&e,WNOHANG,NULL);
    
            if (child_pid > 0)
                dead_children[currentlyDeadChildren++] = child_pid;        
    
        }while(child_pid>(pid_t)0);
    
        mark_unused=1;
    }