Like Tree1Likes
  • 1 Post By tabstop

Processes / Pipes creating Son, Grandson, great-grandson

This is a discussion on Processes / Pipes creating Son, Grandson, great-grandson within the C Programming forums, part of the General Programming Boards category; I have a proposal to create a process with pipes and i have build 20 children. It works! But the ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Barcelona - Spain
    Posts
    18

    Processes / Pipes creating Son, Grandson, great-grandson

    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.
    Thanks so much!!
    The code:

    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());
    }
    

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    As you say, you have to use pipes. I can give some advice but I only learnt this two minutes ago so forgive me for any mistakes.

    You need to call pipe (pipe(2) - Linux manual page) before you fork the processes. The file descriptors that you get from pipe will then be copied in to your child processes (as all data is when forked) and you can use them to communicate between processes. Simply use file writing functions like fprintf or fwrite to write data to the pipe and fscanf or fread to read data from the pipe. Best of luck.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When a child is created, all the state is the same as the parent, yes? So the child will know the value of i and can therefore create a new generation if necessary (in place of the break, perhaps).
    DeadPlanet likes this.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    Barcelona - Spain
    Posts
    18
    Thenks so much i will check your recommendations. Instead i have created a simply code but i have problems with the printf..any idea where is the problem? Here i post the code:

    Thanks again!!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/time.h>
    #include <unistd.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("The father in the process %d is %d", getpid(),getppid());
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You'll have to be more clear what you mean about "problems with the printf".

    I could guess that you mean "sometimes it prints that the parent pid is 1", and that's because if the parent process ends before the child does (and there's no reason why it shouldn't) then the child is placed under the benevolent protection of init.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help in Creating Pipes in Linux
    By nickman in forum C Programming
    Replies: 1
    Last Post: 10-26-2013, 11:21 AM
  2. Creating a Pipe between 2 processes
    By eZACKe in forum C Programming
    Replies: 3
    Last Post: 04-08-2012, 05:31 PM
  3. creating an array of pipes
    By arkashkin in forum Linux Programming
    Replies: 1
    Last Post: 08-30-2010, 09:22 AM
  4. Creating processes
    By xErath in forum Linux Programming
    Replies: 5
    Last Post: 10-09-2004, 01:36 PM
  5. Replies: 4
    Last Post: 09-21-2003, 03:00 PM

Tags for this Thread


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