Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

pipe problems

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 */

Open in new window

0
Mathilda100
Asked:
Mathilda100
1 Solution
 
phoffricCommented:
Don't forget to always check for errors on all library calls!

Since a number of experts are on a field trip for a few days, and since I am too busy at the moment to provide extensive assistance, and since you haven't received a response yet, then the least I can do is see if some other links can help you. (If you have further questions after reading this, then please hit the Request Attention button and ask for a wider audience of experts to help you. Adding the C++ zone will also help.)

    http://www.experts-exchange.com/Programming/Languages/C/Q_25118224.html

    http://www.experts-exchange.com/Programming/Languages/C/Q_25111362.html

Here is a brief tutorial and includes sample programs:
    http://tldp.org/LDP/lpg/node11.html

read from a file (i.e., in your case pipe descriptors):
   http://www.cs.utah.edu/flux/moss/node37.html
write:
    http://www.cs.utah.edu/flux/moss/node44.html 
0
 
Mathilda100Author Commented:
Hello phoffric,
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now