?
Solved

pipe problems

Posted on 2010-11-16
2
Medium Priority
?
287 Views
Last Modified: 2012-05-10
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
Comment
Question by:Mathilda100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 34151361
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
 

Author Closing Comment

by:Mathilda100
ID: 34162899
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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question