1 / 29

Inter Process Communication

Inter Process Communication. Signals. 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.

ayala
Télécharger la présentation

Inter Process Communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inter Process Communication

  2. Signals 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.

  3. Linux 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.

  4. Linux 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

  5. Signals 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.

  6. Pipes 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. synchronize 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 inode’s 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

  8. Named Pipes (FIFO) Not temporary objects mkfifo <path> prw-r—r– 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

  9. System 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 } ;

  10. Message 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 } ;

  11. struct msg { struct msg * msg_next ; long msg_type ; char * msg_spot ; time_t msg_stime ; // no use short msg_ts ; // length } ;

  12. Write to the queue: euid, egid compare with the mode in the queue’s ipc_perm tagged with an application specific type no room, the number / the length added to the message queue’s write waiting queue Read from the queue : check access rights the first message / select messages with particular types no messages match, read waiting queue

  13. Semapores 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).

  14. 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

  15. 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 ; } ;

  16. semary[] semid_ds sem_nsems sem_base sem_queue sem_pending sem_undo undo

  17. 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 ; } ;

  18. Linux will maintain at most one sem_undo per process for each semaphore array. example: 2 -> semaphore value, -2 -> sem_undo Processes are deleted. Linux apply sem_undo. Semaphore set is deleted. The sem_undo are left on process’s task_struct but the semaphore array identifier is made invalid.

  19. Shared 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 ; } ;

  20. shm_seg[] 128 shmid_ds numpages vm_area_struct shm_pages attaches

  21. 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.

  22. no use, detach Remove its vm_area_struct process’s page tables are updated to invalidate the area of the memory that it used to share. The last process the physical memory are freed

  23. Kernel 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) ; }

  24. 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 ; } ;

  25. __pseudo__ void down (struct semaphore * psem) { while (-- psem -> count <= 0) { psem -> waiting ++ ; if (psem -> count + psem -> waiting <=0) do { sleep_uniteruptible (psem -> wait) ; } while (psem -> count) ; psem -> count += psem -> waiting ; psem -> waiting = 0 ; } }

  26. __pseudo__ void up (struct semaphore) { if (++ psem -> count <= 0) { psem -> count += psem -> waiting ; psem -> waiting = 0 ; wake_up (psem -> wait) ; } }

  27. Others Files Sockets

More Related