cop 4604 unix system programmingroy/cop4604.04f/notes/ipc.pdf · n most widely known unix ipc...
TRANSCRIPT
COP 4604 UNIX System Programming
IPCDr. Sam Hsu
Computer Science & EngineeringFlorida Atlantic University
Interprocess Communicationn Interprocess communication (IPC) provides
two major functions/services:n Information exchange
n For data transfer among processes.
n Process synchronizationn For synchronization of process execution.
Various IPC Implementations
•••
SocketsStreams
•••
Message queuesSemaphoresShared memory
••
••
Stream pipes (FDX)Named stream pipes
••
••
••
Pipes (HDX)FIFOs (named pipes)
•••Signals
4.4BSDSVR4POSIX.1IPC Type
Signalsn Most widely known UNIX IPC facility.n Used primarily to inform processes the
occurrence of asynchronous events.n Posted by one process and received by
another or the same process.n The receiving process performs an action
appropriate to the signal received:n Default (SIG_DFL).n Ignored (SIG_IGN).n User-defined.
Pipesn A pipe is a one-way communication channel (HDX)
that couples one process to another.n Used only between processes that have a common
ancestor.n More specifically, for communication between parent
and child in general.n There are two types of pipes: named and unnamed
n Unnamed pipes are used for communication between related processes.
n Named pipes can be used for communication between unrelated processes.
The pipe() System Call
n Syntaxpipe()
n Is used to create an unnamed pipe.n Needs an array of two int's for two fd's
(r/w)
Pipes Are Files
n In fact, a pipe is another generalization of the UNIX file concept.
n A pipe is a FIFO file.lseek() does not work on a pipe.
n Unnamed pipes come and go.n Named pipes (also known as FIFOs) are
permanent files.
Some Properties of Pipes (1/2)
n Write before readn If a read() is issued while the pipe is
empty, it will block.
n The size of a pipe is limited.n Max is PIPE_BUF.n If a write() is issued when the pipe is full, it
will block.
Some Properties of Pipes (2/2)n Data could be intermingled if several
processes are writing to the same pipe.n If all writers of a pipe are closed, a
reader will encounter EOF.n If all readers of a pipe are closed, a
writer will face a broken pipe.n Non-blocking reads and writes
n Issue fcntl() with O_NONBLOCK flag.
FIFOs
n Also known as named pipes.n Can be used between unrelated
processes for data exchange.
The popen()/pclose() Functionsn To create a pipe between the calling program
and the command to be executed. n Will handle dirty work such as:
n The creation of a pipe.n The fork of a child.n Closing the unused ends of the pipe.n Executing a shell to execute the command.n Waiting for the command to terminate.
Three SV IPC Facilities
n SV provides 3 unique IPC facilities (structures). n Semaphores
n For process synchronization.
n Shared memoryn For process communication.
n Message queuesn For process communication.
Semaphoresn Semaphores are useful for process
synchronization and resource management. n A semaphore is an unsigned integer shared
among several competing processes.n Operating upon a semaphore involves adding or
subtracting from the underlying semaphore value.
n The UNIX implementation of semaphores is modeled after Dijkstra's semaphores.
Shared Memory
n Shared memory allows one or more process to share the same segment of memory.n Shared memory may be attached to more
than one process.n Once attached, it is part of a process's data
space.
n Shared memory is the fastest IPC.
Messages Queuesn A message queue allows processes to
send and receive discrete amounts of data, know as messages.n A message is a collection of bytes of varying
lengths, ranging from zero to a system imposed maximum.n The message content can be anything -- ASCII or
binary.
n Each message has a type associated with it.n A message type is a long integer.
Common Features of the Three IPC Facilities (1/3)
n An IPC facility must be created prior to use.n It can be created anytime before use.
n Permission and ownership for a facility are established at the time of creation.n But these attributes can be changed later.
n An IPC facility can exist with contents intact after all processes accessing it have exited.n Changes made to an IPC facility by a process persist
after the process exits.
n An IPC facility must be explicitly removed.n Removal of an IPC facility can be done by either the
owner or creator.
Common Features of the Three IPC Facilities (2/3)
n Shares a common data structure defined in <sys/ipc.h>
struct ipc_perm {uid_t uid; /* owner's user id */gid_t gid; /* owner's group id */uid_t cuid; /* creator's user id */gid_t cgid; /* creator's group id */mode_t mode; /* access modes */uint_t seq; /* slot usage sequence number */key_t key; /* key */
}
Common Features of the Three IPC Facilities (3/3)
n Needs an IPC key to gain access to an IPC facility.n All processes wishing to use the same IPC facility must
specify the same key.n This key must uniquely identify the IPC facility.
n An IPC facility is identified by a nonnegative integer.n In need of an IPC key to generate such a numeric ID.
n Three ways to generate an IPC key:n IPC_PRIVATEn Use a predefined key and store it in a common header file.n Use key_t ftok(const char *path, int id) to generate an IPC
key.
Three Major Categories of IPC System Calls
n get(): Is used to create or access an IPC facility. It returns an IPC identifier.n semget(), shmget(), msgget()
n ctl(): Is used to determine status, set options and/or permissions, or remove an IPC facility.n semctl(), shmctl(), msgctl()
n op(): Is used to operate on an IPC identifier.n semop()n shmat(), shmdt()n msgsnd(), msgrcv()
End-user Level Commands
n ipcsn Used to report inter-process
communication facilities status
n ipcrmn Used to remove a message queue,
semaphore set, or shared memory ID.
Semaphore Basics (1/3)
n A semaphore is an integer variable that can be accessed only through two standard atomic operations:n Proberen – waitn P(S): while S <= 0
do skip;S ß S-1;
n Verhogen – signal V(S): S ß S+1;
Semaphore Basics (2/3)
n Semaphores are used to control access to shared resource(s).n An important mechanism to ensure mutual
exclusion to protect critical sections in a program.
n To obtain a shared resource, a process needs to test the semaphore that controls the resource.n If the semaphore value is positive, the process can
use the resource. The process decrements the semaphore value by 1, indicating it has used one unit of the resource.
Semaphore Basics (3/3)
n If the value tested is 0, the process goes to sleep until the semaphore value is greater than 0.
n The controlling semaphore value is incremented by 1 when a process is done with the shared resource. If any other processes are asleep, waiting for the semaphore, they are awakened.
n Semaphores are typically implemented inside the kernel.n To ensure that the test of a semaphore’s value and
the de/incrementing of the value are implemented as atomic operations.
2 Types of Semaphores (1/2)
n Binary (lock/unlock)n Having a value of 0 or 1.n Usually used for locking a resource.
n N -ary (counting) n N is the available number of an intended
resource on a system.n Usually used for maintaining a pool of
resources.
2 Types of Semaphores (2/2)n Note that the type/nature of a semaphore
(whether binary of N-ary) is imposed by the application (user-defined), not by the system.
n In the binary case, a process waits for the semaphore to become one; in the N-ary case, a process waits for a non-zero value. (The latter can be considered as a more general case of the former.)
Semaphore Implementation (1/2)
n A semaphore is a sharable unsigned short integer variable.
n Semaphores are grouped together to form sets.n No standalone semaphores.
n A semaphore set (array) contains:n 1 to SEMMSL semaphores.n 1 data structure, struct semid_ds, containing
administrative information about the set.
Semaphore Implementation (2/2)
n Each semaphore set is uniquely identified by a nonnegative integer.
n One or more semaphores in a set can be accessed at the same time.
n All semaphores involved in an operation are changed "atomically".
n Only one process can access a semaphore set at any given time.
Basic Semaphore Operations
n Incrementing.n Decrementing.n Testing for zero.
Semaphore System Calls (1/2)n semget():
n To create or gain access to a set of one or more semaphores.
n semctl(): n To get or set the value of a semaphore(s) in a set.n To get or modify the status of a semaphore set. n To identify process waiting for a specific
semaphore value.n To identify process that did the last operation on a
semaphore.n To remove a semaphore set.
Semaphore System Calls (2/2)n semop():
n To wait for the value of a semaphore(s) to become 0.
n To increment semaphore value(s).n To decrement semaphore value(s).n IPC_NOWAIT for non-blocking.n SEM_UNDO for undo operations.n Note: sleep() and wakeup() are used internally.
Blocking/nonblocking Operations
n A semaphore operation can be either: n Blocking: Processes wait for resources to
be released.n Non-blocking: Processes return an error
code -1, and the external errno variable is set accordingly. (IPC_NOWAIT is set.)
n UNIX puts the blocked process to sleep to eliminate the busy waiting phenomenon.
Semaphore system calls
semid = semget()
semctl()
union semun {int valstruct semid_ds *buf;unshort *array;
};
semop()
struct sembuf {ushort sem_num;short sem_op;short sem_flg;
};
Semaphore Data Structureskernel data space
struct semid_ds {struct ipc_perm sem_perm;struct sem *sem_baseushort_t sem_nsemstime_t sem_otime; time_t sem_ctime;int sem_binary;
};
struct sem {ushort_t semval;pid_t sempidushort_t semncnt ushort_t semzcnt;
} [nsems];
(retained by kernel)
Note:In union semun
int val is used for SETVALstruct semid_ds *buf is used
for IPC_STAT and IPC_SETushort *array is used for
GETALL and SETALL
In struct sembufshort sem_op has the value:
0: zero test,+n.: release n, or-n: request n
short sem_flg has the value: IPC_NOWAIT or SEM_UNDO
struct sem is the data structure of a semaphore. It comes in an array.
Shared Memory Basics
n Shared memory allows multiple processes to share memory.n Shared memory is used to hold data
needed by two or more processes.n Look-up tables, form layouts, etc.
Shared Memory Implementation
n The implementation of shared memory is machine dependent. n It depends on the memory management of
the host machine.n System configuration values determine the
minimum and maximum sizes of a segment.
n The number of segments that may be attached to a process is also restricted.
Shared Memory Creation & Attachment
n Typically one process creates a shared memory segment, maps the segment into its data space and initializes the segment.n The term for mapping a shared memory segment
into a user's data space is called attachment.n The creator process specifies R/W permissions
associated for the shared memory segment.n Other processes may then attach the shared
memory segment and access its contents.n Unrelated processes can thus communicate via shared
memory.
Shared Memory Detachment
n A process using a shared memory segment may detach it explicitly when finished.n If not done explicitly, an exit() call will
detach the segment automatically.n Detachment does not remove the shared
memory.
Explicit Removal & Synchronization
n Shared memory must be removed explicitly.n Usually by the same process that does the
creation.
n Synchronized access to shared memory must be done explicitly.n Usually used with semaphores.
Shared Memory System Calls (1/2)
n shmget():n To obtain a shared memory identifier.
n shmat():n To attach a shared memory segment to a
process data segment.n shmdt():
n To detach a shared memory segment from a process data segment.
Shared Memory System Calls (2/2)
n shmctl():n To determine the status of a shared
memory segment.n To change permissions or ownership of a
shared memory segment.n To remove a shared memory.
A Conceptual View of Shared Memory
argv[] & envp[]
BSS
data
text
heap
stack
argv[] & envp[]
BSS
data
text
heap
stack
shared memory
Message Queues Basicsn Message queues are for exchange of data
among processes.n The data exchanged are in discrete portions known
as messages.n A message is a sequence of bytes, from zero to the
system limit, with a message type.n The structure of a message is up to the programmer.
n Once a message queue is created, processes can send/receive messages to/from the queue.
Reading Messages
n A process can selectively read messages on a queue:n First message on the queue.n First message of a specific type on the
queue.n First message from a range of types (from
1 to upper bound of range) on the queue.
Some Message Queue Propertiesn Messages queues are similar to unnamed
pipes; however, n Each message has a type associated with it.n Data in a pipe is just a sequence of types with no
header of format.n Unread data in a pipe vanishes when the last
reader process close a file descriptor associated with the pipe's read end.
n A message queue sticks around.n It will remain active until it is explicitly removed.
Message Queues System Calls (1/2)
n msgget():n To create or to gain access to a message
queue.
n msgctl():n To determine the status of a message queue.n To change the permission or ownership of a
message queue.n To change the maximum size.n To remove a message queue.
Message Queues System Calls (2/2)
n msgsend():n To send a message.
n msgrcv():n To receive a message.
Some Comments on the Three SV IPC Facilities
n The SV IPC structures are system-wide.n They stick around until explicitly removed.
n Not compatible with standard UNIX file manipulation facilities.n Can’t use open(), close(), read(), write(), etc. n Almost a dozen of new system calls were added to
the kernel to support these IPC facilities.
n Basically, they are intra-system IPC facilities.