I have an assignment in which I need to declare a pipe in a header file. I really have no idea how to do this. It might be a really stupid question and I might be missing something obvious. If you could point me in the right direction I would greatly appreciate it.
Thanks for your time.
EDIT:
Sorry about the question being so vague. Maybe I need to reinforce my understanding of pipes.
I'm trying to create a pipe between two child processes. One child will write random characters into the pipe while the other child will read characters out of the pipe.
I guess I don't really understand what happens when I write something like:
int fd[2];
pipe = pipe(fd);
Am I right in saying that the writing and reading file descriptors for the pipe are put into fd[0]
and fd[1]
respectively? If in one of the child processes I close fd[1]
, that child could be thought of as my writer, correct?
EDIT 2:
Okay, it looks as if I pretty much have everything figured out and done, except I am getting an error pertaining to the file descriptors.
My code looks like this: (This is only the code relating to the pipe)
proj2.h
extern int fd[2];
proj2.c
int fd[2];
pipe(fd);
writer.c
close(fd[0]);
result = write(fd[1], &writeBuffer, sizeof(writeBuffer));
if(result < 0){
perror("Write");
}
reader.c
close(fd[1]);
result = read(fd[0], &readBuffer, sizeof(readBuffer))
if(result < 0){
perror("Read");
}
After executing the code, I get an error for every iteration of read() and write() with the error "Bad file descriptor". I've tried searching online to solve this myself, but I do not think I know enough about this material in order to do so. Any direction would be greatly appreciated once again. Everybody that has contributed has done a wonderful job so far, thank you very much. Also, if it looks like I'm just having you do my homework for me, I'm putting forth an honest effort and this isn't the entirety of the assignment.
EDIT 3:
Is the write() system call writing to standard output? What if I only want the contents to be printed after the reader reads them out of the pipe? How do I write them into the pipe without it writing them to standard output?
EDIT 4: I've figured everything out now. Thanks for all of the help everybody. The only thing I'm still curious about is if I could somehow get the status of the parent process. I've collected the statuses from the child process using the wait() system call and was wondering how to retrieve the status of the parent process.