Thread: Terminating Pipe Program

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Terminating Pipe Program

    I'm writing a program that creates four different pipes between four different processes, and relays messages through each pipe. I've written all of the code for passing the messages, however I'm having problems terminating the program once everything is finished. I believe the problem may be in this section
    Code:
    if((pid2=fork())==0){		int i; close(log[0]); close(bc[1]);  
    		while(i<10){
    			sleep(1); 
    			bytes = read(bc[0], msg, 3); 
    			if(msg[0]=='e')
    				exit(1); 
    			time(&now); 
    			n = sprintf(msg2, "C received %s at %s", msg, ctime(&now)); 
    			write(log[1], msg2, strlen(msg2)+1); 
    			i++; 
    		}
    		close(bc[0]); close(log[1]); 
    		exit(1);   
    	} 	
    	if((pid3=fork())==0){
    		int i; close(bd[1]); close(log[0]); 
    		while(i<10){
    			sleep(5); 
    			bytes = read(bd[0], msg, 3); 
    			if(msg[0]=='e')
    				exit(1); 
    			time(&now);
    			n = sprintf(msg2, "D received %s at %s", msg, ctime(&now)); 
    			i++; 
    		}
    		close(bd[0]); close(log[1]); 
    		exit(1); 
    	}
    Any suggestions as to how I can terminate the program otherwise? I have the rest of the source code if required.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(msg[0]=='e')
    > exit(1);
    Perhaps consider using the break; statement to exit the loop, so you actually call the necessary close()'s to close the ends of the pipes.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display something before terminating the program
    By cprogrammer1980 in forum C Programming
    Replies: 1
    Last Post: 03-17-2011, 12:43 PM
  2. Terminating Program
    By millsy2000 in forum C Programming
    Replies: 11
    Last Post: 04-22-2010, 11:31 PM
  3. How can I get my program to stop terminating?
    By mklickman in forum C Programming
    Replies: 3
    Last Post: 09-19-2006, 11:06 AM
  4. Terminating a GLUT program?
    By glo in forum Game Programming
    Replies: 4
    Last Post: 07-05-2006, 02:37 AM
  5. Terminating a program
    By whisper11 in forum C++ Programming
    Replies: 1
    Last Post: 02-03-2005, 12:59 PM