C - 顶级进程和底级(叶)进程之间的命名管道

时间:2016-02-29 17:18:25

标签: c pipe fork ipc named-pipes

我试图从底层叶子流程写入命名管道,并从顶层流程上的管道读取。

要做到这一点,我首先在顶级进程中创建FIFO,然后使用for循环来分叉更多进程。在for循环中,我检查叶子进程,如果它是叶子,我写入FIFO并从循环中断开。然后,在循环之后,我试图从顶层进程中的FIFO读取。这不起作用,我的程序在创建叶子进程后卡住并停止。

如何通过FIFO将消息从一个叶子发送回顶级父进程?

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>

#define MAX_BUF 1024

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

    int numprocs = atoi(argv[1]);
    int lev = numprocs;
    fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", lev, getpid(), getppid());

    //create shared memory
    const int SIZE = numprocs * sizeof(int);
    const char *name = "dleggio1OS";
    int shm_fd;
    int *ptr;
    shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
    ftruncate(shm_fd, SIZE);
    ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
    *ptr = getpid();

    //create fifo
    int fd;
    char *myfifo = "/tmp/dleggio1fifo";
    mkfifo(myfifo, 0666);

    //spawn procs
    int i;
    for(i = 1; i < numprocs; i++){
        lev--;
        int pfds[2];
        char buf[30];
        if(pipe(pfds) == -1){
            perror("pipe");
            exit(1);
        }
        pid_t pid;


        if((pid = fork()) < 0){
            perror("fork");
            exit(1);
        }

        if(pid == 0){ //child

            const int SIZE = numprocs * sizeof(int);
            const char *name = "dleggio1OS";
            int shm_fd;
            int *ptr;
            shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
            ftruncate(shm_fd, SIZE);
            ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
            ptr[i] = getpid();

            close(pfds[1]);
            if(read(pfds[0], buf, 3) <= 0){
                perror("child");
                exit(1);
            }
            int check = atoi(buf);
            fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", check, getpid(), getppid());

            if(check == 1){ //leaf
                fd = open(myfifo, O_WRONLY);
                write(fd,"leaf",sizeof("leaf"));
                close(fd);
                break;
            }

        }
        else{ //parent
            close(pfds[0]);
            char hold[3];
            sprintf(hold,"%d",lev);
            if(write(pfds[1], hold, 3) <= 0){
                perror("parent");
                exit(1);
            }

            wait(NULL);
            return 0;
        }
    }

    //read fifo
    char buff[MAX_BUF];
    fd = open(myfifo,O_RDONLY);
    read(fd,buff,MAX_BUF);
    close(fd);

    shm_unlink(name);
    unlink(myfifo);
    return 0;
}

输出:

ALIVE: Level 3 process with pid=554, child of ppid=451.
ALIVE: Level 2 process with pid=555, child of ppid=554.
ALIVE: Level 1 process with pid=556, child of ppid=555.
_ // <---- stalls here

1 个答案:

答案 0 :(得分:0)

所有进程都挂在&#34; wait()&#34;呼叫(即等待其中一个孩子退出),除了分叉的最后一个孩子......挂在&#34;打开(myfifo,O_WRONLY)&#34; .... 最后一个孩子将继续挂起,直到一个进程打开fifo进行阅读...