1 // kompilacia: gcc -o priklad12 priklad12.c 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 #include <time.h> 7 #include <sys/wait.h> 8 #include <errno.h> 9 10 int main () { 11 int pid; 12 pid = fork(); 13 14 if (pid == -1) { 15 perror("fork"); 16 exit(EXIT_FAILURE); 17 } 18 19 if (pid == 0) { 20 // dcersky proces - pockame 5 sekund 21 sleep(5); 22 // a spustime ping neexistujucihost 23 char * args[] = {"ping", "neexistujucihost", NULL}; 24 execvp("ping", args); 25 26 // ak sa execvp nepodarilo, vratime hodnotu 50 27 exit(50); 28 } 29 30 // rodicovsky proces 31 int status; 32 waitpid(pid, &status, 0); 33 if (WIFEXITED(status)) { 34 printf("dcersky proces skoncil s navratovou hodnotou %i\n", WEXITSTATUS(status)); 35 } else if (WIFSIGNALED(status)) { 36 printf("dcersky proces bol terminovany signalom cislo %i\n", WTERMSIG(status)); 37 } else if (WCOREDUMP(status)) { 38 printf("dcersky proces vyprodukoval core dump\n"); 39 } else if (WIFSTOPPED(status)) { 40 printf("dcersky proces bol stopnuty signalom cislo %i\n", WSTOPSIG(status)); 41 } 42 43 exit(EXIT_SUCCESS); 44 } 45