Thread: messaging between a parent and child proccess

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    26

    messaging between a parent and child proccess

    Code:
    #include <iostream>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    #define BSIZE   100
    
    void parent(int Msgs);
    
    
    int main()
    {
            parent(100);
            return 0;
    }
    
    
    void parent(int Msgs)
    {
            int numMsgs = Msgs;
            int pid;
            int x=0;
    
            int pipe1[2];
            int pipe2[2];
            char buf[BSIZE];
            ssize_t  nbytes;
    
            pid = fork();
            if(pid < 0)
                    cout<<"ERROR"<<endl;
    
            else if (pid == 0)
            {
                    close(pipe1[1]);
                    read(pipe1[0], buf, BSIZE);
                    cout<<endl;
                    cout<<"child read"<<endl;
                    close(pipe2[2]);
            }
    
            else
            {
                    close(pipe1[0]);
                    nbytes = write(pipe1[1], "p", 1);
                    cout<<endl;
                    cout<<"parent write"<<endl;
                    close(pipe1[1]);
                    cout<<read(pipe2[0], buf, BSIZE);
                    cout<<endl;
                    cout<<"parent read"<<endl;
                    close(pipe2[0]);
                    //parent code goes here
         }
    
    }

    //the program has to send messages, the problem i am having is that the message doest not get passed from the parent to the child and back and forth. http://www.cs.stevens-tech.edu/~quyn...1/assign1.html is the link for the exact procedures.
    Last edited by v3dant; 09-04-2004 at 11:53 AM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    you have to set up your pipes.

    int err = pipe(pipe1);

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    thansk that worked

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    Code:
    #include <iostream>
    #include <cstring>
    #include <ctime>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    #define BSIZE	512
    
    void parent(int Msgs);
    
    
    int main()
    {
    	parent(100);	
    	return 0;
    }
    
    
    void parent(int Msgs)
    {
    	int numMsgs = Msgs;
    	int message = 1;
    	char line [512];
    	int lineNum=0;
    	int pid;
    	int ranLines;
    	int pipe1[2];
    	int pipe2[2];
    	int err1 = pipe(pipe1);
    	int err2 = pipe(pipe2);
    	
    	
    	if(err1 == -1 || err2 == -1)
    		cout<<"error with pipes"<<endl;
    
    	char buf[512];
    	ssize_t  nbytes;
    
    	pid = fork();
    
    	if(pid < 0)
    		cout<<"ERROR"<<endl;
    
    	else if (pid == 0)
    	{
    		int d=0;
    		nbytes=read(pipe1[0], buf, BSIZE);
    		cout<<nbytes<<endl;
    		while((nbytes != -1) && (buf[0] != 'b'))
    		{
    			cout<<"parent: ";
    			cout<<buf[0]<<int(buf[1])<<buf[2]<<buf[3]<<buf[4]<<buf[5];
    			cout<<buf[6]<<buf[7]<<int(buf[8])<<endl;
    			nbytes=read(pipe1[0], buf, BSIZE);
    	
    		}
    			
    	}
    
    	else
    	{	
    		//close(pipe1[0]);
    		srand(time(0));
    		int x=0;
    		int y=0;
    		while(message<2)
    		{
    			ranLines = (1+ rand() % 5);
    			if(message==100)
    				write(pipe1[1],"bye",3);
    			else
    			{
    				for(x=1; x<ranLines+1; x++)
    				{
    					line[0]='m';
    					line[1]=char(message);
    					line[2]=' ';
    					line[3]='l';
    					line[4]='i';
    					line[5]='n';
    					line[6]='e';
    					line[7]=' ';
    					line[8]=char(lineNum+x);
    					y=write(pipe1[1],line,9);			
    				}	  
    			}	
    			message++;
    		}	
    		
    		//close(pipe1[1]);
    		//parent code goes here
    	}
    
    }

    ok now i have the pipes working however i cant make it so that the parent continuoulsy provides random number of messages and the child outputs it. the program just outputs one line and then does not read the next line sent to the child proccess.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    how abour putting the if/else if/else trio in a loop, say:

    int numMsgs = 0;
    while(numMsgs < Msgs)
    if
    else if
    else
    numMsgs++;
    end while

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    Code:
    #include <iostream>
    #include <cstring>
    #include <ctime>
    #include "string2int.h"
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    #define BSIZE	512
    
    void parent(int Msgs);
    
    
    int main()
    {
    	parent(100);	
    	return 0;
    }
    
    
    void parent(int Msgs)
    {
    	int numMsgs = Msgs;
    	int message = 1;
    	char line [512];
    	int lineNum=0;
    	string linestr;
    	string chldmsg;
    	string msgstr;
    	int pid;
    	int ranLines;
    	int pipe1[2];
    	int pipe2[2];
    	int err1 = pipe(pipe1);
    	int err2 = pipe(pipe2);
    	
    	
    	if(err1 == -1 || err2 == -1)
    		cout<<"error with pipes"<<endl;
    
    	char buf[512];
    	ssize_t  nbytes;
    
    	pid = fork();
    
    	if(pid < 0)
    		cout<<"ERROR"<<endl;
    
    	else if (pid == 0)
    	{
    		int a=0;
    		int chLines;
    		char chMsg[512];
    		string chMsgNum;
    		string lnNum;
    		nbytes=read(pipe1[0], buf, BSIZE);
    		while(nbytes!=0 && buf[0] != 'b')
    		{
    			cout<<"parent: ";
    			for(a=0; buf[a]!= '*'; a++)
    			{
    				cout<<buf[a];
    			}
    			cout<<endl;
    			nbytes=read(pipe1[0], buf, BSIZE);
    			if(nbytes==0)
    			{
    				//child should now send message to parent
    			}
    			
    		}		
    	}
    
    	else
    	{	
    		//close(pipe1[0]);
    		srand(time(0));
    		int x=0;
    		int y=1;
    		int z=0;
    		while(message<101)
    		{
    
    			ranLines = (1+ rand() % 5);
    			if(message==100)
    				write(pipe1[1],"bye",3);
    			else
    			{
    				for(x=1; x<ranLines+1; x++)
    				{
    				
    					y=0;
    					z=0;
    					line[0]='m';
    					msgstr=BasicTypeToStr(message);
    					for(y=1; z<msgstr.size(); y++)
    					{
    						line[y]=msgstr[z];
    						z++;
    					}
    					y=msgstr.size()+1;
    					line[y]=' ';
    					y++;
    					line[y]='l';
    					y++;
    					line[y]='i';
    					y++;
    					line[y]='n';
    					y++;
    					line[y]='e';
    					y++;
    					line[y]=' ';
    					y++;
    					lineNum++;
    					linestr=BasicTypeToStr(lineNum);
    					z=0;
    					for(y; z<linestr.size(); y++)
    					{
    						line[y]=linestr[z];
    						z++;
    					}
    					line[y]='*';
    					cout<<endl;     //write to external file here
    					write(pipe1[1],line,y+1);			
    				}  
    		
    			//parent proccess should now wait for a response from child
    				nbytes=read(pipe2[0],buf,BSIZE);
    				cout<<nbytes<<endl;
    			//	while(nbytes != -1)
    			//	{
    			//		cout<<"parent read"<<endl;
    			//		nbytes=read(pipe2[0],buf,BSIZE);
    
    			//	}
    			}	
    			message++;
    		}	
    		
    		//close(pipe1[1]);
    		//parent code goes here
    	}
    
    }
    i now have it so that the parent sends the #of lines of the message and the child reads it. how can i make it so that the child is able to write a message and have the parent wait for it and read the message from the child and then it writes a message back and so on? For some reason I dont think the read is returning 0 at the end of file.
    Last edited by v3dant; 09-06-2004 at 08:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 04:04 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM