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

    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:

    #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");
    	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;
    		case -1:
    			printf("A forking error has occurred\n");
    	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:

    bash-2.03$ gcc -oq2 q2.c
    bash-2.03$ q2
    Enter two chars to compare: 
    PID of parent is 9624
    this came out: 
    PID of child 2 is 9626
    PID of child 1 is 9625
    this came out: 
    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
    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
    Join Date
    May 2007
    Berkeley, CA
    #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.

    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
    | -----------------------------------------------------------------------------------|
    |--------------- --------------------------------------------------------------------|


    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
    The edge of the known universe
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    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
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    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