Thread: exec functions under unix

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

    exec functions under unix

    Code:
    #include <iostream>
    #include <cstring>
    #include <ctime>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <poll.h>
    #include "string2int.h"
    using namespace std;
    
    
    void parent(int m);
    
    int main()
    {
    	parent(100);
    	return 0;
    }
    
    
    void parent(int m)
    {
    	int pipe1[2];
    	int pipe2[2];
    	pipe(pipe1);
    	pipe(pipe2);
    	char buf[80];
    	const int BSIZE = 100;
    	int pid = fork();
    
    	if(pid < 0)
    		cout<<"ERROR"<<endl;
    	else if(pid == 0)
    	{
    		int b;
    		pollfd my_fds; int poll_ret;
    		my_fds.fd = pipe1[0];
    		my_fds.events = POLLRDNORM;
    		my_fds.revents = POLLRDNORM;
    		buf[0]='a';		
    		do
    		{
    			poll_ret=poll(&my_fds, (nfds_t)1, 10);
    			if(poll_ret>0)
    			{
    				read(pipe1[0],buf,BSIZE);
    				cout<<"parent: ";
    				for(b=0; buf[b]!='#';b++)
    				{
    					if(buf[b] == '*')
    					{
    						cout<<endl;
    						cout<<"parent: ";
    					}
    					else
    						cout<<buf[b];
    				}
    				cout<<endl;
    				write(pipe2[1],"RESPONSE#",9);
    			}
    		}while(buf[0] != 'b');	
    	}
    	
    	else
    	{
    		int numMsgs=m;  const int BSIZE2 =100;
    		int msgCount=1; int n;
    		string pLinStr;
    		string pMsgStr;
    		int pRanLines;
    		int lineTot=0;
    		char response[80];
    		char pMsgLine[80];
    		pollfd my_fds2; int poll_ret2;
    		my_fds2.fd = pipe2[0];
    		my_fds2.events = POLLRDNORM;
    		my_fds2.revents = POLLRDNORM;
    		int x;
    		int y;
    		int z;
    		int a;
    		srand(time(0));
    		while(msgCount < 101)
    		{
    			pRanLines = (1+ rand() %5);
    
    			if(msgCount == 100)
    				write(pipe1[1],"bye#",4); 
    			else
    			{
    				y=0;
    				for(x=0; x<pRanLines; x++)
    				{
    					pMsgLine[y] = 'm';
    					y++;
    					pMsgStr = BasicTypeToStr(msgCount);
    					for(z=0; z<pMsgStr.size(); z++)
    					{
    						pMsgLine[y]=pMsgStr[z];
    						y++;
    					}	
    					pMsgLine[y]=' ';
    					y++;
    					pMsgLine[y]='l';
    					y++;
    					pMsgLine[y]='i';
    					y++;
    					pMsgLine[y]='n';
    					y++;
    					pMsgLine[y]='e';
    					y++;
    					pMsgLine[y]=' ';
    					y++;
    					lineTot++;
    					pLinStr=BasicTypeToStr(lineTot);
    					for(z=0; z<pLinStr.size(); z++)
    					{
    						pMsgLine[y]=pLinStr[z];
    						y++;
    					}
    					pMsgLine[y]='*';
    					if(x == pRanLines-1)
    						pMsgLine[y]='#';
    					y++;
    
    				}
    				write(pipe1[1], pMsgLine, y);
    				
    			
    				do
    				{
    					poll_ret2=poll(&my_fds2, (nfds_t)1, 10);
    					if(poll_ret2>0)
    					{
    						read(pipe2[0],response,BSIZE2);
    						for(a=0;response[a]!='#';a++)
    							cout<<response[a];
    						cout<<endl;
    					}
    	
    				}while(poll_ret2 <= 0);			
    			}
    			msgCount++;
    		}
    	}
    
    }
    the code within else if(pid == 0) statement needs to be executed in that is another image, and I need to use one of the exec() fucntions, how could i make it so that the code executes in a different file and pass the pipes to the code so that messages can be transfered?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can use (almost) any number of files you want in any given program by associating them with a "stream". The "stream" you use will differ based on whether you use C or C++ syntax. Details on using streams to access files is available in the FAQ section---I believe.

    The FAQ section also review several ways to run different programs from within your current program.

    Good luck.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    according to the specs i have to use one of the exec functions to create a new child process image.....

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    maybe this will help, seemed semi-relevant from my point of view, not being familiar with exec(), anyway.

    http://www.opengroup.org/onlinepubs/...ions/exec.html
    This sounds like the information you need to pass to one of the exec() functions listed comes from the command line. An example is provided.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 08:44 PM
  4. Running 'exec' twice in one UNIX shell script
    By Zughiaq in forum Tech Board
    Replies: 2
    Last Post: 05-03-2003, 12:04 AM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM