-
-
Save AlessandroSpallina/a7938e2a4c6002920e8a to your computer and use it in GitHub Desktop.
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
/* | |
Author: Alessandro Spallina | |
Website: http://aleksnote.altervista.org | |
Email: alessandrospallina1@gmail.com | |
* Creare un programma che: | |
- Scrive a video "Ciao". | |
- Crea un figlio. | |
- Aspetta il primo figlio e quando finisce ne crea | |
un secondo. | |
Il primo figlio scrive a video "questa cartella contiene:" | |
e attende 3 secondi. | |
Il secondo figlio scrive a video il contenuto della cartella | |
(exec). | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <pthread.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
#include <signal.h> | |
int main(){ | |
int pid; | |
printf("Ciao"); | |
pid=fork(); | |
switch(pid){ | |
case -1: | |
perror("\n\nERR: Fork 1 Fallita"); | |
exit(EXIT_FAILURE); | |
break; | |
case 0: //figlio | |
printf("\nquesta cartella contiene:\n"); | |
sleep(3); | |
exit(EXIT_SUCCESS); | |
break; | |
default: //padre | |
waitpid(pid, NULL, 0); | |
pid=fork(); | |
switch(pid){ | |
case -1: | |
perror("\n\nERR: Fork 2 Fallita"); | |
exit(EXIT_FAILURE); | |
break; | |
case 0: | |
execl("/bin/ls", "ls", NULL); | |
exit(EXIT_SUCCESS); | |
break; | |
default: | |
waitpid(pid, NULL, 0); | |
exit(EXIT_SUCCESS); | |
break; | |
} | |
exit(EXIT_SUCCESS); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment