I am in a scenario where I have to write a program that creates two processes. The father process opens a text file for reading. It is assumed that the file is composed of words formed of alphabetic characters separated by spaces. The child process enters a word on the keyboard. The father process looks for the word in the file, and passes to the child the value 1 if the word is in the file, and 0 otherwise. The son displays the result.
Here I think used the tubes for communication between these processes. However, this communication seems to me difficult. Does this ordering: process father-process son-process father-process son is possible?
Attaching a simple pipe program for basic understanding. One can modify according to her needs.
/* simple_pipe.c
Simple demonstration of the use of a pipe to communicate
between a parent and a child process.
Usage: simple_pipe "string"
The program creates a pipe, and then calls fork() to create a child process.
After the fork(), the parent writes the string given on the command line
to the pipe, and the child uses a loop to read data from the pipe and
print it on standard output.
*/
#include <sys/wait.h>
#include "tlpi_hdr.h"
#define BUF_SIZE 10
int
main(int argc, char *argv[])
{
int pfd[2]; /* Pipe file descriptors */
char buf[BUF_SIZE];
ssize_t numRead;
if (argc != 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s string\n", argv[0]);
if (pipe(pfd) == -1) /* Create the pipe */
errExit("pipe");
switch (fork()) {
case -1:
errExit("fork");
case 0: /* Child - reads from pipe */
if (close(pfd[1]) == -1) /* Write end is unused */
errExit("close - child");
for (;;) { /* Read data from pipe, echo on stdout */
numRead = read(pfd[0], buf, BUF_SIZE);
if (numRead == -1)
errExit("read");
if (numRead == 0)
break; /* End-of-file */
if (write(STDOUT_FILENO, buf, numRead) != numRead)
fatal("child - partial/failed write");
}
write(STDOUT_FILENO, "\n", 1);
if (close(pfd[0]) == -1)
errExit("close");
_exit(EXIT_SUCCESS);
default: /* Parent - writes to pipe */
if (close(pfd[0]) == -1) /* Read end is unused */
errExit("close - parent");
if (write(pfd[1], argv[1], strlen(argv[1])) != strlen(argv[1]))
fatal("parent - partial/failed write");
if (close(pfd[1]) == -1) /* Child will see EOF */
errExit("close");
wait(NULL); /* Wait for child to finish */
exit(EXIT_SUCCESS);
}
}
Have a look on more examples here on Chapter-44 The Linux Programming Interface. Keep this book on your table or e-book on you computer, it's a bible for Linux programming.