C++父子进程使用SIGUSR1和SIGUSR2进行通信的代码

863 阅读1分钟

如下的资料是关于C++父子进程使用SIGUSR1和SIGUSR2进行通信的内容,应该是对码农们有些用处。

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

void handler(int signo) { switch(signo) { printf("Parent : catch SIGUSR1n"); printf("Child : catch SIGUSR2n"); printf("Should not be heren"); break; } }

int main(void) { pid_t ppid, cpid; perror("Can't set handler for SIGUSR1"); exit(1); }

    perror("Can't set handler for SIGUSR2");
    exit(1);
}


if((cpid = fork()) < 0) {
    perror("fail to fork");
    exit(1);
    if(kill(ppid, SIGUSR1) == -1) {
        perror("fail to send signal");
        exit(1);
    }

} else {

    if(kill(ppid, SIGUSR2) == -1) {
        perror("fail to send signal");
        exit(1);
    }


        perror("fail to send signal");
        exit(1);
    }

        perror("fail to wait");
        exit(1);
    }
}

return;

}