[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

UNIX simple pipe() program.

Asked by vupadhya in C Programming Language

Tags: pipe, unix, c, simple, program

/*
    Simple, simple pipe() sample.
    Main program stdout -> child stdin -> chile stdout -> Main program stdout
    cc spipe.c -o spipe

    To run
    >./spipe
    any text
    any text
    ^D
   
    or
    >./spipe < jim.txt

*/

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int fd1[2];      /* stdin pipe Main->Child */
int fd2[2];      /* stdout Pipe Child->Main */

void oops(char *err, int exit_code)
{
    fprintf(stderr, "%s\n", err);
    exit(exit_code);
}

/*
    Child process
*/
void p1(void)
{
      int c;
      /* Close pipe output */
      close(fd1[1]);
      /* Close stdin - 0, duplicate pipe input to 0 - it will new stdin */      
      if(-1 == dup2(fd1[0], 0))
          oops("dup2 error 0", 100);
      /* Close old descriptors */
      close(fd1[0]);

      /* Close pipe input*/
      close(fd2[0]);
      /* Close stdout - 1, duplicate pipe input to 1 - it will new stdout */      
      if(-1 == dup2(fd2[1], 1))
          oops("dup2 error 1", 101);
      /* Close old descriptors */
      close(fd1[1]);

      /* Then read from stdin and write to stdout */
      write(1, "\n---Begin---\n", 13);
      while(EOF != (c = getchar()))
          write(1, &c, 1);
      write(1, "\n---End---\n", 11);

      exit(0);
}


main()
{
    int c;
/* Creating pipe */
    if(-1 == pipe(fd1))
      oops("pipe error 1", 1);
    if(-1 == pipe(fd2))
      oops("pipe error 2", 2);
                        /* Split process */
    if(fork()){                  /* if fork() > 0 then parent */
                        /* Write parent stdin to child */
      close(fd1[0]);
      while(EOF != (c = getchar()))
          write(fd1[1], &c, 1);
                        /* Close pipe descriptors */
      close(fd1[1]);
                        /* Close pipe output */
      close(fd2[1]);
                        /* Read child output and send it so stdout */
      while(1 == read(fd2[0], &c, 1))
          write(1, &c, 1);
      close(fd2[0]);
      wait(0);
    }
    else                   /* If fork() == 0  then child */
      p1();
}

I am running this on UNIX solaris machine, Ideally, this program should output whatever I input to it. However, this is what happens:

$ ./spipe
saaddsad
^D
---Begin---

---End---
$

Any ideas? I should get saaddsad in the output.


 
Loading Advertisement...
 
[+][-]04/24/05 05:22 AM, ID: 13853235Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C Programming Language
Tags: pipe, unix, c, simple, program
Sign Up Now!
Solution Provided By: van_dy
Participating Experts: 2
Solution Grade: A
 
[+][-]04/23/05 05:06 PM, ID: 13851991Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/23/05 06:58 PM, ID: 13852261Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92