Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Unix pipes

Name: noob 2009-11-07 21:00

How do you normally work with pipes in unix programming? Do you have one parent process that spawns many children and then keeps track of them or could you somehow make a nice chain where the child process fork() again and again and then wait() for each other or something?

Yeah its an assignment, and i have already solved it in an ugly way... Im just curious how you normally do this. Btw. one of the programs i have in my pipe-chain is less, and it wont work any other way than if i do a exec() in the parent process, but then i cant keep track of i'ts exit status. Can i somehow get it into the foreground of the console while still having it as a child to my parent process?

Name: Anonymous 2009-11-07 22:15

>>15
i just wrote a small program to test it:

#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>

void handler(int);

unsigned int children = 0;

main()
{
   signal(SIGCHLD, handler);

   int i;
   pid_t pid;
   for(i = 0; i < 5; ++i){
      pid = fork();
      if(pid == 0){
         return;
      }
      else{
          children++;
      }
   }
   while(children > 0);
   return;
}

void handler(int x)
{
   int status;
   pid_t pid;
   while((pid = waitpid(-1, &status, WNOHANG)) != -1){
      printf("child %d\n", pid);
      children--;
   }
   return;
}


note that the children variable is only required for the while loop to know when to stop at the end. that part of the code could be implemented much more elegantly

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List