c - How does this code work between two process? -


i doing practice , got code recently.

#include<sys/types.h> #include<stdio.h> #include<string.h> #include<unistd.h>  #define buffer_size 25 #define read_end 0 #define write_end 1  int main(int argc,char *argv[]){                                         if (argc==2){                                                            char output[buffer_size];                                            close(4);                                                            while(1){             read(3,output,buffer_size);                                          if(output[0]=='q'&&output[1]==null){                                     return 0;             }             printf("%s\n",output);                                           }     }      if(argc==1){                                                             char write_msg[buffer_size]="a";                                     int fd[2];                                                           pid_t pid;                                                            if(pipe(fd)==-1){                                                        fprintf(stderr,"pipe failed");             return 1;         }          pid = fork();                                                         if(pid<0){                                                               fprintf(stderr,"fork failed");             return 1;         }          if(pid>0){                                                               close(fd[read_end]);                                                 while (write_msg[0]!='q'||write_msg[1]!=null){                           scanf("%s",write_msg);                                               write(fd[write_end],write_msg,strlen(write_msg)+1);              }              close(fd[write_end]);                                            }         if(pid==0){                                                              execl("/usr/bin/xterm","xterm","-e",argv[0],"1",null);          }         return 0;         } } 

i want ask how work. in opinion, pipe can communicate between parent process , child process. chile proecss in execute execlp function open new terminal window , run same program sequentially. new window isn't child of parent process. however, can receive string pipe file descriptor. can't understand why , wanna know how work between 2 process.

i've been confused while. hope me. thanks.

but child proccss in execute execlp function open new terminal window , run same program sequentially. new window isn't child of parent process.

that's not right. new window is child process. execl() replaces child process process image it's still child process. so, parent , child can communicate via pipe. code classic example , parent , child can communicate via unix pipe.

the child process executes if (argc==2){ ... } code because it's given arugment (in execl() -- argc 2). parent process intended run no arguments.

by way, there couple of issues:

1) want check if output[1] \0 (null byte), not null. that'd make sense in order terminate communication (and processes) inputting q. so, this:

 if(output[0]=='q'&&output[1]==null){                     

should be:

 if(output[0] == 'q'&&output[1] == '\0'){ 

2) similarly,

  while (write_msg[0]!='q'||write_msg[1]!=null){ 

should be:

  while (write_msg[0] != 'q' || write_msg[1] != '\0'){ 

Comments