We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
BCS303 Operating Systems Laboratory
PROGRAM4
4. Develop a C program which demonstrates interprocess communication between a
reader process and writer process. Use mkfifo, open, read,write and close API’s in
your program.
DESCRIPTION:
‘The file structure related system calls available in the UNIX system let you create, open, and
close files, read and write files, randomly access files, alias and remove files, get information
about files, check the accessibility of files, change protections, owner, and group of files, and
control devices. These operations either use a character string that defines the absolute or
relative path name of a file, or a small integer called a file descriptor that identifies the /O
channel. A channel is a connection between a process and a file that appears to the process
‘aan unformatted stream of bytes. The kernel presents and accepts data from the channel as
‘4 process reads and writes that channel. To a process then, all input and output operations are
synchronous and unbuffered.
SYSTEM CALLS USED:
‘System calls are functions that a programmer can call to perform the services of the operating system.
‘Open (): Open () system call to open a file. open () returns a file descriptor, an integer specifying the
position of this open n file in the table of open files for the current process.
‘The return value is the descriptor of the file. Returns -1 if the file could not be opened. The first
parameter is path name of the file to be opened and the second parameter is the opening mode
Close (): Close () system call to close a file.
It is always good practices to close files when not needed as open files do consume resources and all
normal systems impose a limit on the number of files that a process can hold open.
Read (): The read () system call is used to read data from a file or other object identified by a file
descriptor.
fd is the descriptor, buf is the base address of the memory area into which the data is read and
MAX_BUF is the maximum amount of data to read. The retum value is the actual amount of data
read from the file. The pointer is incremented by the amount of data read. An attempt to read beyond
the end ofa file results in a return value of zero.
‘Write 0: The write () system call is used to write data to a file or other object identified by a file
descriptor.
Dept, Of Computer Science and Engineering, BIT 2023-2004 Page 24BCS303 Operating Systems Laboratory
PROGRAM:
/*reader process*/
#include
#include
Hinclude
#include
#include
#define MAX_BUF 1024
int mainQ)
int fd;
/* A temp FIFO file is not created in reader */
char *myfifo = "/tmp/myfifo.txt"
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);
return 0;
J
/*Writer Process*/
#tinclude
#include
#include
#include
#include
int main()
{
int fa;
char buf[1024];
/* create the FIFO (named pipe) */
char * myfifo = "/mp/myfifo.txt";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O.WRONLY);
(/* write "Hi" to the FIFO */
close(fd);
unlink(myfifo): /* remove the FIFO */
return 0;
}
gurPpuT
east
$ ./a.out
CR iS aSiny
Fer
ers
Crocco ss
Dept, Of Computer Science and Engineering, BIT 2023-2004