
   deja.com [20x20.gif] Please visit our sponsor 
   
   Before You Buy Help  |  Contact Us  |  My Deja
   
   [20x20.gif]
    Home >> Discussions >> linux . dev . c-programming
    >>  linux.dev.c-programming 
   DISCUSSIONS SEARCH
     Power Search
   _______________   [button_search.gif]-Submit
   [20x20.gif]
   
   [20x20.gif]
   
   [20x20.gif] [20x20.gif] [20x20.gif] [20x20.gif]
   >> Forum: linux.dev.c-programming
   >> Thread: help in a c program with pipes
   >> Message 1 of 3
   [20x20.gif]
   Subject: Re: help in a c program with pipes
   Date: 01/03/2000
   Author: Daniel Beer <dlbeer@insomnia.localdomain>
                                                                      << previous  .  next >>
   
   On 1 Jan 2000 20:23:11 GMT, ad_gordon <ad_gordon@hotmail.com> wrote:
   >hi,
   >i'm new to linux and i'm trying to learn programing in c under linux. (c i
   >know).
   >i'm trying to write a program, that will simulate the pipe.
   >like for example: MyProgName ls more.
   >each command is one argument for example and the output of one program will
   >become the input of the other one.
   >i've read about fork,dup,execlp,  but still i dont understand it to the
   >full.
   >can anyone help me with a short program?
   >also if any one knows about url's that teaches about this commands i'll be
   >most thankful.
   >
   >adi
   >ad_gordon@hotmail.com
   >
   
   As you probably know, file descriptors are refered to by integer values, and each process
   has 3 standard file descriptors defined:
   
   0 - Input e.g. fgetc();
   1 - Output e.g. printf("Hello\n");
   2 - Error e.g. perror("Hello");
   
   Whenever you call an input function, it reads from file descriptor 0. What you want to do,
   is change a sub-processes standard file descriptors so that instead of reading from a
   terminal, it's reading from a pipe. First off, you need a pipe...this is easily done:
   
     int mypipe[2];
   
     pipe(mypipe);
   
   Anything that you write through the descriptor mypipe[1] comes out of mypipe[0] when you
   try to read it.  Next thing, you need to fork and execute your command, and write stuff
   into the pipe:
   
     #include <stdio.h>
     #include <strings.h>
     #include <unistd.h>
   
     int
     main(void) {
       int mypipe[2];
       int i;
       char *msg="I am a message in a pipe\n";
   
       pipe(mypipe);
       i=fork();
       if(i<0) {  /* fork() returns -1 on error */
         perror("fork");
         exit(-1);
       }
       if(!i) {    /* fork() returns 0 in the child process */
         /* If we're here, we must be the child process */
         dup2(0, mypipe[0]); /* <-- this is the important bit */       /* We've told our
   child process not to read from whatever it's         currently reading from (probably a
   terminal), but to read from         our pipe */
         /* Now, to transfer control of the process to the new program */
   execlp("mail", "mail", "-s", "hello", "your-login-name", NULL);       /* If we're still
   here, it can't have worked, since execlp()         doesn't return */
         perror("execlp");
         exit(-1);
       }
       /* If we're here, we must be the parent process.
         The child will be running and waiting for input. */
       write(mypipe[1], msg, strlen(msg));
       write(mypipe[1], ".\n", 2);
       write(mypipe[1], "\n", 1);
     }
   
   What the above program will do is send an email to your-login-name. Reading from a pipe is
   much the same, except the line:
   
     dup2(0, mypipe[0]);
   
   will be replaced by:
   
     dup2(1, mypipe[1]);
   
   and you will read from mypipe[0].  If you need any further help, please email me.
   
   --
      _     _
    o' \,=./ `o   Daniel Beer <dlbeer@sdf.lonestar.org>
       (o o)      dlbeer.freeshell.org
   ooO--(_)--Ooo-
                                                                      << previous  .  next >>
   Subscribe to linux.dev.c-programming
   Mail this message to a friend
   View original Usenet format
   Create a custom link to this message from your own Web site
   
                                       Search Discussions
                                                
     For a more detailed search in Discussions go to Power Search
   Search only in: (_) linux.dev.c-programming
   (_) All Deja.com
   Search for: ___________________________________ Search
   Search  [recent] discussions
     Please visit our sponsor 
   Please visit our sponsor 
   Explore More:
   
   Enter to win a Kenmore
   range from sears.com
   
   Computer & Tech Jobs
   Upgrade your Career!
   
   Post your resume at the
   Deja.com Career Center
   
   Find Company Information
   At Hoover's Online
   
   Free Maps
   Mapquest.com
   
   Forbes.com Newsletter
   Click here to sign up
   
   Digicams under $299
   at Computers4Sure.com
   
   Great Music?
   Click Here!
   
   Thousands of Products
   Buy at cost @ eCOST.com
   
   FREE Arc Radio!
   When you try Netmarket.
   
     Arts & Entertainment   Automotive   Computing   Consumer Electronics   Health & Fitness
      Home & Family   Lifestyles   Money   Politics & Media   Recreation   Sports   Travel
   
        Computers4Sure.com - Get Free Stuff at FreeShop! - ComputerJobs - TireRack.com -
   Cost+Pricing at eCOST.com - Search for Jobs! JobOptions - Win at Sears - GetMaps@MapQuest
                                                
   Copyright (c) 1995-2000 Deja.com, Inc. All rights reserved.
   Trademarks . Terms & Conditions of Use . Site Privacy Statement.
   Advertise With Us!  |  About Deja.com  |  We're Hiring 
