it:informatica:linux:semplice_sincronizzazione_tra_processi_system_call_fork_wait_ed_exit
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linux/semplice_sincronizzazione_tra_processi_system_call_fork_wait_ed_exit?do=
Semplice sincronizzazione tra processi (system call fork, wait ed exit)
Concetti:
Esempio di sincronizzazione tra un processo padre ed un processo figlio. La generazione dei processi è effettuata mediante la system call fork()
, mentre per la sincronizzazione sono state utilizzate le system call wait()
e exit()
. Infine la system call sleep()
è stata utilizzata per bloccare il processo figlio per 10 secondi.
Testo:
Realizzare un programma che:
- tramite la system call
fork()
genera un processo figlio - il padre, dopo aver generato il processo figlio, si mette in attesa della terminazione del figlio attraverso la system call
wait()
- il figlio attende 10 secondi attraverso la system call
sleep()
, dopodiché termina utilizzando la system callexit()
. Il figlio dovrà passare attraverso la system callexit()
il valore 123 al padre. - il padre, una volta attesa la terminazione del figlio, attraverso le macro
WIFEXITED
eWEXITSTATUS
dovrà recuperare il valore passato dal figlio e stamparlo.
Soluzione:
- fork_wait.c
/* Realizzare una semplice sincronizzazione tra processi utilizzando: - la system call fork per generare un processo figlio - le system call wait ed exit per sincronizzare il processo figlio con il processo padre */ #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> /* Per system call wait */ #include <unistd.h> /* Per system call fork */ int main() { pid_t pid; int status; pid = fork(); /* Il processo figlio viene creato: da qui in avanti vengono */ /* eseguiti in modo concorrente i processi padre e figlio */ if (pid == 0) { /* Processo figlio: la fork restituisce il valore 0 */ printf("FIGLIO\n"); sleep(10); /* Il figlio attende 10 secondi */ exit(123); /* Il figlio termina restituendo al padre il valore 123 */ } else { /* Processo padre: la fork restituisce il pid del figlio */ printf("PADRE\n"); wait(&status); /* Il padre attende la fine del figlio */ if (WIFEXITED(status)){ /* Restituisce valore vero se il figlio e' terminato normalmente */ printf("STATUS: %d\n", WEXITSTATUS(status)); /* Recupero il valore restituito dal figlio */ } printf ("PADRE TERMINATO\n"); } printf("DONE\n"); return (EXIT_SUCCESS); }
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linux/semplice_sincronizzazione_tra_processi_system_call_fork_wait_ed_exit?do=
/web/htdocs/www.skenz.it/home/data/pages/it/informatica/linux/semplice_sincronizzazione_tra_processi_system_call_fork_wait_ed_exit.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1