iniziale
This commit is contained in:
commit
f158cda768
55
doc.c
Normal file
55
doc.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
bhoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooowo
|
||||||
|
|
||||||
|
*/
|
||||||
|
#include <stdio.h> //printf, scanf
|
||||||
|
#include <sys/wait.h> //waitpid
|
||||||
|
#include <stdlib.h> //rand, exit
|
||||||
|
#include <unistd.h> //fork, getpid, getppid, sleep
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) //argv=[fork_multipli,6]
|
||||||
|
{
|
||||||
|
int i, num_figli=3, v[30], c, d, s=0;
|
||||||
|
if (argc>1)
|
||||||
|
num_figli = atoi(argv[1]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf ("i figli sono : %d, \n", num_figli);
|
||||||
|
for(i=0; i<30; i++)
|
||||||
|
{
|
||||||
|
v[i]=rand() % 20;
|
||||||
|
printf ("%d-", v[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
d=30/num_figli;
|
||||||
|
printf ("\nInserisci il numero da cercare: ");
|
||||||
|
scanf("%d", &c);
|
||||||
|
printf ("\n numero da trovare: %d\n", c);
|
||||||
|
}
|
||||||
|
pid_t pid[num_figli];
|
||||||
|
|
||||||
|
for(i=0; i<num_figli; i++)
|
||||||
|
{
|
||||||
|
if ((pid[i] = fork()) < 0)
|
||||||
|
{
|
||||||
|
perror("Errore nella fork().\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if(pid[i]== 0)
|
||||||
|
for(s=0; s<d; s++){
|
||||||
|
if(v[s]==c){
|
||||||
|
printf("%d figlio di %d: ha trovato %d, all'indice %d.\n il figlio è %d", getpid(), getppid(), c, s, i);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s=s+10;
|
||||||
|
d=d+10;
|
||||||
|
}
|
||||||
|
|
||||||
|
//riprendo a gestire il flusso del padre
|
||||||
|
int x;
|
||||||
|
wait;
|
||||||
|
printf("consegnato");
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user