Click to See Complete Forum and Search --> : Pipes in Linux question...
YourSurrogateGod
September 20th, 2004, 07:11 PM
I have an assignment in Linux where I have to implement some pipes. I was wondering if anyone out there has some information (site, tutorial, that would be great) on these things that goes into deep detail about how to use them. Thanks.
Joe Nellis
September 21st, 2004, 03:53 PM
Go pick up a copy of Sam's Teach Yourself Linux Programming in 24 Hours. I know, sounds hokey but there are good examples of pipes. Their motto on the book is "When you only have time for the answers", it's fairly accurate.
YourSurrogateGod
September 22nd, 2004, 09:25 PM
Go pick up a copy of Sam's Teach Yourself Linux Programming in 24 Hours. I know, sounds hokey but there are good examples of pipes. Their motto on the book is "When you only have time for the answers", it's fairly accurate.Hmm... I might.
dimm_coder
September 23rd, 2004, 06:46 AM
What do you mean saying "to implement some pipes"?
Do you have to write a program that uses standard pipe mechanizms provided by Linux or you have to implement your own mechanizms that represent the same behaviour as provided by standard Linux pipes?
In any case, read appropriate man pages (man 2 pipe) and, probably, some useful information you can find here http://www.erlenstar.demon.co.uk/unix/faq_toc.html.
dimm_coder
September 23rd, 2004, 06:57 AM
btw, follow this link http://www.emperorsrage.com/RTFM/ and download at least the "Linux Programming Unleashed" book in pdf.
Joe Nellis
September 23rd, 2004, 03:05 PM
Here is a low level pipe example in C++ from my college days. The pipe(filedescriptor[]) call assigns two pipes to the fd's. We fork a process and hand it both filedescriptors. The intention is that these are to be used like a patch cable between two networked computers. The parent process and the child process both have access to both FDs. One process "promises" it will only read from the first FD and only write to the second FD. The other process promises it will only write to the first FD and only read from the second FD.
I just looked in my Sam's Linux book and it doesn't have this stuff, it uses the popen() command.
// This program opens a pipe between two processes
// created with the fork command. One process sends
// messages typed from the keyboard or redirected input.
// The other process receives the messages sent by the
// other process through the pipe.
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define BUFFMAX 80
void RunChildProcess(int filediscriptors[]);
void RunParentProcess(int filediscriptors[]);
int main()
{
int fd[2]; // our file descriptors
int n;
// create our pipe here
if(pipe(fd))
{
perror("Pipe function failed");
exit(1);
}
// fork into two processes
int fret = fork();
switch (fret)
{
case -1:
perror("Fork failure");
exit(1);
break;
case 0:
RunChildProcess(fd);
break;
default:
RunParentProcess(fd);
break;
}
return 0;
}
// runs the parent forked process message send code
void RunParentProcess(int fd[])
{
int i=0;
// user instructions
cout << "\nSENDER: Enter \"quit\" by itself to exit program."<< endl;
// close the read end of our pipe; we are sending only
close(fd[0]);
// main input and message write loop
for(;;)
{
char output[BUFFMAX] = {'\0'};
// get user input one character at a time until buffer is
// full or a carriage return is pressed.
for(i=0;i<BUFFMAX-1; i++)
{
output[i] =cin.get();
if(output[i] == '\n' || output[i] == EOF)
break;
}
output[i]='\0';
// if user types "quit" then we stop sending
if(!strcmp(output, "quit"))
break;
// We only send messages of length 1 or more
if(strlen(output)>0)
{
if(write(fd[1], output, strlen(output))==-1)
{
perror("Write to reader failed");
exit(1);
}
}
// this sleep command helps to keep both processes
// from writing to screen at the same time.
// 1 second wait is realistically too long. Is there
// a sleep(.1 ) or something like that?
sleep(1);
}
// shut the pipe down. Now read can tell we are done
// sending messages.
close(fd[1]);
}
// runs the child fork process receive code.
void RunChildProcess(int fd[])
{
// the child PID that we use to identify received messages
// printed to screen
int thispid = getpid();
// user instructions
cout <<"\nchild"<< thispid <<": This program terminates when sender quits."
<<"\nchild"<< thispid <<": Ready to receive messages..." <<endl;
// close the write end of the pipe; we are only reading.
close(fd[1]);
// main message read loop
for (;;)
{
char input[BUFFMAX] = {'\0'};
// read from the pipe and fill our input buffer
int n = read(fd[0], input, BUFFMAX);
if (n == -1) // read function error
{
perror("Connection terminated");
close(fd[0]);
exit(1);
}
// we don't print empty strings.
if (n>0)
{
// Safety move: redundant null termination of string
input[n] = '\0';
// if the string starts with a null then no reason
// to print it.
if (strcmp(input,"\0"))
cout << "child"<< thispid<<": " << input << endl ;
}
// string has no length
// this is the sign that the write process has closed
// its file descriptor to the pipe. We should do the same
// and quit the program.
else
break;
}
}
YourSurrogateGod
September 24th, 2004, 03:42 PM
Thanks guys.
I sort of figured it out now. I needed to implement a shell with pipes (my job was only the pipes.) I already worked with them a little and I have something like what Joe put up in C. I have a rough idea of how they work, I just wasn't sure how I would have applied them in the context of my assignment, which is why I asked for extra info in hopes of figuring out how this worked (sorry about not being the exact specifications, I didn't want to seem that I just wanted someone else to do my job, I wanted to do it by myself with some extra ideas that you guys gave me.)
Manish Malik
September 26th, 2004, 02:21 PM
Beej's Guide to Unix Interprocess Communication (included : A fork() primer, Signals, (Named) Pipes, File Locking) : http://www.ecst.csuchico.edu/~beej/guide/ipc/ is another good resource on getting started with IPC.
:thumb:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.