Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems
The UNIX and Linux operating systems provide many tools for interprocess communication (IPC). The three most commonly used tools are:
The open() System call
The open() system call is used to open or create a file. Its synopsis is as follows:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char pathname, int oflag, /* mode_t mode */);
The call converts a pathname into a file descriptor (a small, non-negative integer for use
in subsequent I/O as with read, write, etc.). When the call is successful, the file
descriptor returned will be the lowest file descriptor not currently open for the process.
This system call can also specify whether read or write will be blocking or non-blocking.
The ‘oflag’ argument specifies the purpose of opening the file and ‘mode’ specifies
permission on the file if it is to be created. ‘oflag’ value is constructed by ORing various
flags: O_RDONLY, O_WRONLY, O_RDWR, O_NDELAY (or O_NONBLOCK),
O_APPEND, O_CREAT, etc.
The open() system call can fail for many reasons, some of which are:
The file descriptor returned by the open() system call is used in the read() and write() calls for file (or pipe) I/O.
We discussed the read() system call in the notes for lecture 8. The call may fail for various reasons, including the following:
The call may fail for various reasons, including the following:
As discussed in the notes for lecture 8, the close() system call is used to close a file descriptor. It takes a file (or pipe) descriptor as an argument and closes the corresponding file (or pipe end).
Figure 9.4 shows the kernel mapping of a file descriptor to the corresponding file. The
system-wide File Table contains entries for all of the open files on the system.
UNIX/Linux allocates an inode to every (unique) file on the system to store most of the
attributes, including file’s location. On a read or write call, kernel traverses this mapping
to reach the corresponding file.
Three files are automatically opened by the kernel 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. By default, standar d files are attached to the terminal on which a process runs. The descriptors for standard files are known as standard file descriptors. Standard files, their descriptors, and their default attachments are:
We discussed the pipe() system call in the notes for lecture 8. The pipe() system call fails for many reasons, including the following:
We discussed in the notes for lecture 8 a simple protocol for communication between a parent and its child process using a pipe. Figure 9.5 shows the protocol. Code is reproduced in Figure 9.6.
Pipes can also be used on the command line to connect the standard input of one process to the standard input of another. This is done by using the pipe operator which is | and the syntax is as follows:
cmd1 | cmd2 | ... | cmdN
The semantics of this command line are shown in Figure 9.7.
Figure 9.7 Semantics of the command line that connects cmd1 through cmdN via pipes.
The following example shows the use of the pipe operator in a shell command.
cat /etc/passwd | grep zaheer
The effect of this command is that grep command displays lines in the /etc/passwd file that contain the string “zaheer”. Figure 9.8 illustrates the semantics of this command.
The work performed by the above command can be performed by the following sequence of commands without using the pipe operator. The first command saves the /etc/passwd file in the temp1 file and the second command displays those lines in temp1 which contain the string “zaheer”. After the temp1 file has been used for the desired work, it is deleted.
$ cat /etc/passwd > temp1
$ grep “zaheer” temp1
$ rm temp1