Interprocess Communication
Why provide an environment for cooperating processes?
Why provide an environment for cooperating processes?
– Information sharing
* Cooperating processes can share information (such as access to the same files) among multiple processes, but a mechanism is required for parallel access
– Modularity
* Involves dividing complicated tasks into smaller subtasks, which can be completed by different cooperating processes
– Computation speedup
* Subtasks of a single task can be performed in parallel using cooperating processes
– Convenience
* Different tasks completed/managed by cooperating processes
Methods of Cooperation
We will focus on message passing in this class
Message Passing
Synchronizing Messages
Buffering
– Zero capacity
* No messages may be queued within the link
– Requires sender to wait until receiver retrieves message
– Bounded capacity
* Link has finite number of message buffers
– If no buffers are available, then sender must wait if the link is full
– Unbounded capacity
* Link has unlimited buffer space, so sender never needs to wait
IPC Methods
We will explore the following IPC methods
– Signaling
* As a limited form of IPC, a signal is essentially an asynchronous notification sent to a process in order to notify it of an event that occurred
– Files
* A file is a durable block of arbitrary information, or resource for storing information
– Pipes
* A pipe is a synchronized, interprocess byte stream
– Sockets
* Sockets provide point-to-point, two-way communication between two processes, even processes on different systems
What Are Signals?
A signal is a software interrupt
– Notification of an event
* A way to communicate information to a process about the state of other processes, the OS, and hardware so that the process can take appropriate action
– Can change the flow of the program
* When a signal is delivered to a process, process will stop what it’s doing – and either handle or ignore signal
– Signals can be delivered in an unpredictable manner
* Originate outside of currently executing process
* Asynchronous events due to external trigger at the hardware or OS level – causes a context switch!
Every signal has a name that starts with SIG, a value, a default action, and a description
– See man 7 signal
Default Action of Signals
Each signal has a default action
– Term the process will terminate
– Core the process will terminate and produce a core dump file that traces the process state at the time of termination
– Ign the process will ignore the signal
– Stop the process will stop, like Ctrl-Z
– Cont the process will continue from being stopped
Flow of Signals
Signal Events
In the context of terminal signaling, programs can stop, start, and terminate
– Ctrl-C is the same as sending SIGINT (2) signal
* Default handler exits process
– Ctrl-Z is the same as sending a SIGSTOP (20) signal
* Default handler suspends process
– Ctrl-\ is the same as sending a SIGQUIT (3) signal
* Default handler exits process
– Typing fg or bg at the terminal is the same as sending a SIGCONT (18) signal to bring or send a process to the foreground or background, respectively
Signal Handling
Each signal type has a default handler
– Most default handlers exit the process
Installing a Signal Handler
sighandler_t signal(int iSig, sighandler_t pfHandler);
– Installs pfHandler as the handler of signals for type iSig
– pfHandler is a function pointer
typedef void (* sighandler_t) (int);
– Returns the old handler on success; SIGERR on error
– After call, pfHandler is invoked whenever process receives a signal of type iSig
Predefined Signal Handlers
Sending Signals via Command
Sending Signals via System Call
pause() Function
File Management
Standard Linux Files
File descriptor is unique, non-negative integer used to identify open files on a per-process basis
File & Directory Permissions
examples:
-rwx——
drwxr-xr-w
-rw——-
Reminder: Basic Commands
cat
echo
type
more
less
head
tail
diff
Overview of Files
Linux Files
/dev/ is the part in the Unix directory tree that contains all “device” files
I/O and Data Movement
Input and output share common property of unidirectional movement of data and support to sequential access to data
– The flow of data into a program (input) may come from different devices such as keyboard, mouse, memory, disk, network, or another program
– The flow of data out of a program (output) may go to the screen, printer, memory, disk, network, another program