/* 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 #include #include /* Per system call wait */ #include /* 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); }