One more question about forks/pipes

This is a discussion on One more question about forks/pipes within the C Programming forums, part of the General Programming Boards category; Thanks again to all who helped me out yesterday. I tried to finish up this program (takes 2 chars from ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    7

    One more question about forks/pipes

    Thanks again to all who helped me out yesterday. I tried to finish up this program (takes 2 chars from user [using forking/pipes] and outputs them). This is what I have so far and it isn't working:

    Code:
    #include <stdio.h>
    
    int main( int argc, char * argv[] ){
    
    	int i, j, buff = 0;
    	char p[2];
    	char string[256];
    
    	if( pipe(p)==-1 ){
    		printf("pipe failed");
    		exit(-1);
    	}
    
    	printf("Enter two chars to compare: \n");
    	for(i; i<2; i++) {
    		switch( fork() ){
    		case 0: 
    			buff = getchar();
    			printf("PID of child &#37;d is %d\n", i+1, getpid() );
    			write( p[1], &buff, sizeof(int) );
    			return 0;
    			break; 
    		case -1:
    			printf("A forking error has occurred\n");
    			exit(-1);
    			break;
    		default:
    			break;
    		}
    	}
    
    	printf( "PID of parent is %d\n", getpid() );
    
    	while( i-- ){
    		read( p[0], &buff, sizeof(int) );
    		printf("this came out: %c\n", buff);
    	}
    }
    when I compile and run I get the following:

    Code:
    bash-2.03$ gcc -oq2 q2.c
    bash-2.03$ q2
    Enter two chars to compare: 
    PID of parent is 9624
    t
    this came out: 
    g
    PID of child 2 is 9626
    
    PID of child 1 is 9625
    c
    this came out: 
    bash-2.03$
    Can anybody tell me whats wrong?
    Last edited by suprropmp; 05-19-2007 at 07:54 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Reading through your code multiple times, it appears that besides any other problems you might have, you're never read a value from the user.

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Code:
    #include <stdio.h>
    First of all, you are missing the header files for fork() and pipe(). What this means is that you functions have implicit definitions.

    Code:
    char p[2];
    char string[256];
    Here you have 'magic numbers'.

    Now, here is what I suspect what is going on. I think you want to do something like

    parent<------------------------pipe--------------------------------------filter program
    stdout---------------------------------------------------------------------------stdin
    |-----------------------------------------------------------------------------------^
    | -----------------------------------------------------------------------------------|
    |--------------- --------------------------------------------------------------------|
    prompt------>terminal------------------------------------------------------->|


    Right?

    The problem is that you are confusing buffering on the terminal with buffering over a pipe(). Buffering on a terminal is "line buffered." Buffering over a pipe() isn't. You might want to consider getting the book "Advanced Programming in the Unix Enviroment" by Stevens and Rago. I think it's Chapter 15 that talks about IPC's.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,820
    > char p[2];
    Read the pipe() manual page to find out the correct type.

    > return 0;
    Read about _exit() to decide whether it is the more appropriate way of exiting from a child process.
    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. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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