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