Inter Process Communication Tools - Introduction to Operating System - Lecture Notes, Study notes of Operating Systems

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

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
49
Operating Systems--[CS-604] Lecture No. 9
Operating Systems
Lecture No. 9
Reading Material
Operating Systems Concepts, Chapter 4
UNIX/Linux manual pages for pipe(), fork(), read(), write(),
close(), and wait() system calls
Lecture 9 on Virtual TV
Summary
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
Unix/Linux IPC Tools
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.
P1 P2
Figure 9.1 Pipes on a UNIX/Linux system
Pi
p
e
Figure 9.1 Pipes on a UNIX/Linux system
pf3
pf4
pf5

Partial preview of the text

Download Inter Process Communication Tools - Introduction to Operating System - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems--[CS-604] Lecture No. 9

Operating Systems

Lecture No. 9

Reading Material

ƒ Operating Systems Concepts, Chapter 4 ƒ UNIX/Linux manual pages for pipe(), fork(), read(), write(), close(), and wait() system calls ƒ Lecture 9 on Virtual TV

Summary

ƒ 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

Unix/Linux IPC Tools

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.

P1 P

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

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

P P

FIFOs on a UNIX/Linux system

FIFO

Computer 1 Computer 2

P1 P

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

Standard Descriptors in Unix/Linux

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)

The pipe() System Call

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

Sample Code for IPC with a UNIX/Linux Pipe

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

Command Line Use of UNIX/Linux Pipes

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