0% found this document useful (0 votes)
9 views30 pages

Lec9 CS604

Uploaded by

kamran ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views30 pages

Lec9 CS604

Uploaded by

kamran ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 30

Operating

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
May 28, 2024 © Copyright Virtual University of P
akistan
Review of Lecture 8
 Interprocess communication (IPC)
 Establishlink 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
May 28, 2024 © Copyright Virtual University of P
akistan
Review of Lecture 8
 Process synchronization
 Buffering capacity of a link

 IPC in UNIX and Linux

 Pipe, FIFO, Socket, etc.

 Per process file descriptor table

 pipe, read, write, close


system calls
May 28, 2024 © Copyright Virtual University of P
akistan
UNIX/Linux IPC Tools
 Pipe: For communication between
related processes on a system

P1 P2

Pipe

May 28, 2024


UNIX/Linux System
© Copyright Virtual University of P
akistan
UNIX/Linux IPC Tools
 Named pipe (FIFO): For
communication between unrelated
processes on a system
P1 P2

FIFO

May 28, 2024 © Copyright Virtual University of P


UNIX/Linux System
akistan
UNIX/Linux IPC Tools
 Socket: For communication
between unrelated processes on
the same or different systems
P1 P2

Network
Socket Connection Socket

Computer
May 28, 2024 1 © Copyright Virtual University of P Computer 2
akistan
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
May 28, 2024 © Copyright Virtual University of P
akistan
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.
May 28, 2024 © Copyright Virtual University of P
akistan
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,
etc.
May 28, 2024 © Copyright Virtual University of P
akistan
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
May 28, 2024 © Copyright Virtual University of P
akistan
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
May 28, 2024 © Copyright Virtual University of P
akistan
read System Call
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);

 Returns number of bytes read or -1


 Call fails and errno set accordingly
 Invalid ‘fildes’, ‘buf’, or ‘nbyte’
 Signal caught during read
May 28, 2024 © Copyright Virtual University of P
akistan
write System Call
#include <sys/types.h>
#include <unistd.h>
ssize_t write (int fildes, const void *buf,
size_t nbyte);
 Returns the number of bytes written
or -1

May 28, 2024 © Copyright Virtual University of P


akistan
write System Call
 Call fails
 Invalid argument
 File size limit for process or for
system would exceed
 Disk is full

May 28, 2024 © Copyright Virtual University of P


akistan
File Descriptor to File
Contents
Per Process
File File Descriptor File Inode
Descriptor Table Table Table
0
1
2 File’s
3 contents

4 …

… …

OPEN_MAX — 1
May 28, 2024 © Copyright Virtual University of P
akistan
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.
May 28, 2024 © Copyright Virtual University of P
akistan
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)
May 28, 2024 © Copyright Virtual University of P
akistan
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
May 28, 2024 © Copyright Virtual University of P
akistan
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
May 28, 2024 © Copyright Virtual University of P
akistan
Example

parent child
fork
P P

Read Write
end end

May 28, 2024 © Copyright Virtual University of P


akistan
Sample Code
/* Parent creates pipe, forks a child, child writes into
pipe, and parent reads from pipe */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
int pipefd[2], pid, n, rc, nr, status;
char *testString
May 28, 2024
akistan = "Hello, world!\n“, buf[1024];
© Copyright Virtual University of P
Sample Code
rc = pipe (pipefd);
if (rc < 0) {
perror("pipe");
exit(1);
}
pid = fork ();
if (pid < 0) {
perror("fork");
exit(1);
}
May 28, 2024 © Copyright Virtual University of P
akistan
Sample Code
if (pid == 0) { /* Child’s Code */
close(pipefd[0]);
write(pipefd[1], testString, strlen(testString));
close(pipefd[1]);
exit(0);
}

May 28, 2024 © Copyright Virtual University of P


akistan
Sample Code
/* Parent’s Code */
close(pipefd[1]);
n = strlen(testString);
nr = read(pipefd[0], buf, n);
rc = write(1, buf, nr);
wait(&status);
printf("Good work child!\n");
return(0);
May 28, 2024 © Copyright Virtual University of P
} akistan
Command Line Use of
Pipes
 Connect standard output of a
command to standard input of
another
 Use the pipe operator, |
 Syntax:
cmd1 | cmd2 | … | cmdN
May 28, 2024 © Copyright Virtual University of P
akistan
Command Line Use of
Pipes
cmd1 | cmd2 | … | cmdN

cmd1 pipe cmd2 pipe pipe cmdN

May 28, 2024 © Copyright Virtual University of P


akistan
Command Line Use of
Pipes

cat /etc/passwd | grep zaheer

cat Display
pipe grep
Screen

May 28, 2024 © Copyright Virtual University of P


akistan
Without Using a Pipe

cat /etc/passwd | grep zaheer

$ cat /etc/passwd > temp1


$ grep “zaheer” temp1
$ rm temp1
May 28, 2024 © Copyright Virtual University of P
akistan
Recap of Lecture
 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
May 28, 2024 © Copyright Virtual University of P
akistan

You might also like