每次子进程完成某个任务时如何向父进程发送 SIGUSR1 信号

时间:2021-03-28 00:22:38

标签: c process signals

    else if (pids[i] == 0)
        {
          
            printf("Child[%d](pid):%d....Parent Pid:%d\n", i + 1, getpid(), getppid());
            for (int l = 0; l < i; l++)
            {
                g = g->pnext;
            }

            while (g != NULL)
            {
                genome_cmp(g, mutation_array);//every time the child process finishes this function i want to Print that in the handler function 
                kill(pids[i], SIGUSR1);
                sleep(10);
                save_mutation_array(mutation_array, res, 0);
                free_mutations(mutation_array);

                for (int j = 0; j < nr_filhos; j++)
                {
                    g = g->pnext;
                }
            }

            exit(0);
        }
    }
    signal(SIGUSR1, handler);
    while ((wait_lastPid = wait(&status)) > 0);
    get_time_useconds(&time_usec_end);
    elapsed_time = (long)(time_usec_end - time_usec_begin);
    printf("Total time = %ld microseconds\n", elapsed_time);
    return 0;
}

所以我在使用 SIGUSR1 获取父进程和子进程之间的连接时遇到了麻烦……我想要的是每次子进程完成函数基因组_cmp 时,子进程都会向父进程发送一个 SIGUSR1,从而触发打印的处理程序 func “比较完成”……实际发生的是,即使我创建了几个子进程,它也只打印一次。

        void handler(int signum)
    {
    printf("%d",getpid());
    if (signum == SIGUSR1)
        printf("Comparassion Completed!\n");
}

0 个答案:

没有答案
相关问题