Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <errno.h>
- struct children
- {
- int id;
- time_t start;
- time_t end;
- };
- int main(int arg, char **argv)
- {
- int anzKin = atoi(argv[1]);
- int maxGes = atoi(argv[2]);
- struct children child[anzKin];
- int rank[anzKin];
- char id = 'A';
- unsigned int end,pid,i,j,place=0,loop,count = 0;
- time_t temp;
- for(loop = 0; loop < anzKin; loop++)
- {
- child[loop].id = fork();
- id = id+loop;
- pid = getpid();
- if(child[loop].id==0)
- {
- //Kindprozess, bei fork() == 0
- srand(time(0) + getpid());
- for(i = 0; i < 5;i++)
- {
- usleep((10.0 * 1000000)/(rand() % maxGes + 1));
- for(j = 0; j <= i; j++)
- {
- printf("-");
- }
- printf("%c\n", id);
- }
- break;
- }
- else if(child[loop].id > 0)
- {
- //Vaterprozess, bei fork() > 0 -> PID Kindprozess
- printf("%d %c\n",(child[loop].id-pid), id);
- id = 'A';
- }
- else
- {
- perror("ERROR");
- }
- }
- if(getpid() > 0)
- {
- //Vaterprozess
- while( count < anzKin )
- {
- end = waitpid(-1, NULL, 0);
- rank[place] = end;
- place = place++;
- count = count++;
- }
- printf("RANK CHILD TIME\n");
- for(place = 0; place < anzKin; place++)
- {
- printf("%d %c\n", (place+1), ((rank[place]-pid)+64));
- }
- }
- else
- {
- perror("ERROR");
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment