Thread: Counting return values father/child/grand child ..processes

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

    Counting return values father/child/grand child ..processes

    I'm totally lost with counting return values of the following code i have created.
    The idea is to count all the processes with the wexitstatus command.

    When i aisle a part of the code, for instance childs th program works well but no idea how to add in a counter the granson, great grandson to the child processes.

    I have used the command exit (p) then wait(&status) and a counter at the ende of the code. it works with the following code1:

    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> 
     
    #include "rutines.h" 
     
     
    /* Functions prototypes*/ 
    void show_help(); 
     
    int main(int argc, char *argv[]) 
    { 
        int p,pid[32], num_children, sortida, contador; 
        int d=10; 
        int e=100; 
        char msg[MAXSTR]; 
     
         
         
        if (argc>2) 
        { 
            show_help("pas1ok: Error in Arguments"); 
            exit(1); 
        } 
     
        sscanf (argv[1], "%d", &num_children); 
        // Creating 20 Children processes. 
        for(p=1;p<(num_children+1);p++) 
        { 
            pid[p] = fork(); 
            if (pid[p]<0) 
                error("[pas1ok::main] Error creating child process."); 
            else 
                if (pid[p]==0)             
                {     
                    sprintf(msg, "Child Process %d created -> Order: %d Father: %d.\n",getpid(),p,getppid()); 
                    write_string(msg,""); 
                    /* Child process */  
                     
                    sprintf(msg, "Child proces ended %d  \n",getpid()); 
                    write_string(msg,""); 
                    exit(p); 
                } 
     
                else 
                    wait(&sortida);  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
         
                    contador=contador+WEXITSTATUS(sortida); 
        } 
         
        sprintf(msg, "Father proces %d  -> Total return values: %d  \n",getpid(), contador); 
        write_string(msg,""); 
                                 
        exit(EXIT_SUCCESS); 
    } 
     
     
     
    /******************************************************************************/ 
    /* Function name:    show_help                                                                    */ 
    /* Description:    Shows the error message and the program usage instructions    */ 
    /* Parameters:    The error message.                                                        */ 
    /* Returns:            Nothing.                                                                     */ 
    /******************************************************************************/ 
     
    void show_help(char *err_message) 
    { 
        write_string(err_message,""); 
        write_string("Usage: pas1ok",""); 
    }
    The idea is to create the account for the full code:

    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> 
     
    #include "rutines.h" 
     
     
    /* Functions prototypes*/ 
    void show_help(); 
     
    int main(int argc, char *argv[]) 
    { 
        int p,pid[32], num_children; 
        int d=10; 
        int e=100; 
        char msg[MAXSTR]; 
     
         
         
        if (argc>2) 
        { 
            show_help("pas1ok: Error in Arguments"); 
            exit(1); 
        } 
     
        sscanf (argv[1], "%d", &num_children); 
        // Creating 20 Children processes. 
        for(p=1;p<(num_children+1);p++) 
        { 
            pid[p] = fork(); 
            if (pid[p]<0) 
                error("[pas1ok::main] Error creating child process."); 
            else 
                if (pid[p]==0)             
                {     
                    sprintf(msg, "Child Process %d created -> Order: %d Father: %d.\n",getpid(),p,getppid()); 
                    write_string(msg,""); 
                    /* Child process */  
                    if (p%2==0) 
                    { 
                        // Creating grandson processes. 
                        pid[p] = fork(); 
                        if (pid[p]<0) 
                            error("[pas1ok::main] Error creating grandchild process."); 
     
                        if (pid[p]==0) 
                        { 
                            sprintf(msg, "  Grandchild Process %d created -> Order: %d Father: %d.\n",getpid(),p*d,getppid()); 
                            write_string(msg,""); 
                             
                            exit(EXIT_SUCCESS); 
                        } 
                        else 
                            wait(NULL);  // Waiting grandchild processes                                 
                    } 
                    if (p%6 == 0) 
                    { 
                        // Creating great-grandson processes. 
                        pid[num_children+p] = fork(); 
                        if (pid[num_children+p]<0) 
                            error("[pas1ok::main] Error creating grandchild process."); 
     
                        if (pid[num_children+p]==0) 
                        { 
                            sprintf(msg, "    Great-Grandson Process %d created -> Order: %d Father: %d \n",getpid(),p*e,getppid()); 
                            write_string(msg,""); 
                             
                            exit(EXIT_SUCCESS); 
                        } 
                        else 
                            wait(NULL);  // Waiting great-grandchild processes                                 
                    } 
                     
                    sprintf(msg, "Child proces ended %d -> \n",getpid()); 
                    write_string(msg,""); 
                    exit(EXIT_SUCCESS); 
                } 
     
                else 
                    wait(NULL);  // Waiting grandchild processes     
         
        } 
         
        sprintf(msg, "Father proces %d  -> Total return values:  \n",getpid()); 
        write_string(msg,""); 
                                 
        exit(EXIT_SUCCESS); 
    } 
     
     
     
    /******************************************************************************/ 
    /* Function name:    show_help                                                                    */ 
    /* Description:    Shows the error message and the program usage instructions    */ 
    /* Parameters:    The error message.                                                        */ 
    /* Returns:            Nothing.                                                                     */ 
    /******************************************************************************/ 
     
    void show_help(char *err_message) 
    { 
        write_string(err_message,""); 
        write_string("Usage: pas1ok",""); 
    }
    Thanks so much for your help. Waiting for questions..
    toni

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Consider using this form at each stage.
    Code:
            pid[p] = fork();
            if (pid[p]<0)
                error("[pas1ok::main] Error creating child process.");
            else
                if (pid[p]==0) runChild(pid,p);
            else
                wait(NULL);
    Within runChild(), you call another function called say runGrandChild()

    It will stop you having a bloated main() with massive indentation.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would grandson (etc) be any different? A grandson is just a child process (of your child), so your great-grandson will report to your grandson, which will then report to your son, which will then report to you and you're done.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    Barcelona - Spain
    Posts
    18
    Thanks for your help!!. I advanced one more step with the code and now the problem is the following.I used wait and exit process combined with WEXITSTATUS to count all the process (intermediate and total) are included in the code. Now my problem is the following. Hear the image representing where is the mistake. The child is finished before the great-grand child is created.

    Then i have to create the intermediate quantities of process: For instance: Child 2, grandchild 20 then the result is 22.

    And finally to show the total of intermediates.

    Here the code. I think i'm close to the right result but i cannot see the light.

    Thanks for all

    Counting return values father/child/grand child ..processes-captura-de-pantalla-2013-12-01-15-30-02-jpg

    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>
    
    #include "rutines.h"
    
    
    
    
    
    void show_help();
    
    int main(int argc, char *argv[])
    {
        int p,pid[100];
        int d=10;
        int e=100;
        int fills;
        int status;
        int sumador;
        char msg[MAXSTR];
    
        if (argc>1)
        fills=atoi(argv[1]);
        if (argc==1)
        {
            show_help("Error");
            exit(1);
        }
    
    
        // Creem N processos fill
        for(p=1;p<fills+1;p++)
        {
            pid[p] = fork();
            if (pid[p]<0)
                error("Error");
            else
                if (pid[p]==0)
                {
                sprintf(msg, "CHILD process %d creat -> Ordre: %d Pare: %d.\n",getpid(),p,getppid());
                    write_string(msg,"");
    
                    /* Proces del fill */
                    if (p%2 == 0)
                    {
                        // Creant al net
                        pid[p] = fork();
                        if (pid[p]<0)
                            error("Error");
    
                        if (pid[p]==0)
                        {
                            sprintf(msg, "  GRANDCHILD process %d creat -> Ordre: %d Pare: %d.\n",getpid(),p*d,getppid());
                            write_string(msg,"");
    
                            exit(WEXITSTATUS(status)+p*10);
                        }
                        else
                            wait(&status); // Esperant el net
    
    
                            if (p%6 == 0)
                            {
                            // Creant del bestnet
                            pid[p] = fork();
                            if (pid[p]<0)
                                error("Error");
    
                            if (pid[p]==0)
                            {
                                sprintf(msg, "    GREAT-GRAND CHILD process %d creat -> Ordre: %d Pare: %d \n",getpid(),p*e,getppid());
                                write_string(msg,"");
    
                                exit(p*100);
                            }
                            else
                                WEXITSTATUS(status);  // Esperant al besnet processes
                            }
    
                    }
    
                    sprintf(msg, "CHILD process acabat %d -> %d \n",getpid(),WEXITSTATUS(status));
                    write_string(msg,"");
                    exit(WEXITSTATUS(status)+p);
                }
                else
                    wait(&status);  // Esperant net
        }
    
        sumador=sumador+WEXITSTATUS(status);
    
        sprintf(msg, "Proces principal pare %d creat -> Ordre %d Pare: %d .\n",getpid(),0,getppid());
        sprintf(msg, "Numero de valors de retorn: %d en total.\n ",sumador);
        write_string(msg,"");
    
        exit(EXIT_SUCCESS);
    }
    
    
    
    void show_help(char *err_message)
    {
        write_string(err_message,"");
        write_string("Us de Pas1ok","");
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your grandchild process should be creating the great-grandchild process, surely?

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

    Advancing but not at all..

    Quote Originally Posted by tabstop View Post
    Your grandchild process should be creating the great-grandchild process, surely?
    Here it is a little advance but not work well.

    Counting return values father/child/grand child ..processes-captura-de-pantalla-2013-12-01-18-25-22-jpg

    If you look at child 2, the total amount must be 2+20=22 and the result is totally different.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For one: Your great-grand child process needs to have a parent of 17027 (otherwise, it's just a grandchild). Your grandchild needs to create your great-grandchild, which means the code for doing so must occur before line 63 (in your most-recently-posted code), as that is where your grandchild shuffles off this mortal coil.

    That also might make your exit line make sense in your grandchild code, as now there is a reason for status to perhaps mean something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Child processes
    By jsx-27 in forum C Programming
    Replies: 3
    Last Post: 08-24-2012, 02:27 AM
  2. forks and child processes
    By kpax in forum C Programming
    Replies: 1
    Last Post: 05-28-2008, 04:47 AM
  3. sockets and child processes
    By Elkvis in forum Linux Programming
    Replies: 2
    Last Post: 03-06-2008, 04:03 PM
  4. Exchanging environment between father and child processes
    By Leonardo in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2003, 01:29 PM
  5. Child processes in Linux
    By Music_Man in forum Linux Programming
    Replies: 0
    Last Post: 03-20-2003, 09:04 AM

Tags for this Thread