Created
October 4, 2013 06:28
-
-
Save CapitanLiteral/6821780 to your computer and use it in GitHub Desktop.
Crea X hijos donde X es un parametro que le entramos por consola, la usamos para aprender a usar el exit, wait, y wexitstatus.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
pid_t pid; | |
int nProc = 0; | |
int i; | |
int sortida; | |
int entrada; | |
fflush(stdin); | |
//Conversió de argument de char a int | |
nProc = atoi(argv[1]); | |
// For per a crear els processos fills | |
for(i = 0; i < nProc; i++) | |
{ | |
pid = fork(); | |
switch(pid) | |
{ | |
case -1: /* error del fork() */ | |
perror("fork"); | |
break; | |
case 0: /* proceso hijo */ | |
//Asigno el valor calculat a una variable sortida | |
sortida = nProc * (getpid()%10); | |
//Trec la variable sortida per la funcio exit | |
exit(sortida); | |
break; | |
default: /* padre */ | |
// Espero el exit del del fill | |
wait(&entrada); | |
//imprimeixo el status del exit que li he pasat amb la variable sortida | |
printf("El valor retornat es:%d", WEXITSTATUS(entrada)); | |
printf("\n"); | |
} | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment