Inter-Process Communication Mechanisms (IPC)
Total Page:16
File Type:pdf, Size:1020Kb
Inter-Process Communication Mechanisms (IPC) Signals Pipes Message Queues Semaphores Shared Memory Signals v Signal: a mechanism to notify process of system events ™ Asynchronous notification ™ Synchronous errors or exceptions v Invoke signals ™ Processes may send each other signals by kill system call, ™ Kernel may send signals to a process. v A process may react to a signal by ™ Ignore the signal ™ handle signals itself v Asynchronously execute a specified procedure (the signal handler) Signals (Cont.) v A set of defined signals ™ 1)SIGHUP 2) SIGINT 3) SIGQUIT ™ 4) SIGILL 5) SIGTRAP 6) SIGIOT ™ 7) SIGBUS 8) SIGFPE 9) SIGKILL ™ 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 ™ 13) SIGPIPE 14) SIGALR 15)SIGTERM ™ 17) SIGCHLD 18) SIGCONT 19) SIGSTOP ™ 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU ™ 23) SIGURG 24) SIGXCPU 25) SIGXFSZ ™ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH ™ 29) SIGIO 30) SIGPWR Signals (Cont.) v Kernel handles signals using default actions. ™ Terminate the process ™ Core dump and terminate the process v E.g., SIGFPE(floatingpoint exception) : core dump and exit ™ Ignore the signal ™ Suspend the process ™ Resume the process’s execution, if it was stopped v Signal related fields in task_struct data structure ™ signal (32 bits):pending signals ™ blocked: a mask of blocked signal ™ sigaction array: address of handling routine or a flag to let kernel handle the signal Linux Signals v Normal process can only send signals to processes with the same uid and gid or to the processes in the same process group Note: Process Group v Unix introduces the notion of process groups to represent a job ™ $ ls| sort | more ™ A progress group consists of three processes: ls, sort, more v Login session ™ All processes that are descendants of the login shell process Signal Handling v Signal Delivery ™Deliver until the receiver process is scheduled vSignals are not presented to the process immediately they are generated ™Every time a process exits from a system call its signal and blocked fields are checked vIf there are any unblocked signals, they can now be delivered Signal Handling (Cont.) v The Linux signal processing code looks at the sigaction structure for each of the current unblocked signal Pipes v Half-duplex ™Data flows only in one direction v The writer and the reader communicate using standard read/write library function Communication pipe Task A Task B Pipe v $ls| pr |lpr v Linux using two file data structure ™Point at the same temporary VFS inode(points to a physical page within memory) v Use standard read/write library function Pipe Restriction of Pipes and Signals v Signal ™ The only information transported is a simple number v Renders signals unsuitable for transferring data. v Pipe ™ Impossible for any arbitrary process to read or write in a pipe unless it is the child of the process which created it. ™ Named Pipes (also known as FIFO) v also one-way flow of data v allowing unrelated processes to access a single FIFO. System V IPC Mechanisms v System V IPC Mechanisms ™Message queues vSend a message to other processes or receive messages from them ™Semaphores vSynchronize itself with other processes by means of semaphores ™Shared memory vShare a memory area with other processes System V IPC Mechanisms v First appeared in UNIX System V in 1983 v They allow unrelated processes to communicate with each other ™Including those that do not share the same ancestor Key Management v Each IPC resource is identified by ™ 32-bit key: similar to the file pathname v Freely chosen by the programmer ™ 32-bit identifier: similar to the file descriptor v Assigned by the kernel System V IPC v Create IPC resources: semget(), msgget(), shmget() ™ Derive the IPC identifier from the IPC key ™Process then use identifier to access the resource v If two independent processes want to share a common IPC resource ™The processes agree on some fixed, predefined IPC keys v Access to these System V IPC objects is checked using access permissions ™ Similar to access the file Message Queues v Message queue ™ A linked list of messages stored within the kernel ™Identified by a message queue identifier ™For processes to send messages asynchronously to each other. S queue R Message Queues (Cont.) v New messages are always added to the end of a queue v But can be removed anywhere in the queue intmsgrcv(intmsgid, void *ptr, size_tnbytes, long type, intflag) ™The type argument lets us specify which message we want v Type == 0, the first message is returned v Type > 0, the first message whose type equals type is returned Message Queue Data Structures m_listm_list ipc_idsstructure v Where the kernel keeps its message queues structipc_ids{ intsize;/* number of ipc_idelements */ intin_use;/* number of in-use ipc_idelements */ intmax_id; unsigned short seq; unsigned short seq_max; structsemaphore sem;/* semaphore protecting the ipc_idsDS spinlock_tary; structipc_id*entries;/* list of message queues */ }; msg_queuestructure v A single message queue structmsg_queue{ structkern_ipc_permq_perm;/* access permission */ time_tq_stime; /* last msgsndtime */ time_tq_rtime; /* last msgrcvtime */ time_tq_ctime; /* last change time */ unsigned long q_cbytes; /* current number of bytes on queue */ unsigned long q_qnum; /* number of messages in queue */ unsigned long q_qbytes; /* max number of bytes on queue */ pid_tq_lspid; /* pidof last msgsnd*/ pid_tq_lrpid; /* last receive pid*/ structlist_headq_messages; /* List of message in queue */ structlist_headq_receivers; structlist_headq_senders; }; Message Structures v Each message is broken in one or more pages ™ Dynamically allocated v The first page stores ™ The message header with type msg_msg ™The message text v Start right after the next field v If the message text is longer than 4072 bytes ™ The second or third page (and so on) is used with type meg_megseg Message Structures v Message Header structmsg_msg{ structlist_headm_list;/* pointers for message lists */ long m_type;/* message types */ intm_ts; /* message text size */ structmsg_msgseg*next; /* next portion of the message */ /* the actual message follows immediately */ }; Maintains a list of the linked messages in a single queue v Message Segment structmsg_msgseg{ structmsg_msgseg*next; /* the next part of the message follows immediately */ }; Structure of large messages structmsg_msg structmsg_msgseg next structmsg_msgseg next next First Chunk Second Third Chunk Chunk Message queue related system calls v msgget() ™ Return an id either for the existing queue with the key, or for a new message queue with the key v msgsnd() ™ Sends a message to a message queue v msgrcv() ™ Receives a message from a message queue v msgctl() ™ Perform a set of administrative operations on a message queue Semaphores v Semaphores ™A semaphore is a location in memory whose value can be test_and_set (atomic) by more than one processes ™Can be used to implement critical regions Semaphore Data Structure structsem_array The semaphore array Pending semaphore operations Semaphore Data Structure sem& sem_arraystructure structsem_array{ structkern_ipc_permsem_perm;/* permissions*/ time_tsem_otime; /* last semoptime */ time_tsem_ctime; /* last change time */ structsem*sem_base; /* ptrto first semaphore in array */ structsem_queue*sem_pending; /* pending operations */ … unsigned short sem_nsems;/* number of semaphores in array */ }; structsem{ intsemval; /* current value */ intsempid; /* pidof last operation*/ }; sem_queuestructure structsem_queue{ structsem_queue*next; /* next entry in the queue */ structsem_queue**prev; /* previous entry in the queue */ structtask_struct*sleeper; /* pointer to the sleeping process */ intpid;/* process id of requesting process */ intstatus; /* completion status of operation */ structsem_array*sma; /* semaphore array for operations */ ……. structsembuf*sops; /* array of pending operations */ intnsops; /* number of pending operations*/ intalter;/* 1èoperation will alter semaphore */ }; Semaphore related system calls v semget() ™ The counterpart of msgget, semgetcreates a semaphore set. v semop() ™ Performs an operation on semaphores v semctl() ™ Like msgctl, semctlperforms administrative operations on a semaphore set Shared Memory v Shared memory ™Allow processes to communicate via memory that appears in all of their virtual address space ™Controlled via keys and access rights checking ™Rely on other mechanisms (e.g. semaphores) to synchronize access to the memory Shared Memory (Cont.) v Fastest, easiest of the IPC services ™Because data does not need to be copied between the client and server v Mutual exclusion problem ™Should synchronize access to a given region among multiple processes ™Often semaphores are used to synchronize shared memory access Shared Memory (Cont.) share Process 2 Process 1 physical page Share memory system calls v shmget() ™ Returns a unique id for a shared memory region v shmat() ™ Attaches the calling process to a shared memory region v shmdt() ™ Detaches the calling process from the shared memory region v shmctl() ™ Performs administrative operations on a shared memory region File System Linux File System v Linux supports different file system structures at the same time ™ Ext2, ISO 9660, ufs, FAT-16,VFAT,… v Hierarchical File System Structure ™ Linux adds each new file system into this single file system tree as it is mounted v The real file systems are separated from the OS by an interface layer: Virtual File System: VFS v VFS allows Linux to support many different file systems, each presenting a common software interface to the VFS. Hierarchical File System