



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Operating Systems [CS-604] Lecture No. 8
Operating Systems Concepts, Chapter 4 UNIX/Linux manual pages for pipe(), fork(), read(), write(), close(), and wait() system calls
Interprocess communication (IPC) and process synchronization UNIX/Linux IPC tools (pipe, named pipeโFIFO, socket, TLI, message queue, shared memory) Use of UNIC/Linux pipe in a sample program
IPC provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space. We discuss in this section the various message passing techniques and issues related to them.
The function of a message system is to allow processes to communicate without the need to resort to the shared data. Messages sent by a process may be of either fixed or variable size. If processes P and Q want to communicate, a communication link must exist between them and they must send messages to and receive messages from each other through this link. Here are several methods for logically implementing a link and the send and receive options: Direct or indirect communication Symmetric or asymmetric communication Automatic or explicit buffering Send by copy or send by reference Fixed size or variable size messages We now look at the different types of message systems used for IPC.
Direct Communication With direct communication, each process that wants to communicate must explicitly name the recipient or sender of the communication. The send and receive primitives are defined as: Send(P, message) โ send a message to process P Receive(Q, message) โ receive a message from process Q.
A communication link in this scheme has the following properties: A link is established automatically between every pair of processes that want to communicate. The processes need to know only each otherโs identity to communicate A link is associated with exactly two processes. Exactly one link exists between each pair of processes. Unlike this symmetric addressing scheme, a variant of this scheme employs asymmetric addressing, in which the recipient is not required to name the sender. Send(P, message) โ send a message to process P Receive(id, message) โ receive a message from any process; the variable id is set to the name of the process with which communication has taken place.
Indirect Communication With indirect communication, messages can be sent to and received from mailboxes. Here, two processes can communicate only if they share a mailbox. The send and receive primitives are defined as: Send(A, message) โ send a message to mailbox A. Receive(A, message) โ receive a message from mailbox A. A communication link in this scheme has the following properties: A link is established between a pair of processes only if both members have a shared mailbox. A link is associated with more than two processes. A number of different links may exist between each pair of communicating processes, with each link corresponding to one mailbox.
Communication between processes takes place by calls to send and receive primitives (i.e., functions). Message passing may be either blocking or non-blocking also called as synchronous and asynchronous. Blocking send: The sending process is blocked until the receiving process or the mailbox receives the message. Non-blocking send: The sending process sends the message and resumes operation. Blocking receive: The receiver blocks until a message is available. Non-blocking receiver: The receiver receives either a valid message or a null.
Whether the communication is direct or indirect, messages exchanged by the processes reside in a temporary queue. This queue can be implemented in three ways: Zero Capacity: The queue has maximum length zero, thus the link cannot have any messages waiting in it. In this case the sender must block until the message has been received. Bounded Capacity: This queue has finite length n; thus at most n messages can reside in it. If the queue is not full when a new message is sent, the later is placed in the queue and the sender resumes operation. If the queue is full, the sender blocks until space is available.
unlink(2) the file is deleted. close() returns zero on success, or -1 if an error occurred.
A UNIX/Linux pipe can be used for IPC between related processes on a system. Communicating processes typically have sibling or parent-child relationship. At the command line, a pipe can be used to connect the standard output of one process to the standard input of another. Pipes provide a method of one-way communication and for this reason may be called half-duplex pipes. The pipe() system call creates a pipe and returns two file descriptors, one for reading and second for writing, as shown in Figure 8.1. The files associated with these file descriptors are streams and are both opened for reading and writing. Naturally, to use such a channel properly, one needs to form some kind of protocol in which data is sent over the pipe. Also, if we want a two-way communication, we'll need two pipes.
Figure 8.1 A UNIX/Linux pipe with a read end and a write end
The system assures us of one thing: the order in which data is written to the pipe, is the same order as that in which data is read from the pipe. The system also assures that data won't get lost in the middle, unless one of the processes (the sender or the receiver) exits prematurely. The pipe() system call is used to create a read-write pipe that may later be used to communicate with a process we'll fork off. The synopsis of the system call is:
#include <unistd.h> int pipe (int fd[2]);
Each array element stores a file descriptor. fd[0] is the file descriptor for the read end of the pipe (i.e., the descriptor to be used with the read system call), whereas fd[1] is the file descriptor for the write end of the pipe. (i.e., the descriptor to be used with the write system call).The function returns -1 if the call fails. A pipe is a bounded buffer and the maximum data written is PIPE_BUF, defined in <sys/param.h> in UNIX and in <linux/param.h> in Linux as 5120 and 4096, respectively. Lets see an example of a two-process system in which the parent process creates a pipe and forks a child process. The child process writes the โHello, world!โ message to the pipe. The parent process reads this messages and displays it on the monitor screen. Figure 8.2 shows the protocol for this communication and Figure 8.3 shows the corresponding C source code.
Figure 8.2 Use of UNIX/Linux pipe by parent and child for half-duplex communication
/* 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 = "Hello, world!\nโ, buf[1024];
rc = pipe (pipefd); if (rc < 0) { perror("pipe"); exit(1); } pid = fork (); if (pid < 0) { perror("fork"); exit(1); } if (pid == 0) { /* Childโs Code / close(pipefd[0]); write(pipefd[1], testString, strlen(testString)); close(pipefd[1]); exit(0); } / Parentโs Code */ close(pipefd[1]); n = strlen(testString); nr = read(pipefd[0], buf, nA); rc = write(1, buf, nr); wait(&status); printf("Good work child!\n"); return(0); }
Figure 8.3 Sample code showing use of UNIX/Linux pipe for IPC between related processesโchild write the โHello, world!โ message to the parent, who reads its and displays it on the monitor screen