Wait()导致程序在打开FIFO命名管道时暂停

时间:2016-05-27 03:39:22

标签: c pipe mkfifo

每当我使用wait函数时,我的代码在open()上暂停,没有它,程序就不起作用了。没有错误代码,但我想知道什么是错的。

代码最终将是controller.c将打开c1,c2,c3。它将等待c1哪个将获得输入并将其放入管道然后将转到c2将打印它,然后c3将以不同的格式打印它。

int main(int ac, char**av)
{
    int fd;
    int test;
    char * myfifo = "/tmp/myfifo";
    unlink(myfifo);                     //makes sure no fifo already exists

    /* create the FIFO (named pipe) */
    if((mkfifo(myfifo, 0666)) <0)
        perror("MKFIFO: ");



    int statval;
    pid_t pid1 = fork();                    //creates a process for c1 to run in
    wait(&statval); 
    if ( pid1 == 0)                     //makes sure to only run the program in the child process
    {   
        printf("Opening c1\n");
        int x = execvp("./c1",av);          //Runs the other file based on the array
        if (x == -1)
            exit (1);
        perror("Error");                //used for error reporting
        printf("Closing c1\n");
        exit(0);                    //ends process when finished
    }
    if (WIFEXITED(statval))                 //if there is an exit code
    printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process

    pid_t pid2 = fork();                    //creates a process for c2 to run in
    wait(&statval); 
    if ( pid2 == 0)                     //makes sure to only run the program in the child process
    {   
        printf("Opening c2\n");
        int x = execvp("./c2",av);          //Runs the other file based on the array
        if (x == -1)
            exit (1);
        perror("Error");                //used for error reporting
        exit(0);                    //ends process when finished
    }
    if (WIFEXITED(statval))                 //if there is an exit code
    printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process

    unlink(myfifo);

C1:

printf("TEST1\n");
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    int fd;
        char * myfifo = "/tmp/myfifo";
        char buf[BUFSIZE];

    fd = open(myfifo, O_WRONLY);
    if (fd < 0)
        perror("OPEN: ");   

    //while (1) {
        printf("Enter a line of text\n"); 
        //scanf("%s", buf);
        if ((strcmp(line,"-999"))==0){
            printf("TEST");         
            //break;
        }
        //write(fd, buf, sizeof(buf));
            write(fd, "hi", sizeof("hi"));
    //}
    close(fd);
    unlink(myfifo);
        return 0;

C2:

int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[BUFSIZE];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, BUFSIZE);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;

程序将打开c1,打印TEST,然后在我手动退出之前不执行任何操作。

1 个答案:

答案 0 :(得分:2)

第一个分叉后,父母和孩子wait()。孩子的呼叫失败,因为它没有等待的孩子,但你忽略了结果。然后,孩子执行程序c1,(假设执行成功)依次打开FIFO进行写入。这个阻止直到FIFO的另一端被打开,但是程序不会打开另一端,因为父进程正在等待子进程在进行之前退出。你已经造成了僵局。

两个孩子都不能通过打开FIFO直到另一个(或其他程序)打开FIFO的另一端,因此父母必须在等待任何一个孩子之前分叉两个孩子。此外,对于wait()的孩子来说,这是毫无意义的,尽管他们对该功能的调用应该很快失败。