Thread: Using pipe and fork to form a ring

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Using pipe and fork to form a ring

    I've found some sample code online about how to create a ring with pipes and fork(). I'm to pass messages from one process to another through the pipe.

    A process reads in a number from its parent process, but apparently, fscanf() doesn't work as expected. (in main, after creation of pipe in the while loop) The output is not the same.

    Any idea what goes wrong? Thanks!

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX         512
    
    void make_trivial_ring(){
      int  fd[2];     
      pipe (fd);
      dup2(fd[0], STDIN_FILENO);
      dup2(fd[1], STDOUT_FILENO);
      close(fd[0]);
      close(fd[1]); 
    }
    
    
    void add_new_node(int *pid){
      int   fd[2];
      pipe (fd);
      *pid = fork();
      if (*pid > 0)  dup2(fd[1], STDOUT_FILENO);
      else           dup2(fd[0], STDIN_FILENO);
      close(fd[0]); 
      close(fd[1]); 
    }
    
    
    int main(int argc,  char *argv[ ])
    {
      int  k, i, childpid;
      int maxpid, maxnode, maxnum;
    
     
      make_trivial_ring();
      for (i = 1; i < atoi(argv[1]); i++){
        add_new_node(&childpid);
        if (childpid) break;   
      }
    
    fprintf(stderr, "123\n");
    fscanf(stderr, "%d", &maxpid);
    	fprintf(stderr, "%d\n", maxpid);
      
       //sleep(5);
       exit(0); 
    }

    Output:
    Code:
    123
    1307813
    123
    1307813
    123
    1307813

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can't fscanf an stderr. fscanf requires an input stream.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Is there any advice on what should I use instead of fscanf to read message from another process ? Thanks

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You could use read() to read from a socket file descriptor (an integer, not the FILE * like fscanf takes) into some buffer. Then you can use sscanf to parse the buffer in the same fashion as fscanf.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you draw a picture of what you're trying to do?

    Like which processes are reading/writing to.

    > if (*pid > 0) dup2(fd[1], STDOUT_FILENO);
    The parent has only one stdout.
    So each time you dup2(), the previous pipe end is lost.
    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. fork + pipe + dup2
    By caio1985 in forum C Programming
    Replies: 6
    Last Post: 09-07-2010, 02:18 PM
  2. Replies: 4
    Last Post: 10-14-2009, 04:44 PM
  3. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  4. fork() and pipe()
    By scioner in forum C Programming
    Replies: 3
    Last Post: 06-13-2008, 11:32 PM
  5. fork and pipe
    By tbarsness in forum C Programming
    Replies: 3
    Last Post: 10-22-2007, 12:40 PM