-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Pleva
committed
Nov 2, 2016
1 parent
a0b0cf6
commit 717cdc2
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <math.h> | ||
//#include <iostream.h> | ||
//#include <random> | ||
|
||
// | ||
//int main(int argc, char **argv) | ||
//{ | ||
// printf("hello world\n"); | ||
// return 0; | ||
//} | ||
|
||
//1. Viz priklad 1 z predchoziho tutorialu. | ||
// Potomci budou koncit s nahodnym cislem status exit( ?? ); | ||
// Rodic bude sledovat navratovy kod. | ||
// Potomci budou nahodile havarovat, napr. spatny pointer, deleni 0... | ||
// Rodic tyto havarie pozna. */ | ||
|
||
int main(void){ | ||
|
||
pid_t pids[10]; | ||
int i, pds; | ||
int n = 10; | ||
|
||
for (i = 0; i < n; ++i) { | ||
if ((pids[i] = fork()) < 0) { | ||
perror("fork"); | ||
abort(); | ||
} else if (pids[i] == 0) { | ||
srand(time(NULL) ^ (getpid()<<16)); | ||
int ex_out = (rand() % 10) + 1; | ||
printf("Syn s PID=%d a vratim %d \n", getpid(), ex_out); | ||
if ((ex_out == 1) || (ex_out == 5)){ | ||
printf("Dnes neni dobry den, delime nulou\n"); | ||
double b = 23/0; | ||
} else { | ||
printf("Syn s PID=%d a vratim %d \n", getpid(), ex_out); | ||
exit(ex_out); | ||
} | ||
exit(ex_out); | ||
} else { | ||
printf("Rodic s pid %d \n", getpid()); | ||
|
||
} | ||
} | ||
int status; | ||
while ((pds = wait(&status)) > 0) | ||
{ | ||
if(WIFEXITED(status) != 0){ | ||
printf("Synator vratil %d \n", WEXITSTATUS(status)); | ||
} | ||
else { | ||
printf("Synator se prepocital (%d) \n", WEXITSTATUS(status)); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
//int f = fork(); | ||
|
||
|
||
|
||
// if(f == 0){ | ||
// int ex_out = rand() % 10 + 1; | ||
// printf("Syn s PID=%d a vratim %d \n", getpid(), ex_out); | ||
// exit(ex_out); | ||
// } | ||
// else { | ||
// | ||
// | ||
// return 0; | ||
//} | ||
|