Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <signal.h>
  6. #include <sys/wait.h>
  7.  
  8. int c_i;
  9. int create_and_pause(int i)
  10. {
  11. int pid = fork();
  12. if (pid<0)
  13. {
  14. printf("error");
  15. }
  16. if(pid==0)
  17. {
  18. printf("[Child process %d: %d]Was created by %d and will pause\n",i,getpid(),getppid());
  19. //pause();
  20. c_i=i;
  21. raise(SIGSTOP);
  22.  
  23. }
  24. return pid;
  25. }
  26.  
  27. int main(int argc, char * argv[])
  28. {
  29. int i;
  30.  
  31. int N =5;
  32. int d[5] = {1,1,2,1,2}; //na antikatastathei me loop pou pairnei argv
  33. int p[N];
  34. int father_p = getpid();
  35.  
  36. for(i = 0; i<N; i++)
  37. {
  38. if(getpid()==father_p)
  39. {
  40. p[i] = create_and_pause(i);
  41. sleep(1);
  42. }
  43. }
  44.  
  45. if(getpid() == father_p)
  46. {
  47. printf("tis ftiaksame oles yay \n");
  48.  
  49. for(i =0; i<N; i++)
  50. {
  51. kill(p[i],SIGCONT);
  52.  
  53. }
  54. printf("ekana se oles continue\n");
  55. }
  56.  
  57.  
  58. int j = 0 , cnt = 0 ;
  59.  
  60. if(getpid()!=father_p)
  61. {
  62.  
  63.  
  64. sleep(2);
  65. printf("[Child Process %d: %d]Is starting \n",c_i,getpid());
  66. for(j =0; j<10;j++) //tha antikatastathei apo while true
  67. {
  68. cnt++;
  69. sleep(d[c_i]);
  70. printf("process %d counter is %d\n",getpid(),cnt);
  71. }
  72. printf("eimai h %d kai molis teliosa\n",getpid());
  73. exit(0);
  74.  
  75. }
  76.  
  77. if(getpid() == father_p)
  78. {
  79. printf("twra kanw wait poutanes\n");
  80. for(i=0;i<N;i++)
  81. {
  82. wait(NULL);
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement