Answers for "pipe system call explained"

C
0

pipe system call

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

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 pipen");
        return 1;
    }

    pId = fork();

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

    return 0;
}
Posted by: Guest on May-13-2021

Code answers related to "C"

Browse Popular Code Answers by Language