Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

two way communication between child and parent processes in C using pipes

#include <stdio.h>
#include<stdlib.h>
#include 
#include<unistd.h>
int main()
{
    pid_t pid;
    int r;
    char *ch=NULL;
    char *ch1=NULL;
    int readpipe[2];
    int writepipe[2];
    int a;
    int b;
    a=pipe(readpipe);
    b=pipe(writepipe);
      // check a and b

    pid=fork();
    // check pid

    if(pid==0)
    { //CHILD PROCESS
            close(readpipe[1]);
            close(writepipe[1]);
            read(readpipe[0],ch,sizeof(ch));
            printf("
READ = %s",ch);
            close(readpipe[0]);
            ch1="YES";
            write(writepipe[1],ch1,sizeof(ch1));
            close(writepipe[1]);
    }
    else
    { //PARENT PROCESS
            close(writepipe[0]);
            close(writepipe[1]);
            ch="HI!! YOU THERE";
            write(readpipe[1],ch,sizeof(ch));
            close(readpipe[1]);
            read(writepipe[1],ch1,sizeof(ch1));
            printf("
ACK RECEIVED %s",ch1);
            close(writepipe[1]);
    }
    return 0;
}
Source by www.codeproject.com #
 
PREVIOUS NEXT
Tagged: #communication #child #parent #processes #C #pipes
ADD COMMENT
Topic
Name
9+8 =