Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6.  
  7. pid_t newChildProcess(){
  8.   pid_t child_pid = fork(); //create childProcess
  9.   if(child_pid==0){
  10.     /*Child Process*/
  11.     printf("Child Process\n");
  12.     close(mypipefd[1]);
  13.     read(mypipefd[0],buf1,sizeof(buf1));
  14.     b = buf1;
  15.     b++;
  16.   execl(argv[0],argv[0],b,NULL);
  17.   }else{
  18.     /*Parent Process*/
  19.     close(mypipefd[0]); // close useless stdin stream
  20.     write(mypipefd[1], hex_num_a,sizeof(hex_num_a)); //stream to child
  21.     (void)wait(&status); // wait for child
  22.     printf("Parent Process\n");
  23.   }
  24. }
  25.  
  26. int main(int argc, char *argv[]) {
  27.   pid_t child_pid1;
  28.   pid_t child_pid2;
  29.   int mypipefd[2];
  30.   int ret;
  31.   char buf[20];
  32.   int status;
  33.   char *b = argv[1];//argv[1]
  34.   char *hex_num_a = argv[1];
  35.   char *hex_num_b = argv[2];
  36.  
  37.   char buf1[4];
  38.   memset(buf1,0,sizeof(buf1));
  39.   ret  = pipe(mypipefd);
  40.  
  41.   if(ret== -1){
  42.     perror("pipe");
  43.     exit(1);
  44.   }
  45.  
  46.   if(b[0] !='1'){
  47.     exit(EXIT_FAILURE);
  48.   }
  49.  
  50.   child_pid11 = fork();
  51.    
  52.   if(child_pid1==0){
  53.     /*Child Process*/
  54.     if(child 1){
  55.       printf("Child Process\n");
  56.       close(mypipefd[1]);
  57.       read(mypipefd[0],buf1,sizeof(buf1));
  58.       printf("danach %s\n", buf1);
  59.       //fprintf(stdout, "danach %s\n", buf1);
  60.       b = buf1;
  61.       b++;
  62.       fprintf(stderr,"hallo %s\n",b);
  63.     }else if(child 2){
  64.  
  65.     }
  66.     execl(argv[0],argv[0],b,NULL);
  67.   }else{
  68.     /*Parent Process*/
  69.  
  70.     /*create 4 child processes*/
  71.     newChildProcess();
  72.     newChildProcess();
  73.     newChildProcess();
  74.     /* ------------- */
  75.     close(mypipefd[0]);
  76.     child_pid2 = fork();
  77.     fprintf(stderr, "davor %s\n", hex_num_a);
  78.     write(mypipefd[1], hex_num_a,sizeof(hex_num_a));
  79.     (void)wait(&status);
  80.     printf("Parent Process\n");
  81.   }
  82.   /* code */
  83.   return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement