Lec9 CS604 Pps
Lec9 CS604 Pps
Lec9 CS604 Pps
Systems
Lecture 9
Agenda for Today
Review of previous lecture
UNIX/Linux IPC tools and
associated system calls
UNIX/Linux standard files and
kernel’s mechanism for file access
Use of pipe in a program and at the
command line
Recap of the lecture
14 September 2019 © Copyright Virtual University of
Pakistan
Review of Lecture 8
Interprocess communication (IPC)
Establish link and use send/recv
Issues about links: establishing links,
link capacity, links per pair of processes,
processes per link, message size, uni-
or bi-directional
Direct communication
Indirect communication
14 September 2019 © Copyright Virtual University of
Pakistan
Review of Lecture 8
Process synchronization
Buffering capacity of a link
P1 P2
Pipe
14 September 2019
UNIX/Linux System
© Copyright Virtual University of
Pakistan
UNIX/Linux IPC Tools
Named pipe (FIFO): For
communication between unrelated
processes on a system
P1 P2
FIFO
Network
Socket Connection Socket
14Computer 1
September 2019 © Copyright Virtual University of Computer 2
Pakistan
UNIX/Linux Pipe
Important system calls
open, read, write, close,
pipe
open: Open or create a file
read: Read from a pipe
write: Write data to a pipe
close: Close/destroy a pipe
pipe: Create a pipe for IPC
14 September 2019 © Copyright Virtual University of
Pakistan
open System Call
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open (const char *path, int oflag,
/* mode_t mode */ ...);
‘oflag’ specifies purpose of
opening the file and ‘mode’ specifies
permission on the file if it is to be
created.
14 September 2019 © Copyright Virtual University of
Pakistan
open System Call
Returns a file descriptor on success
and –1 on failure
Can specify that read and write will
be blocking or non-blocking
‘oflag’ value is constructed by
ORing various flags: O_RDONLY,
O_WRONLY, O_RDWR, O_NDELAY (or
O_NONBLOCK), O_APPEND, O_CREAT,
14etc.
September 2019 © Copyright Virtual University of
Pakistan
open System Call
Call fails
Non-existent file
Operation specified is not allowed
due to file permissions
Search not allowed on a
component of pathname
User’s disk quota on the file
system has been exhausted
14 September 2019 © Copyright Virtual University of
Pakistan
open System Call
Call fails
No write permission on the directory
in which the file is being created
Signal was caught during open
Process has reached the limit of
maximum open files
System limit reached on maximum
number of simultaneous open files
14 September 2019 © Copyright Virtual University of
Pakistan
read System Call
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);
…
… …
OPEN_MAX — 1
14 September 2019 © Copyright Virtual University of
Pakistan
Standard Descriptors
in UNIX/Linux
Three files are automatically
opened for every process for the
process to read its input from and
send its output and error messages
to. These files are called standard
files: standard input, standard
output, and standard error.
14 September 2019 © Copyright Virtual University of
Pakistan
Standard Descriptors
in UNIX/Linux
By default, standard files are
attached to the terminal on which a
process runs
Descriptors for standard files are
known as standard file descriptors.
Standard input: 0 (keyboard)
Standard output: 1 (display screen)
Standard error: 2 (display screen)
14 September 2019 © Copyright Virtual University of
Pakistan
pipe() Call
int pipe (int pipedes[2]);
Call fails
At least two slots not empty in the
PPFDT—too many files or pipe open
in the process
Buffer space not available in the
kernel
File table is full
14 September 2019 © Copyright Virtual University of
Pakistan
UNIX/Linux Pipe
Important characteristics of a pipe
Used for communication between
related processes
Used as half-duplex channel
Bounded buffer
Maximum data written is PIPE_BUF
(defined in <sys/param.h> in UNIX
and in <linux/param.h> in Linux)—
5120 and 4096, respectively
14 September 2019 © Copyright Virtual University of
Pakistan
Example
parent child
fork
P P
Read Write
end end
cat Display
pipe grep
Screen