Hello everyone,
I am trying to create a c program which uses two unnamed pipes for parent and child communication. Parent asks for user option in one line command (for instance, display myfile.txt, uppercase myfile.txt etc ) passes the information to child, and the child executes the commands accordingly (i.e. displays file, converts it to uppercase, lowercase etc.). I have not been able to figure out how the user input can be passed from parent to the child using the pipe. The pipes are communicating but not the way I want (i.e. the parent writes to the pipe "The commhand is: %s %s" instead of "The command is: display myfile.txt". I really appreciate your help. My code is below:
#include <stdio.h>#include <string.h>#include <sys/wait.h>#include <sys/errno.h>main(){ int pid; int c[2]; int p[2]; FILE *fp; /*file pointer to user selected file */ char userfile[256] = ""; /*file selected by the user*/ char line[127] = ""; char buff[100]; char option[10] = ""; int rc; //create 2 pipes if (pipe(p) < 0 || pipe(c) < 0){ puts("Error creating the pipe"); exit(0); } // end if pipe fails if ( (pid = fork() ) > 0 ) { /* parent code */ close(p[0]); /* close unused pipe components */ close(c[1]); /* close unused pipe components */ printf("Parent: Please enter a command: "); scanf("%s" "%s", option, userfile); rc = write(p[1], "The command is: %s %s\n", *option, *userfile, sizeof("The command is: %s %s\n", *option, *userfile)); if(rc < 0 ) { perror("Parent's write failed"); exit(1); } /* end of rc < 0 */ rc = read(c[0], buff, sizeof(buff) ); if(rc < 0 ) { perror("Parent's read failed"); exit(1); } /* end of rc < 0 */ printf("[Child] %s", buff); write(p[1], "The command was executed\n", sizeof("The command was executed\n")); if(rc < 0 ) { perror("Parent's write failed"); exit(1); } /* end of rc < 0*/ read(c[0], buff, sizeof(buff) ); if(rc < 0 ) { perror("Parent's read failed"); exit(1); } /* end of rc < 0 */ printf("[Child] %s", buff); close(p[1]); /* finished writing p[1] */ close(c[0]); /* finished reading c[0] */ return(0); } /* end of parent process */ else if (pid == 0) { /* child */ close(p[1]); /* reads from p[0] */ close(c[0]); /* writes to c[1] */ rc = read(p[0], buff, sizeof(buff) ); if(rc < 0 ) { perror("Child's read failed"); exit(1); } /* end of rc < 0*/ printf("[Parent] %s", buff); rc = write(c[1], "Executing the command: %s %s\n", sizeof("Executing the command: %s %s\n")); if(rc < 0 ) { perror("Child's write failed"); exit(1); }/* end of rc < 0 */ if(strcmp(option, "display") == 2){ fp = fopen(userfile, "r"); if((fp == NULL)) { printf("Error: File could not be opened\n"); rc = write(c[1], "The file was not displayed\n", sizeof("The file was not displayed\n")); exit(1); } while(!feof(fp)) { if(fgets(line, 255, fp)) printf("%s", line);} rc = read (p[0], buff, sizeof(buff) ); if(rc < 0 ) { perror("Child's read failed"); exit(1); } /* end of rc < 0*/ printf("[Parent] %s", buff); rc = write(p[1], "The file was displayed\n", sizeof("The file was displayed\n")); if(rc < 0 ) { perror("Child's write failed"); exit(1); } /* end of rc < 0*/ _exit(0); /* child terminates*/ } /* else if (strcmp(option, "uppercase") == 2){ } else if (strcmp(option, "lowercase") == 2){ } else if (strcmp(option, "rename") == 2){ } else if (strcmp(option, "exit") == 2){ }*/ rc = read (p[0], buff, sizeof(buff) ); if(rc < 0 ) { perror("Child's read failed"); exit(1); } /* end of rc < 0*/ printf("[Parent] %s", buff); rc = write(c[1], "Hello again from child\n", sizeof("Hello again from child\n")); if(rc < 0 ) { perror("Child's 2nd write failed"); exit(1); } /* end of rc < 0*/ _exit(0); /* child terminates*/ } /* end of child */ if(pid < 0 ){ /* check errors in fork*/ printf("Fork error"); exit(1); } /* end of error check */} /* end of main */
thank you for your response: I took a quick look at the links that you provided, and am sure they will help me a lot.
Thanks again.