Advertisement
dimon2242

Untitled

Jul 2nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #define PERMS 0666
  9.  
  10. void exitt(int sgc){
  11.     exit(1);
  12. }
  13.  
  14. int main () {
  15.     int pid2, pid3, pid4, pid5, pid6, st;
  16.  
  17. //parent
  18.     printf("main parent process %d\n",getpid());
  19.     pid2 = fork();
  20.  
  21. //first child
  22.     if (pid2 == 0) {
  23.         int fd;
  24.         char fname[10];
  25.         sprintf(fname, "%d",getpid());
  26.         if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
  27.             perror("open file error");
  28.         }
  29.             close(fd);
  30.         printf("first child process%d\n",getpid());
  31.         }
  32.         pid3 = fork();
  33.  
  34. //second child
  35.     if (pid3 == 0){
  36.         int fd;
  37.         char fname[10];
  38.         sprintf(fname, "%d",getpid());
  39.         if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
  40.             perror("open file error");
  41.         }
  42.             close(fd);
  43.         printf("second child process%d\n", getpid());
  44.         }
  45.         pid4 = fork();
  46.  
  47. //third child
  48.     if (pid4 == 0){
  49.         int fd;
  50.         char fname[10];
  51.         sprintf(fname, "%d",getpid());
  52.         if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
  53.             perror("open file error");
  54.         }
  55.             close(fd);
  56.         printf("third child process%d\n",getpid());
  57.         }
  58.         pid5 = fork();
  59.  
  60. //fourth child
  61.     if (pid5 == 0){
  62.         int fd;
  63.         char fname[10];
  64.         sprintf(fname, "%d",getpid());
  65.         if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
  66.             perror("open file error");
  67.         }
  68.             close(fd);
  69.         printf("fourth child process%d\n",getpid());
  70.         }
  71.         pid6 = fork();
  72. //fifth child
  73.     if (pid6 == 0){
  74.         int fd;
  75.         char fname[10];
  76.         sprintf(fname, "%d",getpid());
  77.         if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
  78.             perror("open file error");
  79.         }
  80.             close(fd);
  81.         printf("fifth child process%d\n",getpid());
  82.         signal (SIGINT, exitt);
  83.  
  84. }
  85. if ( pid6 < 0) {
  86. printf("can't create process 6:error %d\n", pid6);
  87. signal (SIGINT, exitt);
  88. kill(pid2,SIGINT);
  89.  
  90. sleep(1);
  91. }
  92. else
  93. if (pid5 < 0){
  94.     printf("can't create process 5:error %d\n", pid5);
  95.     signal (SIGINT, exitt);
  96.     kill(pid5,SIGINT);
  97. }
  98. else
  99. if (pid4 < 0){
  100.     printf("can't create process 4:error %d\n", pid4);
  101.     signal (SIGINT, exitt);
  102.     kill(pid4,SIGINT);
  103.  
  104. }
  105. else
  106. if (pid3 < 0){
  107.     printf("can't create process 3:error %d\n", pid3);
  108.     signal (SIGINT, exitt);
  109.     kill(pid3,SIGINT);
  110.  
  111. }
  112. else
  113. {
  114. if (pid2 < 0)
  115.     printf("can't create process 2:error %d\n", pid2);
  116.     signal (SIGINT, exitt);
  117.     kill(pid2,SIGINT);
  118. }
  119. wait(&st);
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement