Skip to content

Commit

Permalink
init comm
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pleva committed Nov 2, 2016
1 parent a0b0cf6 commit 717cdc2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
77 changes: 77 additions & 0 deletions main.c
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;
//}

0 comments on commit 717cdc2

Please sign in to comment.