informatica:sol:laboratorio15:esercitazionia:scexamples
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
| Entrambe le parti precedenti la revisioneRevisione precedente | |||
| informatica:sol:laboratorio15:esercitazionia:scexamples [26/04/2015 alle 09:49 (11 anni fa)] – [pipe2proc.c] Massimo Torquati | informatica:sol:laboratorio15:esercitazionia:scexamples [26/04/2015 alle 13:34 (11 anni fa)] (versione attuale) – [command.c (chiamate: fork, wait, getpid, execvp)] Massimo Torquati | ||
|---|---|---|---|
| Linea 112: | Linea 112: | ||
| </ | </ | ||
| + | ==== zombie.c (chiamate: fork, waitpid, getpid, execlp) ==== | ||
| + | |||
| + | Il seguente programma crea un numero di processi '' | ||
| + | |||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // utility macro | ||
| + | #define SYSCALL(r, | ||
| + | if((r=c)==-1) { perror(e); | ||
| + | |||
| + | int main(int argc, char * argv[]) { | ||
| + | const int SEC=2; | ||
| + | |||
| + | if (argc != 2) { | ||
| + | fprintf(stderr, | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | | ||
| + | int nproc = atoi(argv[1]); | ||
| + | int pid; | ||
| + | for(int i=0; | ||
| + | SYSCALL(pid, | ||
| + | if(pid==0) { // figlio | ||
| + | sleep(SEC); | ||
| + | exit(0); // esco con successo | ||
| + | } else | ||
| + | printf(" | ||
| + | } | ||
| + | | ||
| + | // solo il processo padre arriva qui | ||
| + | |||
| + | // aspettiamo un po' in modo da essere " | ||
| + | // che i processi figli siano terminati | ||
| + | sleep(2*SEC); | ||
| + | |||
| + | printf(" | ||
| + | SYSCALL(pid, | ||
| + | if(pid==0) { | ||
| + | execlp(" | ||
| + | perror(" | ||
| + | exit(errno); | ||
| + | } else { | ||
| + | int stato; | ||
| + | printf(" | ||
| + | SYSCALL(pid, | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | // adesso attendiamo la terminazione dei processi | ||
| + | for(int i=0; | ||
| + | int stato; | ||
| + | SYSCALL(pid, | ||
| + | if(WIFEXITED(stato)) | ||
| + | printf(" | ||
| + | | ||
| + | } | ||
| + | printf(" | ||
| + | SYSCALL(pid, | ||
| + | if(pid==0) { | ||
| + | execlp(" | ||
| + | perror(" | ||
| + | exit(0); | ||
| + | } else { | ||
| + | int stato; | ||
| + | SYSCALL(pid, | ||
| + | " | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| ==== pipe2proc.c (chiamate: fork, wait, getpid, write, pipe, dup, execve) ==== | ==== pipe2proc.c (chiamate: fork, wait, getpid, write, pipe, dup, execve) ==== | ||
informatica/sol/laboratorio15/esercitazionia/scexamples.1430041771.txt.gz · Ultima modifica: 26/04/2015 alle 09:49 (11 anni fa) da Massimo Torquati
