Click here to Skip to main content
15,893,161 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionunknown letters printing Pin
Game-point4-May-10 18:06
Game-point4-May-10 18:06 
AnswerRe: unknown letters printing Pin
David Crow4-May-10 18:09
David Crow4-May-10 18:09 
QuestionBoost Filesystem copy_file() Pin
RobNO4-May-10 17:50
professionalRobNO4-May-10 17:50 
AnswerRe: Boost Filesystem copy_file() Pin
RobNO4-May-10 18:32
professionalRobNO4-May-10 18:32 
GeneralRe: Boost Filesystem copy_file() Pin
RobNO4-May-10 18:34
professionalRobNO4-May-10 18:34 
GeneralRe: Boost Filesystem copy_file() Pin
Richard MacCutchan4-May-10 21:32
mveRichard MacCutchan4-May-10 21:32 
QuestionFull text search using Patricia tree with meta-data on a song database Pin
C++AsASecondLanguage4-May-10 12:06
C++AsASecondLanguage4-May-10 12:06 
QuestionTwo way communication between parent and child processes... Pin
DamienCurr4-May-10 9:10
DamienCurr4-May-10 9:10 
Good afternoon!

Taking the following into account:

/**************************************************************
* p2cComm.c
*   
* C program that implements communication between two processes  
* Program creates 2 unnamed pipes, p and q. Then it creates a 
* child process. Next, it links the parent with the child using
* the two pipes. Pipe p: Parent to Child, Pipe q: Child to Parent.
* Child receives commands from parent through Pipe p and sends 
* responses through Pipe q.
***************************************************************/  

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
 
int main(int argc, char *argv[]){ 
 
        int pid; 
        int p[2];   /* pipe "p" */ 
        int q[2];   /* pipe "q" */ 
        int a; 
        int b; 
 
        /* Create Pipe. Pipe P is used to transfer information  
        from the parent process to the child process */ 
        a = pipe(p);
        if(a == -1) 
        { 
        fprintf(stderr, "Pipe Failed.\n"); 
        return EXIT_FAILURE; 
        } 
         
        /* Create second pipe. Pipe Q is used to transfer information 
        from the child process to the parent process. */ 
        b = pipe(q); 
        if(b == -1) 
        { 
         fprintf(stderr, "Pipe Failed.\n"); 
         return EXIT_FAILURE; 
        } 
         
        /* Create child process */ 
        pid = fork(); 
 
        switch(pid){ 
         
                case -1: /* fork failed */ 
                        perror("main: fork"); 
                        exit(1); 
                
		/* Child process will execute a loop, waiting for command
		from the parent process. Child executes the command. Child
		returns a response to the parent */
		case 0: /* Child process */ 
                printf("Child process ID: %d\n", pid);
                break;
                /* do some things */ 
 
                /* Parent process will execute a loop, asking user for a one
		line command. Parent sends command to child for execution.
		Parent waits for the response of the child. Parent finally
		reports the result (displayed on screen). */
		default: /* Parent process */ 
                printf("Parent process ID: %d\n", pid);
                break;
                /* do some things */ 
         } 
        getchar();
        return 0; 
}


The child program will execute a list of commands that it receives from the parent (via pipe p). I have a menu of commands that will run in the parent process and send the user's choice to the child for execution. The child will send the results back to the parent when done (through pipe q), so the parent process may display the results.

In the code sample I submitted, I've created both pipes, p and q. Then, I forked the child process. My question is how will the child know to recieve it's commnads through pipe p and how will the parent know to receive the results through pipe q? Should the creation of the second pipe occur later in the code than where it is located?

As always, I greatly appreciate the guidence that I have recieved while visiting forums such as this one. Any and all suggestions are welcome!
AnswerRe: Two way communication between parent and child processes... Pin
«_Superman_»4-May-10 10:41
professional«_Superman_»4-May-10 10:41 
GeneralRe: Two way communication between parent and child processes... Pin
DamienCurr4-May-10 11:12
DamienCurr4-May-10 11:12 
Generalcode in C .... Pin
slamdunk12345674-May-10 8:57
slamdunk12345674-May-10 8:57 
GeneralRe: code in C .... Pin
Chris Losinger4-May-10 9:08
professionalChris Losinger4-May-10 9:08 
GeneralRe: code in C .... PinPopular
CPallini4-May-10 9:09
mveCPallini4-May-10 9:09 
GeneralRe: code in C .... Pin
Rajesh R Subramanian4-May-10 9:37
professionalRajesh R Subramanian4-May-10 9:37 
GeneralRe: code in C .... Pin
CPallini4-May-10 11:56
mveCPallini4-May-10 11:56 
GeneralRe: code in C .... Pin
Maximilien4-May-10 9:41
Maximilien4-May-10 9:41 
JokeRe: code in C .... PinPopular
Richard MacCutchan4-May-10 10:33
mveRichard MacCutchan4-May-10 10:33 
JokeRe: code in C .... Pin
«_Superman_»4-May-10 10:43
professional«_Superman_»4-May-10 10:43 
GeneralRe: code in C .... Pin
LittleYellowBird4-May-10 22:25
LittleYellowBird4-May-10 22:25 
GeneralRe: code in C .... Pin
Michael Schubert5-May-10 1:21
Michael Schubert5-May-10 1:21 
QuestionDebugging Program Hangs Pin
softwaremonkey4-May-10 7:42
softwaremonkey4-May-10 7:42 
AnswerRe: Debugging Program Hangs Pin
David Crow4-May-10 8:28
David Crow4-May-10 8:28 
GeneralRe: Debugging Program Hangs Pin
softwaremonkey5-May-10 20:09
softwaremonkey5-May-10 20:09 
AnswerRe: Debugging Program Hangs Pin
«_Superman_»4-May-10 8:38
professional«_Superman_»4-May-10 8:38 
GeneralRe: Debugging Program Hangs Pin
softwaremonkey5-May-10 20:06
softwaremonkey5-May-10 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.