Title: Inter Process Communication
1Inter Process Communication
2Signals
- kill -l
- Processes can choose to ignore most of the
signals. - except SIGSTOP, SIGKILL
- Processes handle signals themselves or allow
kernel to - handle (do default actions).
- no inherent relative priorities.
- no mechanism for handling multiple signals of the
same - kind.
3Linux implements signals using information stored
in the task_struct for the process. the number
of supported signals is limited to the word size
of the processor. (32/64 bits) signal (the
currently pending signals) blocked (a mask of
blocked signals) If a blocked signal is
generated, it remains pending until it is
unblocked.
4Linux holds information about how each process
handles every possible signals an array of
sigaction pointed at by the task_struct for
every Process. the address of routine / let
kernel handle it / ignore Send signals to other
processes kernel / superuser the same uid or
euid / in the same process group //
uid and gid
5Signals are not presented to the process
immediately when they are generated. They must
wait until the process is running again. A
process has specified its own signal
handler. Kernel must call the signal handle
routine (the sigaction holds the address). The
program counter is set to the signal handling
routine and the parameters to the routine are
added to the call frame or registers.
6Pipes
ls pr lpr Use two file which both point at
the same temporary VFS inode which points at the
physical page within memory. file f_inode f_op
7(No Transcript)
8synchronize access to the pipe lock, waiting
queue, signals write to the pipe uses the
standard write library functions enough room to
write and not locked locks it and copies not
enough room or locked sleep, the pipe inodes
waiting queue,interruptible When the data has
been written, any waiting reader sleeping on the
waiting queue will be woken up. Non-blocking
read no data to read or locked an error will
be returned
9Named Pipes (FIFO)
Not temporary objects mkfifo ltpathgt prw-rr 0
fifo1 Processes are free to use a FIFO so long
as they have appropriate access right to it. A
FIFO already exists. open / close Linux must
handle readers opening the FIFO to read
before writers opening to write. Pipe / FIFO the
same data structure and operations
10System V IPC Mechanism
- message queues, semaphore, share memory
- key_t ftok (const char path, int id)
- IPC_PRVIATE
- int sys_msgget (key_t key, int msgflg)
- struct ipc_perm
- key_t key
- ushort uid, gid, cuid, guid
- ushort mode // access right
- ushort seq // id
-
11Message Queues
One or more processes to write/read msgque
vector struct msgid_ds struct ipc_perm
msg_perm struct msg msg_first, msg_last
time_t msg_stime, msg_rtime, msg_ctime //
send, receive, change struct wait_queue
wwait, rwait ushort msg_cbytes ushort
msg_qnum ushort msg_qbytes ushort
msg_lspid, msg_lrpid // send, receive
12struct msg struct msg msg_next long
msg_type char msg_spot time_t msg_stime
// no use short msg_ts // length
13(No Transcript)
14 Write to the queue euid, egid compare with the
mode in the queues ipc_perm tagged with an
application specific type no room, the number /
the length added to the message queues write
waiting queue Read from the queue check
access rights the first message / select
messages with particular types no messages
match, read waiting queue
15Semapores
- A location in memory whose value can be tested
and set by more than one process. - Test / set uninterruptible (used to implement
critical regions), the addition of the current
value of the semaphore and the set value
(positive/negative).
16- Example read/write a file
- initial value 1
- two operations test and decrease / test and
increase - the first process decrease, succeed, 0
- another process decrease, fail, waiting queue
- the first process has finished increase, wake
up the process in the waiting queue
17- semary vector
- struct semid_ds
- struct ipe_perm sem_perm // access right
- time_t sem_otime, sem_ctime // the last time
of operation, change - struct sem sem_base // array of semaphores
- struct sem_queue sem_pending,
sem_pending_last - struct sem_undo undo
- unshort sem_nsems // the number of semaphores
-
- struct sem
- short semval
- short sempid
-
18semary
semid_ds
sem_nsems
sem_base
sem_queue
sem_pending
sem_undo
undo
19- If a process does not require non-blocking, will
add a sem_queue to semaphore waiting queue. - remove sem_queue, wake up
- Deadlocks
- process crashed or was killed in a critical
region. - struct sem_undo
- struct sem_undo proc_next
- struct sem_undo id_next
- int semid
- short semarj
-
20- Linux will maintain at most one sem_undo per
process for each semaphore array. - example 2 -gt semaphore value, -2 -gt sem_undo
- Processes are deleted. Linux apply sem_undo.
- Semaphore set is deleted. The sem_undo are left
on processs task_struct but the semaphore array
identifier is made invalid.
21Shared Memory
- Processes use memory that appears in all of their
virtual memory. - No check how the processes are using it.
- shm_segs vector
- struct shimd_ds
- struct ipc_perm shm_perm
- int shm_segsz // size
- time_t shm_atime, shm_dtime, shm_ctime
- unsigned short shm_cpid, shm_lpid
- short shm_nattach
- unsigned short shm_npages
- unsigned long shm_pages
- struct vm_area_struct attaches
-
22shm_seg
128
shmid_ds
numpages
vm_area_struct
shm_pages
attaches
23- Each process that wishes to share the memory must
attach to that virtual memory via a system call. - create a new vm_area_struct
- choose its virtual address / Linux choose
- vm_next_shared, vm_prev_shared link them together
- The first time that a process accesses one page
of the shared virtual memory, a page fault will
occur. - Allocate a physical page.
24- no use, detach
- Remove its vm_area_struct
- processs page tables are updated to invalidate
the area of the memory that it used to share. - The last process
- the physical memory are freed
25Kernel Synchronize
- struct wait_queue
- stuct task_struct task
- struct wait_queue next
-
- static inline void add_wait_queue (struct
wait_queue - p, struct wait_queue wait)
- unsigned long flags
- save_flags (flags) // register flag
- cli ()
- __add__wait__queue (p, wait)
- restore_flags (flags)
26- static inline void remove_wait_queue (struct
wait_queue - p, struct wait_queue wait)
- unsigned long flags
- save_flags (flags)
- cli ()
- __remove__wait__queue (p, wait)
- restore_flags (flags)
-
- struct semaphore
- int count
- int waiting
- struct wait_queue wait
-
27- __pseudo__ void down (struct semaphore psem)
- while (-- psem -gt count lt 0)
- psem -gt waiting
- if (psem -gt count psem -gt waiting lt0)
- do
- sleep_uniteruptible (psem -gt wait)
- while (psem -gt count)
- psem -gt count psem -gt waiting
- psem -gt waiting 0
-
28- __pseudo__ void up (struct semaphore)
- if ( psem -gt count lt 0)
- psem -gt count psem -gt waiting
- psem -gt waiting 0
- wake_up (psem -gt wait)
-
-
29Others