Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

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

    pId = fork();

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

    return 0;
}
Source by raw.githubusercontent.com #
 
PREVIOUS NEXT
Tagged: #pipe #system #call
ADD COMMENT
Topic
Name
7+7 =