1 / 23

Inter-Process Communication Mechanisms

Inter-Process Communication Mechanisms. CSE331 Operating Systems Design. Cooperating Processes. Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process

uyen
Télécharger la présentation

Inter-Process Communication Mechanisms

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 Mechanisms CSE331 Operating Systems Design

  2. Cooperating Processes • Independent process cannot affect or be affected by the execution of another process. • Cooperating process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing • Computation speed-up • Modularity • Convenience • Dangers of process cooperation • Data corruption, deadlocks, increased complexity • Requires processes to synchronize their processing

  3. Purposes for IPC • Data Transfer • Sharing Data • Event notification • Resource Sharing and Synchronization • Process Control

  4. IPC Mechanisms • Mechanisms used for communication and synchronization • Message Passing • message passing interfaces, mailboxes and message queues • sockets, STREAMS, pipes • Shared Memory: Non-message passing systems • Common examples of IPC • Synchronization using primitives such as semaphores to higher level mechanisms such as monitors. Implemented using either shared memoryor message passing. • Debugging • Event Notification - UNIX signals

  5. Message Passing • In a Message passing system there are no shared variables. IPC facility provides two operations for fixed or variable sized message: • send(message) • receive(message) • If processes P and Q wish to communicate, they need to: • establish a communicationlink • exchange messages via send and receive • Implementation of communication link • physical (e.g., shared memory, hardware bus) • logical (e.g., syntax and semantics, abstractions)

  6. Implementation Questions • How are links established? • Can a link be associated with more than two processes? • How are links made known to processes? • How many links can there be between every pair/group of communicating processes? • What is the capacity of a link? • Is the size of a message that the link can accommodate fixed or variable? • Is a link unidirectional or bi-directional?

  7. Message Passing Systems • Exchange messages over a communication link • Methods for implementing the communication link and primitives (send/receive): • Direct or Indirect communications (Naming) • Symmetric or Asymmetric communications • Automatic or Explicit buffering • Send-by-copy or send-by-reference • fixed or variable sized messages

  8. Direct Communication – Internet and Sockets • Processes must name each other explicitly: • Symmetric Addressing • send (P, message) – send to process P • receive(Q, message) – receive from Q • Asymmetric Addressing • send (P, message) – send to process P • receive(id, message) – rx from any; system sets id = sender • Primitives: • send(A, message) – send a message to mailbox A • receive(A, message) – receive a message from mailbox A • Properties of communication link • Links established automatically between pairs • processes must know each others ID • Exactly one link per pair of communicating processes • Disadvantage: a process must know the name or ID of the process(es) it wishes to communicate with

  9. Indirect Communication - Pipes • Messages are sent to or received from mailboxes (also referred to as ports). • Each mailbox has a unique id. • Processes can communicate only if they share a mailbox. • Properties of communication link • Link established only if processes share a common mailbox • A link may be associated with more than 2 processes. • Each pair of processes may share several communication links. • Ownership: • process owns (i.e. mailbox is implemented in user space): only the owner may receive messages through this mailbox. Other processes may only send. When process terminates any “owned” mailboxes are destroyed. • system owns – then mechanisms provided to create, delete, send and receive through mailboxes. Process that creates mailbox owns it (and so may receive through it) but may transfer ownership to another process.

  10. Indirect Communication • Mailbox sharing: • P1, P2, andP3 share mailbox A. • P1, sends; P2andP3 receive. • Who gets the message? • Solutions • Allow a link to be associated with at most two processes. • Allow only one process at a time to execute a receive operation. • Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was

  11. Synchronizing Message Flow • Message passing may be either blocking or non-blocking. • blocking send: sender blocked until message received by mailbox or process • nonblocking send: sender resumes operation immediately after sending • blocking receive: receiver blocks until a message is available • nonblocking receive: receiver returns immediately with either a valid or null message.

  12. Conventional View Protection domains - (virtual address space) user process 2 process n process 1 kernel How can processes communicate with each other and the kernel?

  13. process 1 pipe Universal IPC Facilities handler user process 2 dbx kernel stop handle event • Universal Facilities in UNIX • Signals - asynchronous or synchronous event notification. • Pipes - unidirectional, FIFO, unstructured data stream. • Process tracing - used by debuggers to control target process

  14. UNIX Pipes • Pipe sets up communication channel between two (related) processes. Two processes connected by a pipe 37

  15. One process writes to the pipe, the other reads from the pipe. • Looks exactly the same as reading from/to a file. • System call: int fd[2] ; pipe(&fd[0]) ; fd[0] now holds descriptor to read from pipe fd[1] now holds descriptor to write into pipe

  16. Simple Example #include <unistd.h> #include <fcntl.h> #include <stdio.h> char *message = "This is a message!!!" ; main() { char buf[1024] ; int fd[2]; pipe(fd); /*create pipe*/ if (fork() != 0) { /* I am the parent */ write(fd[1], message, strlen (message) + 1) ; } else { /*Child code */ read(fd[0], buf, 1024) ; printf("Got this from MaMa!!: %s\n", buf) ; } }

  17. Create a Pipeline • Sometimes useful to connect a set of processes in a pipeline. Process Process Pipe Pipe c D Process A writes to pipe AB, Process B reads from AB and writes to BC Process C reads from BC and writes to CD …..

  18. MAX Shared Memory (unique key) Create ptr ptr Attach Attach 0 ptr ptr ptr Proc. 4 Proc. 5 Proc. 3 Shared Memory Common chunk of read/write memory among processes Proc. 2 Proc. 1

  19. Creating Shared Memory int shmget(key_t key, size_t size, int shmflg); Example: key_t key; int shmid; key = ftok(“<somefile>", ‘A'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Here’s an example: shm_create.c.

  20. Attach and DetachShared Memory void *shmat(intshmid, void *shmaddr, intshmflg); intshmdt(void *shmaddr); Example: key_t key; intshmid; char *data; key = ftok("<somefile>", ‘A'); shmid = shmget(key, 1024, 0644); data = shmat(shmid, (void *)0, 0); shmdt(data); Here’s an shm_attach.c

  21. Deleting Shared Memory int shmctl(int shmid, int cmd, struct shmid_ds *buf); shmctl(shmid, IPC_RMID, NULL); Example: Shm_delete.c

  22. Command-line IPC control • ipcs • Lists all IPC objects owned by the user • ipcrm • Removes specific IPC object

  23. Shared Semaphores • Managing concurrent access to shared memory segment. • Using Shared Semaphores • Creation: semget( … ) • Incr/Decr/Test-and-set : semop(…) • Deletion: semctl(semid, 0, IPC_RMID, 0);

More Related