



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
UNIX and Linux interprocess communication tools, Associated system calls, UNIX and Linux standard files, Kernel mechanism for file access, Use of pipe in program. Above mentioned are key points of this lecture handout. Virtual University handout for introduction to operating system are in detail and explanatory.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Operating Systems--[CS-604] Lecture No. 9
Operating Systems Concepts, Chapter 4 UNIX/Linux manual pages for pipe(), fork(), read(), write(), close(), and wait() system calls Lecture 9 on Virtual TV
UNIX/Linux interprocess communication (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
The UNIX and Linux operating systems provide many tools for interprocess communication (IPC). The three most commonly used tools are: Pipe: Pipes are used for communication between related processes on a system, as shown in Figure 9.1. The communicating processes are typically related by sibling or parent-child relationship.
Named pipe (FIFO) : FIFOs (also known as named pipes) are used for communication between related or unrelated processes on a UNIX/Linux system, as shown in Figure 9.2.
Figure 9.1 Pipes on a UNIX/Linux system
Pipe
Figure 9.1 Pipes on a UNIX/Linux system
BSD Socket : The BSD sockets are used for communication between related or unrelated processes on the same system or unrelated processes on different systems, as shown in Figure 9.3.
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: Non-existent file Operation specified is not allowed due to file permissions
FIFOs on a UNIX/Linux system
Computer 1 Computer 2
Network Socket Connection Socket
Figure 9.3 Sockets used for IPC between processes on different UNIX/Linux systems
Figure 9.2 Pipes on a UNIX/Linux system
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: Standard input: 0 (keyboard) Standard output: 1 (display screen) Standard error: 2 (display screen)
We discussed the pipe() system call in the notes for lecture 8. The pipe() system call fails for many reasons, including the following: At least two slots are not empty in the PPFDT—too many files or pipes are open in the process Buffer space not available in the kernel File table is full
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.
/* 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");
P P
fork
parent child
Write end
Read end
Write to screen
Figure 9.5 IPC between parent and child processes with a UNIX/Linux 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 9.6 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
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.
cmd1 pipe cmd2 pipe ... pipe cmdN