ipc 1 Flashcards
Interprocess Communication
- Processes may be independent or cooperating
– An independent process cannot affect or be affected by the execution of another process - A process that does not share data with another process is independent
– A cooperating process can affect or be affected by the execution of another process - A process that shares data with another process is a cooperating process
- Cooperating processes require interprocess communication (IPC)
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
- Cooperation by sharing
– Use shared data such as memory, variables, files, databases, etc. mutually exclusive
– Critical section used to provide data integrity with mutually exclusive writing to prevent inconsistent data - Cooperation by communication
– Cooperation using messages (message passing)
– May lead to deadlock if each process waiting for a message from the other to perform an operation
– Starvation also possible is process never receives a message
We will focus on message passing in this class
Message Passing
- A mechanism is needed for processes to communicate and synchronize their actions
- In a message system, processes communicate with each other without resorting to shared variables
- IPC facility provides two primitive operations for fixed or variable-sized message passing: send and receive
- If two processes wish to communicate, they need to
– Establish a communication link between them - Logical (e.g., logical properties) or physical (e.g., shared memory, hardware bus)
– Exchange messages via send/receive
Synchronizing Messages
- Message passing may be either blocking or non-blocking
– Blocking is considered to be synchronous - Send sender blocked until message received
- Receive receiver blocks until a message is available
– Non-Blocking is considered to be asynchronous - Send sender resumes operation immediately after sending (i.e., send and continue)
- Receive receiver returns immediately with either a valid or null message
Buffering
- A message queue can be implemented in one of three ways
– 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
- Event gains attention of OS
- OS stops process execution immediately – Sends it a signal
- Signal Handler then executes to completion
- Process execution resumes where it left off
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
- A program can install its own handler for signals of almost any type
– Cannot install its own handler for the following signals - SIGKILL (9)
– Default handler exits the process
– Catchable termination signal is SIGTERM (15) - SIGSTOP (19)
– Default handler suspends the process
– Can resume the process with signal SIGCONT (18)
– Catchable suspension signal is SIGTSTP (20)
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
- Can install predefined signal handler SIG_IGN to ignore signals
void (*pfRet)(int) = signal(SIGINT, SIG_IGN);
– Subsequently, process will ignore SIGINT (2) signals - Can install predefined signal handler SIG_DFL to restore default signal handler
void (*pfRet)(int) = signal(SIGINT, myHandler);
…
pfRet = signal(SIGINT, SIG_DFL);
– Subsequently, process will handle SIGINT (2) signals using the default handler for SIGINT (2) signals
Sending Signals via Command
- kill –signal pid
– Send signal of type signal to process with ID pid
– Specify signal type name (–SIGINT) or number (–2)
– Examples:
kill –2 1234
kill –SIGINT 1234
Same as typing Ctrl-C if process 1234 is running in foreground - killall loop
– Send a signal to all processes named loop to “terminate”
– This will actually send the signal SIGTERM, whose purpose is to communicate a termination request to the given process, which does not necessarily have to terminate
Sending Signals via System Call
- int raise(int iSig);
– Instructs OS to send signal of type iSig to current process
– Returns 0 to indicate success; non-0 to indicate failure
– Example:
int retVal = raise(SIGINT); // process commits suicide
assert(retVal != 0); // should not get here - int kill(pid_t iPid, int iSig);
– Sends iSig signal to process with ID iPid
– Equivalent to raise(iSig) when iPid is ID of current process
– Example:
pid_t iPid = getpid(); // process gets its pid
kill(iPid, SIGINT); // sends itself a SIGINT signal, commits suicide
pause() Function
- int pause( );
– Suspends the calling process, without wasting resources, until some kind of signal is caught
– Signal action can be the execution of a signal handler function or process termination
– Only returns –1 when a signal was caught and the signal-catching function returned
File Management
- A file is a collection of related information, defined by its creator
– E.g., source code, binary files, data, … - With respect to files, the OS is responsible for:
– File creation and deletion
– Directory creation and deletion
– Support of primitives for accessing and manipulating files and directories
– File security
– Mapping files onto secondary storage
– Hiding differences between storage types
Standard Linux Files
- Linux views absolutely everything as files
– There are default files associated with every shell where a command reads its input from and sends its output and error messages to
– These three files known as standard files for the command - stdin standard input default = keyboard
- stdout standard output default = terminal
- stderr standard error default = terminal
File descriptor is unique, non-negative integer used to identify open files on a per-process basis
File & Directory Permissions
- Linux controls the who and what access for files and directories through the use of a permission list
– Sequence of ten characters organized into four parts to the leftmost output of ls –l command - Permission list
The first column describes the type of file d directory
– ordinary file
l symbolic link
s socket
p named pipe
c character device file (e.g., terminal)
b block device file (e.g., hard disk drive)
– Most often, users see either – or d for ordinary files and directories
examples:
-rwx——
drwxr-xr-w
-rw——-
Reminder: Basic Commands
cat
echo
type
more
less
head
tail
diff
- File Viewing Commands
– cat concatenate files and print to standard output (terminal)
– echo print a line of text
– type print a brief description of a command
– more file viewer for paging through text on screen at a time
– less file viewer similar to more, but with more features
– head print the first n lines of a file
– tail print the last n lines of a file
– diff compare files line-by-line
Overview of Files
- Data communication with a C program and the outside world is performed through files
- Files are a non-volatile way to store data by means of such media as tape, CD-ROM, floppy disk, ZIP disk, hard drive, etc.
- C (just like the Linux operating system) considers all process communication media to be files
– An ordinary file on a disk is considered to be a file
– So is the keyboard, the screen, parallel ports, and serial ports
Linux Files
- A Linux file is a sequence of m bytes
– B0, B1, …, Bk, …, Bm-1 - All I/O devices are represented as files
/dev/sda2 (/usr disk partition)
/dev/tty2 (terminal) - Even the kernel is represented as a file
/dev/kmem (kernel memory image)
/proc (kernel data structures)
/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