Guest User

Untitled

a guest
Apr 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <errno.h>
  6.  
  7. struct children
  8. {
  9. int id;
  10. time_t start;
  11. time_t end;
  12. };
  13.  
  14. int main(int arg, char **argv)
  15. {
  16. int anzKin = atoi(argv[1]);
  17. int maxGes = atoi(argv[2]);
  18. struct children child[anzKin];
  19. int rank[anzKin];
  20. char id = 'A';
  21. unsigned int end,pid,i,j,place=0,loop,count = 0;
  22. time_t temp;
  23.  
  24.  
  25. for(loop = 0; loop < anzKin; loop++)
  26. {
  27. child[loop].id = fork();
  28. id = id+loop;
  29.  
  30. pid = getpid();
  31.  
  32. if(child[loop].id==0)
  33. {
  34. //Kindprozess, bei fork() == 0
  35. srand(time(0) + getpid());
  36.  
  37. for(i = 0; i < 5;i++)
  38. {
  39. usleep((10.0 * 1000000)/(rand() % maxGes + 1));
  40. for(j = 0; j <= i; j++)
  41. {
  42. printf("-");
  43. }
  44.  
  45. printf("%c\n", id);
  46. }
  47.  
  48. break;
  49. }
  50. else if(child[loop].id > 0)
  51. {
  52. //Vaterprozess, bei fork() > 0 -> PID Kindprozess
  53. printf("%d %c\n",(child[loop].id-pid), id);
  54. id = 'A';
  55. }
  56. else
  57. {
  58. perror("ERROR");
  59. }
  60. }
  61.  
  62. if(getpid() > 0)
  63. {
  64. //Vaterprozess
  65. while( count < anzKin )
  66. {
  67. end = waitpid(-1, NULL, 0);
  68.  
  69. rank[place] = end;
  70. place = place++;
  71. count = count++;
  72. }
  73. printf("RANK CHILD TIME\n");
  74.  
  75. for(place = 0; place < anzKin; place++)
  76. {
  77. printf("%d %c\n", (place+1), ((rank[place]-pid)+64));
  78. }
  79. }
  80.  
  81. else
  82. {
  83. perror("ERROR");
  84. }
  85.  
  86.  
  87. return 0;
  88. }
Add Comment
Please, Sign In to add comment