Parent/Child comms homework

This is a discussion on Parent/Child comms homework within the C Programming forums, part of the General Programming Boards category; i have assignment and i cant solve it can any one help me plz?? Create a text file (commands.txt) and ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Parent/Child comms homework

    i have assignment and i cant solve it can any one help me plz??

    Create a text file (commands.txt) and store the following commands in that file.
    ls l
    ps
    who

    Write a C/C++ program that does the following:

    Creates a pipe and forks and child process.

    The parent process reads each command from the commands.txt and passes the command to the child process (one command at a time) using a pipe. The child process reads each command from the pipe and executes the commands. The command and the output of each command are saved in a file output.txt. Note that the child process should written in such a way that it can execute as many commands as sent to it by the parent process (it should not be only for 3 commands, assume the child process does not know the number of commands to be sent to it by the parent process).

    The parent process then prints the output of all the commands from output.txt after removing the commands and inserting two blank lines before the output of each command. .


    The file output.txt is then removed.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why don't you:

    1) Create your own thread
    2) Show us what is not working.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by csgirl View Post
    i have assignment and i cant solve it can any one help me plz??
    We don't do homework, we offer advice and consolation. Ie, at least try, then you can post your attempt or whatever it is that's not working for you.

    Also, this should have been a fresh thread, hopefully one of the mods will fork it off as the other posts are not really relevant altho the topic is similar.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Moved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    okii i will try to solve it then posted here

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    3
    THIS IS MY ANSWER THAT I TRY

    THE PROBLEM , THE PROG RETURN THE BYTES OF COMMANDS.TXT

    Code:
    #include<sys/wait.h>
    #include<assert.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<string.h>
    #include<iostream>
    #include<fstream> 
    using namespace std;
    int main()
    {	
    
    	// create file named commands.txt
    	system("ls -l >commands.txt");
    	system("ps >> commands.txt");
    	system("who >>commands.txt");
    
    		
      		char filename[80],fn[80];
    		char  info;
    		string cmd="cat";
    
    		// create pipe 
    		int p[2];
    		
    		pipe(p);
    		
    		// create fork
    		
    		int f = fork();
    		
    		if ( f == 0) 
    			{
    				
    				cout<<"Enter file name:";
    				cin>>filename;
    				close(p[0]);
    				write(p[1],filename,strlen(filename));
    				exit(0);
    
    			}
    
    
    		else 
    		{
    			
     			wait(NULL);
    			close(p[1]); // CLOSE WRITE SIDE
    			 
    			info=read(p[0],fn,sizeof(fn));
    			cout<<"\nReceiving string:"<<info<<endl;
    			system(cmd.c_str());
    			cout<<info<<endl;
    			
    
    				
    			
                     }
    
    	
    return 0 ;
    }



    BUT I WANT THE PARENT READ FROM THE COMMANDS.TXT THEN WRITE IT IN CHILD USING PIPE

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    Create a text file (commands.txt) and store the following commands in that file.
    ls –l
    ps
    who

    If you did this then the following code will overwrite the file. Is this really what you want to do????

    Code:
            // create file named commands.txt
    	system("ls -l >commands.txt");
    	system("ps >> commands.txt");
    	system("who >>commands.txt");

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    1. Get the parent to just read the text file, and print it to the screen.
    2. Parent sends the strings to the child, then it prints them to the screen.
    3. Child executes the commands instead of printing them (you see the output on screen)
    4. Child redirects to a file.
    and so on
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21