Solved

pipe problems

Posted on 2010-11-16
2
283 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
2 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX 5.x set up arrow to recall 11 53
what is the difference between "sudo su" and "su - root" 6 117
Writing a UNIX script menu allowing for multiple options. 5 56
delete-remove 14 85
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

770 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