【问题标题】:How to send signal from a children to another children in C?如何在C中将信号从一个孩子发送到另一个孩子?
【发布时间】:2018-05-22 14:00:12
【问题描述】:

如何将乘客-儿童的信号发送给出租车-儿童?
我有以下 C 代码:

 passengerPid = fork()
 if(passengerPid) { //passenger-parents 

                taxiPid = fork();   
                if(taxiPid) { //taxi - parents      
                } else { //taxi - children  
                } 
        }       
        else { //passenger - children  
        }

【问题讨论】:

    标签: c signals fork pid


    【解决方案1】:

    我怎样才能将乘客-儿童的信号发送给出租车 - 儿童? 是的可以,但这里的问题是你如何在 @ 中获得 taxi-children pid 987654322@进程?一旦你得到了那个出租车孩子的pid,为了从另一个进程向一个进程发送信号,你可以使用kill()函数。使用任何IPC 机制将一个进程的pid(任何int 数据)发送到另一个进程。

    这是我的解决方案,在此之前通过 wait()waitpid() 的手册页:

    /**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); 
        }
    }
    

    上述代码的工作原理在 cmets 中进行了说明。在两个过程中使用macro(#if 0) 部分是为了将taxi-child process 中的pid 发送到passenger-child process启用用于观察输出的

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2013-09-15
      • 2022-12-29
      • 1970-01-01
      • 2018-09-01
      • 2018-02-07
      相关资源
      最近更新 更多