CMP325 Operating Systems Lecture 06, 07 I/O Redirection & IPC
CMP325 Operating Systems Lecture 06, 07 I/O Redirection & IPC
Operating Systems
Lecture 06, 07
I/O Redirection & IPC
Spring 2020
Abdul Mateen (PUCIT)
Today’s Agenda
• Review of previous lecture
• File Management in Linux
• IO Redirection
• Cooperating Processes
• Taxonomy of Inter Process Communication
• Persistence of IPC Objects
• Use of Pipes on the Shell
• Use of FIFOS on the Shell
• Use of Signals on the Shell
2
Connection of an Opened File
3
File Management in Linux
The four key system calls for performing file I/O (programming
languages and software packages typically employ these calls indirectly
via I/O libraries):
Open Read Write Close
The flags argument also specifies whether the file is to be opened for
reading, writing, or both.
• status = close(fd) is called after all I/O has been completed, in order
to release the file descriptor fd and its associated kernel resources
5
File Descriptor to File Contents
System Wide File Table Inode Table
File Status Inode Type Pmns Owner Lock ....
offset flags pointer s
0
PPFDT
13
Fd File ptr 12
flags
0 54
1
233
2
3
4 75
5
93
int main(){
char buff[256];
while(1){
int n = read(0, buff, 255);
write(1, buff, n);
}
return 0;
}
9
Example – File Handling
int main(){
char buff[2000];
int fd = open ("/etc/passwd", O_RDONLY);
int n;
for(;;){
n = read(fd, buff, 1000);
if (n <= 0){
close(fd);
exit(-n);
}
write(1, buff, n);
}
return 0;
}
10
Example – File Handling
int main(){
int n;
char buff[1024];
int fd=open("file.txt",O_CREAT|O_TRUNC|O_RDWR,
0666);
for(;;){
n = read(0, buff, 1023);
if (n <= 0){
printf("Error in reading kb.\n");
exit(-n);
}
write(fd, buff, n);
}
close(fd);
return 0;
} 11
I/O Redirection
12
Stdin and Stdout for Commands
Example 1:
$ cat
PPFDT
This is GR8 Fd flags File ptr
This is GR8
0 stdin
<CTRL + D> 1 stdout
$ 2 stderr
Example 2: 3
4
$ sort
5
rauf
arif OPENMAX-1
kamal
<CTRL+D>
arif
kamal
rauf 13
Redirecting Input of a Command (0<)
• By default, cat and sort commands takes their input
form the standard input, i.e. key board. We can
detach the key board from stdin and attach some
file to it; i.e. cat command will now read input from
this file and not from the key board
# cat 0< f1.txt
PPFDT
# sort 0< f1.txt Fd flags File ptr
0 f1.txt
1 stdout
2 stderr
3
OPENMAX-1 14
Redirecting Output of a Command (1>)
Similarly, by default cat and sort commands sends
their outputs to user terminal. We can detach the
display screen from stdout and attach a file to it; i.e.
cat command will now write its output to this file and
not to the display screen
# cat 1> f1.txt
PPFDT
Fd flags File ptr
0 stdin
1 f1.txt
2 stderr
3
OPENMAX-1 15
Redirecting Error of a Command (2>)
Similarly, by default all commands send their error
messages on stderr, which is also connected to the
VDU. We can detach the VDU from the error stream
and can connect it to a file instead. This is called
error redirection
# cat nofile.txt 2> errors.txt
PPFDT
Fd flags File ptr
0 stdin
1 stdout
2 errors.txt
3
nofile.txt
4
OPENMAX-1 16
Redirecting Input, Output and Error
$ cat 0< f1.txt 1> f2.txt 2> f3.txt
PPFDT
Fd flags File ptr
0 f1.txt
1
f2.txt
2
f3.txt
3
OPENMAX-1
17
Duplicating a File Descriptor
$ cat 0< f1.txt 1> f2.txt 2>&1
PPFDT
Fd flags File ptr
0 f1.txt
1
f2.txt
2
3
OPENMAX-1
18
Draw PPFDT of following Commands
• Differentiate between following two commands (if file2 do not exist)
$ cat < file1 > file2
$ cp file1 file2
• Differentiate between following two commands (if lab1 do not exist)
$ cat lab1.txt 1> output.txt 2> error.txt
$ cat 0< lab1.txt 1> output.txt 2> error.txt
• Differentiate between following two commands
$ find /etc/ -name passwd 2> f1 1>&2
$ find /etc/ -name passwd 2> f1 2>&1
• Explain behavior of following commands
$ cat 1> output.txt 0< input.txt 2> error.txt
$ cat 2> error.txt 1> output.txt 0< input.txt
$ cat f1.txt 2>&1 1> f2.txt
$ cat 0< f1.txt 1> f2.txt 2>&1
$ cat 2>&1 1> f2.txt 0< f1.txt
$ cat 1> f1.txt 2>&1 0< f1.txt
19
Taxonomy of
Inter Process Communication
20
Cooperating Processes
• Independent process is a process that cannot
affect or cannot be affected by the execution of
another process. A process that does not share
data with another process is independent
• Cooperating process is a process that can affect
or can be affected by the execution of another
process. A process that share data with other
process is a cooperating process
• Advantages of Cooperating processes:
– Information sharing
– Computation speed up
– Modularity
– Convenience
21
Application Design
• Option 1: One huge monolithic program that does
every thing
• Option 2: Multi_threaded programs
• Option 3: Multiple programs using fork() that
communicate with each other using some form of
Inter Process Communication (IPC)
22
Ways to Share Information among Processes
Shared
Process Process Process Process Process Process
memory
User area
Kernel area
23
Use of Pipes on the Shell
24
UNIX IPC Tool: Pipes
§ Pipes are used for communication between
related processes (parent-child-sibling) on
the same UNIX system.
P1 P2
Pipe
UNIX/Linux System
25
UNIX IPC Tool: Pipes
History of Pipes: Pipes history goes back to 3rd edition of
UNIX in 1973. They have no name and can therefore be
used only between related processes. This was corrected
in 1982 with the addition of FIFOs
26
UNIX IPC Tool: Pipes – Byte Stream
In pipe, each read operation may read an arbitrary number
of bytes regardless of the size of bytes written by the
writer.
27
UNIX IPC Tool: Pipes
• The UNIX system allows stdout of a command to
be connected to stdin of another command using
the pipe operator |.
29
UNIX IPC Tool: Pipes
• Example Write a command that displays the
contents of /etc/ directory, one page at a time
Try drawing the above commands pictorially and also draw their respective PPFDTs for better understanding 33
Use of Named Pipes on the Shell
34
UNIX IPC Tool: FIFO
§ Named pipes (FIFO) are used for
communication between related or unrelated
processes on the same UNIX system.
P1 P2 p1 p2
FIFO
UNIX/Linux System
35
UNIX IPC Tool: FIFO
Pipes have no names, and their biggest disadvantage is
that they can be only used between processes that have a
parent process in common (ignoring descriptor passing)
36
UNIX IPC Tool: FIFO
A FIFO is created by one process and can be opened by
multiple processes for reading or writing. When processes
are reading or writing data via FIFO, kernel passes all
data internally without writing it to the file system. Thus
a FIFO file has no contents on the file system; the file
system entry merely serves as a reference point so that
processes can access the pipe using a name in the file
system
37
UNIX IPC Tool: FIFO
Use of Signals on the Shell
39
Introduction to Signals
Suppose a program is running in a while(1) loop and you press
Ctrl+C key. The program dies. How does this happens?
40
Introduction to Signals
● Signal is a software interrupt delivered to a process by OS
because:
41
Introduction to Signals
Whenever a process receives a signal, it is interrupted from
whatever it is doing and forced to execute a piece of code called
signal handler. When the signal handler function returns, the
process continues execution as if this interruption has never
occurred
42
Synchronous and Asynchronous Signals
Signals may be generated synchronously or asynchronously:
43
Asynchronous Signals
Asynchronous signals are generated by the events outside
the control of the process that receives them. These
signals arrive at unpredictable times during execution.
Examples include:
44
Signal Delivery and Handler Execution
45
Signal Numbers and Strings
Every signal has a symbolic name and an integer value
associated with it, defined in /usr/include/asm-generic/
signal.h
$ kill -l
● Linux supports 32 real time signals from SIGRTMIN (32)
to SIGRTMAX (63).
Unlike standard signals, real time signals have no
predefined meanings, are used for application defined
purposes. The default action for an un-handled real time
signal is to terminate the receiving process. See also $ man
7 signal
46
Sending Signals to Processes
A signal can be issued in one of the following ways:
Ø Using keyboard:
§ <Ctrl+c> gives SIGINT(2)
§ <Ctrl+\> gives SIGQUIT(3)
§ <Ctrl+z> gives SIGTSTP(20)
47
Sending Signals to Processes
If no signal name or number is specified then default is
to send SIGTERM(15) to the process
48
Signal Actions
Upon delivery of a signal, a process carries out one of the
following default actions, depending on the signal: [$man 7
signal]
49
Signal Actions
3. A core dump file is generated, and the process is
terminated. A core dump file contains an image of the
virtual memory of the process, which can be loaded into a
debugger in order to inspect the state of the process at
the time that it terminated
50
Signal Dispositions
Each signal has a current disposition which determines how
the process behave when the OS delivers it the signal
● If you install no signal handler, the run time environment
sets up a set of default signal handlers for your program.
Different default actions for signals are:
51
Signal Dispositions
TERM
Abnormal termina<on of the program with _exit( ) i.e., no clean up. However,
status is made available to wait() & waitpid() which indicates abnormal
termina<on by the specified signal
CORE
Abnormal termina<on with addi<onal implementa<on dependent ac<ons, such
as crea<on of core file may occur
STOP
Suspend/stop the execu<on of the process
CONT
Default ac<on is to con<nue the process if it is currently stopped
52
Instructor:Arif Butt
Important Signals
Important Signals (Default
(Default Behavior:
Behavior: Term) Term)
SIGHUP(1) Informs the process when the user who run the process logs out. When a
terminal disconnect (hangup) occurs, this signal is sent to the controlling
process of the terminal. A second use of SIGHUP is with daemons. Many
daemons are designed to respond to the receipt of SIGHUP by
reinitializing themselves and rereading their configuration files.
SIGINT(2) When the user types the terminal interrupt character (usually <Control+C>,
the terminal driver sends this signal to the foreground process group. The
default action for this signal is to terminate the process.
SIGKILL(9) This is the sure kill signal. It can’t be blocked, ignored, or caught by a
handler, and thus always terminates a process.
SIGPIPE(13) This signal is generated when a process tries to write to a pipe, a FIFO, or a
socket for which there is no corresponding reader process. This normally
occurs because the reading process has closed its file descriptor for the IPC
channel
SIGALRM(14) The kernel generates this signal upon the expiration of a real-time timer set
by a call to alarm() or setitimer()
SIGTERM(15) Used for terminating a process and is the default signal sent by the kill
command. Users sometimes explicitly send the SIGKILL signal to a
process, however, this is generally a mistake. A well-designed application
will have a handler for SIGTERM that causes the application to exit
gracefully, cleaning up temporary files and releasing other resources
Punjab University College Of Information And Technology(PUCIT) 2153
beforehand. Killing a process with SIGKILL bypasses SIGTERM handler.
Important Signals
Important
signal isSignals
SIGQUIT(3)
When the user types the quit character (Control+\) on the keyboard, this
(Default
sent to the foreground Behavior:
process group. Core)
Using SIGQUIT in this
manner is useful with a program that is stuck in an infinite loop or is
otherwise not responding. By typing Control-\ and then loading the
resulting core dump with the gdb debugger and using the backtrace
command to obtain a stack trace, we can find out which part of the program
code was executing
SIGILL(4) This signal is sent to a process if it tries to execute an illegal (i.e.,
incorrectly formed) machine-language instruction module
SIGFPE(9) Generate by floating point Arithmetic Exception
SIGSEGV(11) Generated when a program makes an invalid memory reference. A
memory reference may be invalid because the referenced page
doesn’t exist (e.g., it lies in an unmapped area somewhere between
the heap and the stack), the process tried to update a location in read-
only memory (e.g., the program text segment or a region of mapped
memory marked read-only), or the process tried to access a part of
kernel memory while running in user mode. In C, these events often
result from dereferencing a pointer containing a bad address. The
name of this signal derives from the term segmentation violation
54
Important Signals (cont...)
Important
Default Behavior: Stop Signals
SIGSTOP(19) This is the sure stop signal. It can’t be blocked, ignored, or
caught by a handler; thus, it always stops a process
SIGTSTP(20) This is the job-control stop signal, sent to stop the foreground
process group when the user types the suspend character
(usually <Control+Z>) on the keyboard.. The name of this
signal derives from “terminal stop”
56
Job Control States
57
Foreground and Background Processes
• On a system, there is always one foreground process
that is holding the terminal (VDU and keyboard).
Example is vim or may be a less program
• On a system, there can be many processes which are
running in the background. They usually neither need
user input nor requires to give output on the monitor.
Example is an audio player playing an audio song, or a
find program searching a file from a huge file system.
Such programs run in the background so that the
terminal is available to the user for execution of other
commands
• On GUI a user can simply minimize an application and
run other applications, while on a CLI we need to make
use of commands to move processes in these two
states 58
Job Control States
Running in
foreground
1. <Ctrl+c> (SIGINT)
Command 2. <Ctrl+\> (SIGQUIT) Terminated
fg (SIGCONT)
kill
bg (SIGCONT)
Command & Suspended Stopped in
Running in
kill(SIGSTOP) background
background
59