[ale]Stupid pipe question

Chris Fowler cfowler at outpostsentinel.com
Tue Dec 4 07:19:50 EST 2001


In my shell implementation, I forgot to put in the wait.  I had a wait in
the shell parent but not after the fork into the child which then forked
again to exec more.

Thanks,
Chris Fowler




-----Original Message-----
From: Joe Steele [mailto:joe at madewell.com]
To: ale at ale.org
Sent: Friday, November 30, 2001 6:19 PM
To: 'Chris Fowler'
Cc: Ale
Subject: RE: [ale]Stupid pipe question


The code as modified below should work.

--Joe

-----Original Message-----
From:	Chris Fowler [SMTP:cfowler at outpostsentinel.com]
Sent:	Friday, November 30, 2001 4:24 PM
To:	Ale
Subject:	[ale]Stupid pipe question

I'm trying to implement a simple pipe to more in a shell I'm creating.  I
ahve some test code below
vut is seems not to behave the way I expect.  It appears more does some
ioctls() on stdin which in my code is really a pipe.  What is the correct
way I should do this?

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
/* needed for wait() */
#include <sys/wait.h>


int
main(int argc, char *argv[])
{
        int pfd[2];
        int pid;
        int count; /* bytes read from input */
        char buffer[1024];


        pipe(pfd);

        pid = fork();

        if(pid == 0)
        {
                close(pfd[1]);
                dup2(pfd[0], 0);
/* putting this read() here doesn't make sense */
/*              read(0, buffer, sizeof(buffer));*/
                execl("/bin/more", "/bin/more", 0);
                perror("exec");
                exit(0);

        }

        close(pfd[0]);
        dup2(pfd[1], 1);
/* this while loop needs refining
 *      while(read(0, buffer, sizeof(buffer)) > 0)
 *              write(1, buffer, strlen(buffer));
 */
        while((count=read(0, buffer, sizeof(buffer))) > 0)
                write(1, buffer, count);
/* need to close all the descriptors for the pipe so "more" sees EOF */
        close(1);
        close(pfd[1]);
/* need to wait for the child to exit */
        wait(NULL);

        return 0;
}

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.




---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list