This is what I came up with. Notice the use of "perror" throughout -- you should look this command up and learn to use it:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
char buffer[1024];
int fin, fout;
pid_t pid;
if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo");
if ((pid=fork())<0) perror("fork");
if (pid==0) { /* the child writes */
if ((fin=open("pipefile", O_WRONLY))<3) perror("child open");
if (write(fin,"hello world\0",12)<0) perror ("child write");
return 0;
} else { /* the parent reads */
if ((fout=open("pipefile", O_RDONLY))<3) perror("parent open");
if (read(fout,buffer,1024)<0) perror("parent read");
printf("Message: \"%s\"\n",buffer);
}
return 0;
}
1) Make the fifo and set read/write permission for the owner.
2) Fork and open the fifo for writing by the child process (then write something into it).
3) Back in the parent process, open the fifo for reading, read, and print the mesage to the screen.
4) Stir carefully and allow to cool.
The reason I used <3 with read and write is that 0,1, and 2 are the standard file descriptors (in, out, and error).