📜  管道系统调用 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:35.045000             🧑  作者: Mango

代码示例1
#include 
#include 

int main()
{
    int pipeFds[2], status, pId;
    char writeMsg[20] = "hello world";
    char readMsg[20];

    status = pipe(pipeFds);

    if (status == -1)
    {
        printf("Unable to create pipe\n");
        return 1;
    }

    pId = fork();

    // child process
    if (pId == 0)
    {
        read(pipeFds[0], readMsg, sizeof(readMsg));
        printf("\nChild Process receives data\n%s\n", readMsg);
    }
    // parent process
    else if (pId > 0)
    {
        printf("Parent Process sends data\n%s", writeMsg);
        write(pipeFds[1], writeMsg, sizeof(writeMsg));
    }

    return 0;
}