Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want parent and child processes to communicate in C linux using pipes. I have created two file descriptors. one for parent to child i.e. readpipe and other writepipe for viceversa.
But I am getting null as output for ch and ch1 strings in my code below.
Please help me in this.

C++
#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("\nREAD = %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("\nACK RECEIVED %s",ch1);
            close(writepipe[1]);
    }
    return 0;
}

Thanks...:)
Posted
Updated 29-Apr-22 21:21pm
v2
Comments
Babita22 19-Mar-14 5:02am    
thanks @Richard for making it look clear. How to do it btw?
Richard MacCutchan 19-Mar-14 16:34pm    
Click the Improve question link, and you can edit your question. You will then see the <pre> tags that I added around your code. Why not look at all the menu items above the edit window to see what they are there for?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<wait.h>
int main(void)
{
        pid_t pid;
        int r;
        char buf[1024];
        char cp[50];
        char ans;
        int readpipe[2];
        int writepipe[2];
        long int a;
        int b;
        a=pipe(readpipe);
        b=pipe(writepipe);

        if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
        if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }
        printf("\nSIZE OF CP IS %d",sizeof(cp));
        printf("\nSEND SOMETHING TO CHILD PROCESS\t");
        scanf("%[^\n]%*c",&ans);
        fflush(stdin);
        pid=fork();
        if(pid==-1)
        {
                printf("pid:main");
                exit(1);
        }
        while(ans=='y' || ans=='Y')
        {
                if(pid==0)
                { 
                //CHILD PROCESS
                         if(ans=='n')
                        {
                                //a=getpid();
                                //printf("\nPARENT PROCESS a=%ld",a);
                                kill(pid,SIGKILL);
                        }

                        close(readpipe[1]);
                        close(writepipe[0]);
                        if(read(readpipe[0],buf,sizeof(buf)) < 0)
                        {
                            break;
                        }
                        printf("\nChild Process Read: %s\n",buf);
                        printf("\n(child)Enter data:\t");
                        fflush(stdin);
                        fgets(cp, 50, stdin);
                        printf("\nData Written to Parent%s",cp);
                        if(/*!strncmp("Q",cp,1) || */write(writepipe[1],cp,strlen(cp)+1) < 0)
                        {
                            break;
                        }
                        //printf("\nSEND SOMETHING TO PARENT PROCESS\t");
                        //fflush(stdin);
                        //scanf(" %[^\n]%*c",&ans);

                }
                else
                {
                        close(readpipe[0]);
                        close(writepipe[1]);
                        printf("\n(Parent)Enter data\t");
                        fgets(cp, 50, stdin);
                        printf("\nData Writtent to Child: %s",cp);
                        if(/*!strncmp("Q",cp,1) ||*/ write(readpipe[1],cp,strlen(cp)+1) < 0)
                        {
                            break;
                        }        

                        if(read(writepipe[0],buf,sizeof(buf)) < 0)
                        {
                            break;
                        }
                        printf("\nParent Process Read: %s\n",buf);
                        printf("\nSEND SOMETHING TO CHILD PROCESS\t");
                        fflush(stdin);
                        scanf(" %[^\n]%*c",&ans);
                        if(ans=='n')
                        {
                                kill(pid,SIGKILL);
                        }
                }
//              printf("\nSEND SOMETHING TO CHILD PROCESS\t");
//              scanf("%[^\n]%*c",&ans);

        }
        close(readpipe[1]);
        close(writepipe[0]);
        close(readpipe[0]);
        close(writepipe[1]);
        return 0;
}
 
Share this answer
 
v2
I see two problems (there may be more) ...


1. In the CHILD PROCESS half of the code, you close writepipe[1] and then write to it.

2. In the PARENT PROCESS half of the code, you close BOTH writepipe handles but later close writepipe[1] again.
 
Share this answer
 
Write a C Program which will create a pipe. Write your name on the pipe from write end. Then read data from pipe from the read end. Then display data on terminal.
 
Share this answer
 
#include <stdio.h>
#include <unistd.h>
#include <sys types.h="">
#include <stdlib.h>
#include <memory.h>

int main() {
//for parent to child
int parentToChild[2];

//for child to parent
int childToParent[2];

//string data
char message1[] = "massage from child to parent\n";
char message2[] = "message from parent to child\n";
char readBuffer[120];
pipe(parentToChild);
pipe(childToParent);
int processId , bytes;

if((processId = fork())== -1){
printf("error while creating a child process");
}

if(processId == 0){
//child process here
//send message from child to parent
close(childToParent[0]);
write(childToParent[1] , message1 , strlen(message1)+1);

//recive message from parent
close(parentToChild[1]);
bytes = read(parentToChild[0] , readBuffer , sizeof(readBuffer));
printf("%s",readBuffer);

// exit(1);
}

if(processId !=0 ){
//parent process here
//send message from parent to child
close(parentToChild[0]);
write(parentToChild[1] , message2 , strlen(message2)+1);

//recieve message from child
close(childToParent[1]);
bytes = read(childToParent[0] , readBuffer , sizeof(readBuffer));
printf("%s", readBuffer);
// exit(1);
}

return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 25-May-18 7:37am    
Already answered FOUR years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900