简体   繁体   English

子进程和父进程之间的pipe()

[英]pipe() between child and parent process

I write this code for pipe() between child and parent process. 我在child和parent进程之间为pipe()编写了这段代码。 I need to ensure whether this is a correct code or not. 我需要确保这是否是正确的代码。 By the way it's gives the answer what is suppose to see ! 顺便说一句,它给出了想要看到的答案!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>



int main(int argc, char *argv[]){
    pid_t pid;
    int fd[2];
    char buf[20];

pipe(fd);


switch(pid = fork()){

    case -1:
        perror("pipe");
        exit(1);

    case 0: 
        /*child process*/
        close(fd[1]);
        read(fd[0], buf, 20);
        printf("Child read message from parent: %s\n", buf);
        exit(1);
        break;

    default:
     /*parent process*/
        close(fd[0]);
        write(fd[1], "Hello from parent\n", 17);
        break;
    } 
    return 0;

}

The switch statement is mostly ok, although you don't need the pid variable since you don't do anything with it. switch语句基本上没问题,虽然你不需要pid变量,因为你没有对它做任何事情。

The parent code is also mostly ok, but the string is actually 18 bytes without the NUL terminator, and 19 bytes with the NUL terminator. 父代码也很好,但是字符串实际上是没有NUL终结符的18个字节,而是带有NUL终结符的19个字节。 It's good practice to handle the newline and NUL terminator in the child, so I would stick with 17 and remove the newline from the string. 在孩子中处理换行符和NUL终止符是一个好习惯,所以我会坚持使用17并从字符串中删除换行符。

The child code is wrong. 子代码错了。 You need a variable to store the return value from read . 您需要一个变量来存储read的返回值。 You need to check the return value to make sure the read succeeded. 您需要检查返回值以确保read成功。 And you need to add a NUL terminator to the string. 并且您需要在字符串中添加NUL终止符。 In the C programming language, a "string" is an array of characters that ends with a zero byte (called the NUL terminator, and written as '\\0' ). 在C编程语言中, “字符串”是以零字节结尾的字符数组(称为NUL终止符,写为'\\0' )。 It's your job to make sure that the buffers you use are always big enough to hold the NUL terminator, and that every string has a NUL terminator. 你的工作是确保你使用的缓冲区总是足以容纳NUL终结符,并且每个字符串都有一个NUL终结符。

So the fixed code looks like this: 所以固定代码如下所示:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main( void )
{
    int fd[2];
    char buffer[20];

    if ( pipe(fd) < 0 ) {
        perror( "pipe" );
        exit( 1 );
    }

    switch( fork() ) {
        case -1:
            perror( "fork" );
            exit( 1 );

        case 0:
            /*child process*/
            close(fd[1]);
            ssize_t count = read( fd[0], buffer, sizeof(buffer)-1 );
            if ( count <= 0 ) {
                perror( "read" );
                exit( 1 );
            }
            buffer[count] = '\0';
            printf( "Child read message from parent: %s\n", buffer );
            exit(1);

        default:
            /*parent process*/
            close(fd[0]);
            char *message = "Hello from parent";
            size_t length = strlen( message );
            write( fd[1], message, length );
            break;
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM