c - 在子进程发出完成信号后,SIGKILL 将无法停止子进程

标签 c linux process fork sigkill

我的程序创建了 n 个子进程,每个子进程 count(+5) 如果超过 100,它会向父进程发送一个信号,父进程应该杀死这个子进程。 我做了这个程序,但它不起作用,它一直在计算第一个 child ,这意味着 SIGKILL 不起作用。

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>


pid_t pids[10];
int pidval[10];
int l;

void handler1(int mysignal)
{
    int i;
    for (i=0; i<l; i++) {
        if (pidval[i]>100) {
            kill(pids[i], SIGKILL);
            printf("\n killed ");
        }
    }
}

int main(int argc, char ** argv)
{

    int i, s;
    l = atoi(argv[1]);
    pid_t pid;
    for(i=0; i<l; i++)
    {
        pid=fork();

        if(pid<0) 
            printf("\n error \n");
        if (pid==0) {
            pids[i] = getpid();
            while(1) {
                s+=5; 
                if(s>100) 
                {
                    pidval[i]=s;
                    printf("\noverflow,%d,%d,%d",s,pids[i],getpid());
                    kill(getppid(), SIGALRM);
                };
            }
        }
        if(pid>0) {
            signa(SIGALRM,handler1);
            waitpid(-1,NULL,0);
        }
    }
}

最佳答案

使用SIGTERM,看看这个article .

关于c - 在子进程发出完成信号后,SIGKILL 将无法停止子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21234067/

相关文章:

c - Socket编程-C-选择连接

c - 为什么每次调用 glMatrixMode 后都必须调用 glLoadIdentity?

c - 通过 C 中的 Netlink 从内核到用户空间的多播

c - 存储数十亿整数的数据结构

linux 不检测死的 tcp 连接

java - 如何制作 Java 跨平台 GUI 应用程序(Windows、Linux)?我应该使用什么工具?

并发进程和随机整数

c - 有没有办法在运行 Linux 或 Windows 的台式计算机上使用 C 中的 "test"关键字来实现 "volatile"?

c# - 使用 Process 类的 C# 应用程序中的 ffmpeg - 用户提示未显示在标准错误中

python - 如何使用 os.spawnv 使用 Python 发送电子邮件副本?