0% found this document useful (0 votes)
89 views1 page

Fifo PGM

This C code implements a simple client-server model using FIFOs instead of pipes. It creates two FIFOs, forks a child process, and has the parent and child open the FIFOs for reading and writing to allow communication between the client and server processes. The FIFOs are unlinked after the processes finish to remove the special files.

Uploaded by

Vani Garikipati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views1 page

Fifo PGM

This C code implements a simple client-server model using FIFOs instead of pipes. It creates two FIFOs, forks a child process, and has the parent and child open the FIFOs for reading and writing to allow communication between the client and server processes. The FIFOs are unlinked after the processes finish to remove the special files.

Uploaded by

Vani Garikipati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

/* use FIFOs, not PIPES to implement the simple client-server model.

*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
extern int errno;
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define PERMS 0666 /* octal value*/
main()
{ int childpid, readfd, writefd;
if ( (mkfifo(FIFO1, PERMS) < 0) && (errno != EEXIST))
err_sys("can't create fifo 1: %s", FIFO1);
if ( (mkfifo(FIFO2, PERMS) < 0) && (errno != EEXIST)) {
unlink(FIFO1);
err_sys("can't create fifo 2: %s", FIFO2); }
if ( (childpid = fork()) < 0) {err_sys("can't fork"); }
else if (childpid > 0) { /* parent */
if ( (writefd = open(FIFO1, 1)) < 0)
err_sys("parent: can't open write fifo");
if ( (readfd = open(FIFO2, 0)) < 0)
err_sys("parent: can't open read fifo");
client(readfd, writefd);
while (wait((int *) 0) != childpid) /* wait for child */
;
close(readfd);
close(writefd);
if (unlink(FIFO1) < 0)
err_sys("parent: can't unlink %s", FIFO1);
if (unlink(FIFO2) < 0)
err_sys("parent: can't unlink %s", FIFO2);
exit(0);
} else { /* child */
if ( (readfd = open(FIFO1, 0)) < 0)
err_sys("child: can't open read fifo");
if ( (writefd = open(FIFO2, 1)) < 0)
err_sys("child: can't open write fifo");
server(readfd, writefd);
close(readfd);
close(writefd);
exit(0);
}
}

You might also like