Wala Ramouni
Wala Ramouni

Reputation: 177

How can I implement a C program where the parent process communicates with the child process and vice versa?

I'm using pipes to create a C program that determines if the entered integer number by user is even or odd. This is its full specification:

  1. The parent process should send the integer number to the forked child process.
  2. The child process should receive the sent integer number to determine whether it's even or odd.
  3. Then, the result should be returned back to parent process in order to display it on the shell for the user.

I have use pipes for interprocess communication, and thus exchange data between the parent and child processes.

This is what I've done so far:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

void sendNumber(int);
void RecResult();
int f_des[2];

int main(int argc, char *argv[])
{
    static char message[BUFSIZ];
    if ( pipe(f_des) == -1 )
    {
        perror("Pipe");
        exit(2);
    }
    switch ( fork() )
    {
            
        case -1: perror("Fork");
            exit(3);
            
        case 0: /* In the child */
        {
            
            // read a meesage
            close(f_des[1]);
            read(f_des[0], message, BUFSIZ  );
            
            int num=atoi(message);
            printf("Message received by child: [%d]\n", num);
            
            
            if(num % 2 == 0)
                
                sprintf(message,"%d is Even \0",num);
            
            else
                
                sprintf(message,"%d is Odd \0",num);
            
            close(f_des[0]);
            write(f_des[1], message, BUFSIZ);
            exit(0);
        }
            
            break;
        default: /* In the parent */
        {
            int num;
            printf("\n Enter an Integer \n");
            scanf("%d",&num);
            char msg[BUFSIZ];
            sprintf(msg,"%d",num);
            
            // write a message
            close(f_des[0]);
            write(f_des[1], msg, BUFSIZ);
            
            printf("\n Waiting Child \n");
            
            wait(NULL); 
            // read a mesaage
            static char message[BUFSIZ];
            close(f_des[1]);
            read(f_des[0], message, BUFSIZ);
            
            printf("========> Result:  %s . \n",message);
        }
    }
}

The child process receives the message successfully but the parent does not receive any result:

rose@ubuntu:~$ gcc -o test_3 test_3.c
rose@ubuntu:~$ ./test_3

 Enter an Integer 
3

 Waiting Child 
Message received by child: [3]
========> Result:   . 
rose@ubuntu:~$ 

Upvotes: 0

Views: 2090

Answers (1)

Lucian Bredean
Lucian Bredean

Reputation: 893

A PIPE channel is a uni-directional communication channel. If you want the child process to write back to the parent process, you will need a second PIPE channel, different than the one used for receiving the message from the parent.

Upvotes: 1

Related Questions