0

I have a proposal to create a process with pipes and i have build 20 children. It works! But the most complicated matter is to fullfil the following requirements:

I have to create a grandson for each children with pair number (c.e. 2nd, 4rth, 6th,..)and finally i have to create a great-granson for each grandson which is divisible by 6. (c.e. 6th grandson, 12th, 18th)

I'm sorry but i'm novice with unix and concurrent proceses. Here it is my simply code as basis to start.

The code:

#include <unistd.h>
#include <sys/types.h>
main(){
pid_t pid;
int i, n=20;

for (i=0; i<n; i++) {
 pid=fork();
 if (pid == 0) break;
}
printf(“\n The father in the process %d is %d”, getpid(),getppid());
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user2924989
  • 29
  • 1
  • 6

1 Answers1

0

Not tested but i think this does what you want:

#include <unistd.h>
#include <sys/types.h>
main(){
pid_t pid;
pid_t spid;
pid_t sspid;
int i, n=20;

for (i=0; i<n; i++) {
    pid=fork();
    if (pid == 0){
        // Son process
        if(i%2 == 0){
            //Son process && even
            spid = fork();
            if (spid == 0){
                // Grand son process
                if(i%3 == 0){
                    sspid = fork();
                    if (sspid == 0){
                        // Great grand son process
                    } else {
                        // Grand son process
                    }
                }
            }
        }
        break; // to avoid continuing the for in the child processes
    } else {
        // Main process
    }
 }
 printf(“\n The father in the process %d is %d”, getpid(),getppid());
}
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • Hi again!! I tested it but still doesn't run. I compiled it and this is the error: "undefined reference to fork()" and "undefined reference to getpid()" – user2924989 Nov 17 '13 at 12:09
  • On windows this can help: http://stackoverflow.com/questions/13643278/library-that-has-reference-to-fork-in-c On linux I don't think you could get this error. On other op systems I have no idea. – Lajos Veres Nov 17 '13 at 12:16