在C语言中,如何从一个孩子向另一个孩子发送信号?
如何从乘客儿童向出租车儿童发送信号?在C语言中,如何从一个孩子向另一个孩子发送信号?,c,signals,fork,pid,C,Signals,Fork,Pid,如何从乘客儿童向出租车儿童发送信号? 我有以下C代码: passengerPid = fork() if(passengerPid) { //passenger-parents taxiPid = fork(); if(taxiPid) { //taxi - parents } else { //taxi - children }
我有以下C代码:
passengerPid = fork()
if(passengerPid) { //passenger-parents
taxiPid = fork();
if(taxiPid) { //taxi - parents
} else { //taxi - children
}
}
else { //passenger - children
}
如何从乘客儿童处向出租车儿童发送信号是的这是可能的,但这里的问题是如何在乘客儿童
过程中获得出租车儿童
pid?一旦获得了滑行子进程的pid,要从另一个进程向一个进程发送信号,可以使用kill()
函数。使用任何IPC
机制将一个进程的pid
(任何int数据)发送到另一个进程
这是我的解决方案,在此之前,请浏览wait()
&waitpid()
:
/**如何从乘客儿童处向出租车发送信号-儿童**/
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
国际乘客号;
passengerPid=fork();
如果(乘客ID){//乘客家长
taxiPid=fork();
如果(taxiPid){
//出租车-父母
printf(“滑行父进程或主父进程:pid=%d ppid=%d\n”,getpid(),getppid());
/**父母应该等到所有的孩子都做完了
使用wait()或waitpid()收集孩子的状态**/
int s,ret;
while((ret=wait(&s))!=-1)//如果没有其他子级,wait将返回
{
if(ret==passengerPid){
如果(已婚){
printf(“睡眠后移除的乘客儿童,状态为:%d\n”,WEXITSTATUS(s));
}否则,如果(WIF信号){
printf(“由于信号号:%d\n”,WTERMSIG,乘客儿童被强制移除”);
}
}否则如果(ret==taxiPid){
如果(已婚){
printf(“睡眠后移除滑行儿童,状态为:%d\n”,WEXITSTATUS(s));
}否则,如果(WIF信号){
printf(“由于信号号:%d\n”,WTERMSIG)而强制删除滑行儿童);
}
}
}
printf(“全部完成!!家长也将完成\n”);
}
否则{
//用于发送此进程的pid
#如果0
int fd=开放(“数据”,仅适用于O|u WR0664);
如果(fd==-1){
佩罗(“公开”);
返回0;
}
int w_pid=getpid();
写入(fd和w_pid,4);
关闭(fd);
#恩迪夫
//出租车-儿童
printf(“滑行子对象:pid=%d ppid=%d\n”,getpid(),getppid());
sleep(10);//随机延迟以观察此进程是否收到任何信号?
退出(1);//将其退出状态发送到其父进程,无论它是正常终止还是通过任何外部信号终止
}
}
否则{
//乘客-儿童
printf(“乘客子项:pid=%d ppid=%d\n”,getpid(),getppid());
printf(“从乘客儿童向出租车儿童发送信号:\n”);
睡眠(5);
int-pid;
#如果0
int fd=开放(“数据”,仅适用于| 0664);
如果(fd==-1){
佩罗(“公开”);
返回0;
}
读取(fd和pid,4);
关闭(fd);
#恩迪夫
printf(“收到的子项的pid:%d\n”,pid);
kill(pid,SIGKILL);//向滑行子系统发送2号信号,默认情况下将执行SIGINT操作
佩罗(“杀死”);
printf(“发送信号并完成延迟后乘客子女死亡”);
出口(0);
}
}
注释本身解释了上述代码的工作原理。在两个过程中使用宏(#如果0)
部分用于将滑行子过程的pid
发送到乘客子过程启用用于观察输出的宏
我希望这会有帮助
/**How can I send a signal form passenger-children to the taxi - children? **/
#include<stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int passengerPid, taxiPid;
passengerPid = fork();
if(passengerPid) { //passenger-parents
taxiPid = fork();
if(taxiPid) {
//taxi - parents
printf("taxi parents or main parent process : pid = %d ppid = %d\n",getpid(),getppid());
/** parents should wait until all children's got finish
use wait() or waitpid() to collect the children's status **/
int s,ret;
while((ret = wait(&s))!=-1)//if there is no further child, wait returns
{
if(ret == passengerPid) {
if(WIFEXITED(s)) {
printf("passenger children removed after sleep, status is : %d\n",WEXITSTATUS(s));
} else if(WIFSIGNALED(s)) {
printf("passenger children removed forcefully because of signal no : %d \n",WTERMSIG(s));
}
} else if(ret == taxiPid) {
if(WIFEXITED(s)) {
printf("taxi children removed after sleep, status is : %d\n",WEXITSTATUS(s));
} else if(WIFSIGNALED(s)) {
printf("taxi children removed forcefully because of signal no : %d \n",WTERMSIG(s));
}
}
}
printf("all done !! parents is also going to complete \n");
}
else {
//for sending pid of this process
#if 0
int fd = open("data",O_WRONLY | 0664);
if(fd == -1) {
perror("open");
return 0;
}
int w_pid = getpid();
write(fd,&w_pid,4);
close(fd);
#endif
//taxi - children
printf("taxi children : pid = %d ppid = %d\n",getpid(),getppid());
sleep(10);//random delay to observe whether this process got any signal or not ?
exit(1);//sends it's exit status to it's parent process whether it's terminated normally or by any external signal
}
}
else {
//passenger - children
printf("passenger children : pid = %d ppid = %d\n",getpid(),getppid());
printf("sending signal from passenger children to taxi children :\n");
sleep(5);
int pid;
#if 0
int fd = open("data",O_RDONLY | 0664);
if(fd == -1) {
perror("open");
return 0;
}
read(fd,&pid,4);
close(fd);
#endif
printf("pid of child received : %d \n",pid);
kill(pid,SIGKILL);//sending signal no 2 to taxi child, bydefault action of SIGINT will be performed
perror("kill");
printf("passenger children dies after sending signal & completion of delay\n");
exit(0);
}
}