1 / 83

Inter Process Communication

Inter Process Communication. Pipes/FIFO/Message Queues Semaphores Shared Memory. Why do processes communicate?. To share resources Client/server paradigms Inherently distributed applications Reusable software components etc. Types of IPC. Message Passing

zilya
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 Pipes/FIFO/Message Queues Semaphores Shared Memory

  2. Why do processes communicate? • To share resources • Client/server paradigms • Inherently distributed applications • Reusable software components • etc

  3. Types of IPC Message Passing Pipes, FIFOs, and Message Queues Synchronization Mutexes, condition variables, read-write locks, file and record locks, and semaphores Shared memory Remote Procedure Calls Solaris doors and Sun RPC

  4. Sharing of information

  5. What is IPC? Each process has a private address space. Normally, no process can write to another process’s space. How to get important data from process A to process B? Message passing between different processes running on the same operating system is IPC Synchronization is required in case of IPC through shared memory or file system

  6. Pipes Pipes are the oldest form of UNIX System IPC and are provided by all UNIX systems Most commonly used form of IPC Historically, they have been half duplex (i.e., data flows in only one direction). Because they don’t have names, pipes can be used only between processes that have a common ancestor. Normally, a pipe is created by a process, that process calls fork, and the pipe is used between the parent and the child.

  7. UNIX Pipes Parent process, p1 Child process, p2 Info to be shared Info copy int p[2]; pipe(p); write(p[1], “hello”, size); …. read(p[0], inbuf, size); …. pipe for p1 and p2 olleh write function read function FIFO buffer size = 4096 characters

  8. Pipes #include <unistd.h> int pipe(int fd[2]); returns 0 if OK, else -1 fd[0]-> for reading, fd[1] is for writing

  9. Pipes Pipes are rarely used in a single process. They are generally used between parent and child

  10. Pipes main () { int i; int p[2]; pid_t ret; pipe (p); //creating pipe char buf[100]; ret = fork (); if (ret == 0) { write (p[1], "hello", 6);//writing to parent through pipe } if (ret > 0) { read (p[0], buf, 6); //reading from child via pipe printf ("Child Said:%s\n", buf); //printing to stdout } }

  11. Pipes: who|sort stdout

  12. who|sort Create a pipe in the parent Fork a child Duplicate the standard output descriptor to write end of pipe Exec ‘who’ program In the parent wait for the child. Duplicate the standard input descriptor to read end of pipe Exec ‘sort’ program

  13. who|sort main () { int i; int p[2]; pid_t ret; pipe (p); ret = fork (); if (ret == 0) { close (1); dup (p[1]); close (p[0]); execlp (“who", “who", (char *) 0); } if (ret > 0) { close (0); dup (p[0]); close (p[1]); wait (NULL); execlp (“sort", “sort", (char *) 0); }}

  14. dup and dup2 Functions #include <unistd.h> int dup(int filedes); int dup2(int filedes, int filedes2); Both return: new file descriptor if OK, 1 on error The new file descriptor returned by dup is guaranteed to be the lowest-numbered available file descriptor. With dup2, we specify the value of the new descriptor with the filedes2 argument. If filedes2 is already open, it is first closed. If filedes equals filedes2, then dup2 returns filedes2 without closing it.

  15. dup and dup2

  16. Popen #include <stdio.h> FILE *popen(const char *cmdstring, const char *type); Returns: file pointer if OK, NULL on error int pclose(FILE *fp);

  17. popen Popen does creating a pipe, forking a child, closing the unused ends of the pipe, executing a shell to run the command, and waiting for the command to terminate fp = popen("ls *.c", "r"); Not to be used in this course

  18. Name Spaces When two unrelated processes use some type of IPC to exchange information, the IPC object must have a name or identifier of some form The set of possible names for a given type of IPC is called its name space FIFOs have pathname in the file system as identifier

  19. FIFOs Create a FIFO #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode) //returns 0 if OK or -1 Ex: if( mkfifo("fifo1", 0666)<0) perror(); mkfifo returns error ‘EEXIST’ if the FIFO already exists at the given path

  20. FIFOs Once a FIFO is created, it should be opened either for reading or writing wfd=open("fifo1",O_WRONLY); or FILE *fp = fopen(“fifo1”, “w”); FIFO can’t be opened both for reading and writing at the same time Unlike pipe, FIFO is not deleted as soon as all the processes referring to it exit. It has to be explicitly deleted from system. unlink(“fifo1”)

  21. FIFOs between parent and child

  22. FIFOs between parent and child

  23. Properties of FIFO

  24. FIFOs between parent and child Swap these two calls and see

  25. Non-blocking option A descriptor can be set non-blocking in one of the two ways Or

  26. Read and write operations Pipe and FIFO

  27. Writing to pipe/fifo when pipe/fifo is open for reading If data size is less than or equal to PIPE_BUF, the write is atomic i.e. either all the data is written or no data written If there is no room in the pipe for the requested data (<PIPE_BUF), by default it blocks. If O_NONBLOCK option is set, EAGAIN error is returned If data is >PIPE_BUF and O_NONBLOCK option is set, even if 1 byte space is available in the pipe, it will write that much data and return Atomicity is not guaranteed

  28. Message Queues A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier Any process with adequate privileges can place the message into the queue and any process with adequate privileges can read from queue There is no requirement that some process must be waiting to receive message before sending the message

  29. FIFO Vs Message Queues • Data is passed as a stream of bytes in FIFO where is as in MsgQs message boundaries are preserved. • FIFOs require that the writer and reader process be present to pass data where as in MsgQs it is not required • FIFOs follow ‘first in first out’ order of passing data where as in MsgQs messages can be retrieved in any order • Pipes/FIFO are process persistent; MsgQs are kernel persistent

  30. Message Queues Every message queue has following structure in kernel

  31. Message Queues

  32. Permissions struct ipc_perm { uid_t uid; /* owner's effective user id */ gid_t gid; /* owner's effective group id */ uid_t cuid; /* creator's effective user id */ gid_t cgid; /* creator's effective group id */ mode_t mode; /* access modes */ . . . }; Permission Bit user-read 0400 user-write (alter) 0200 group-read 0040 group-write (alter) 0020 other-read 0004 other-write (alter) 0002

  33. Message Queues Firstmsgget is used to either open an existing queue or create a new queue #include <sys/msg.h> int msgget(key_t key, int flag); Returns: message queue ID if OK, 1 on error Key value can be IPC_PRIVATE, key generated by ftok() or any key (long integer) Flag value must be IPC_CREAT if a new queue has to be created IPC_CREAT and IPC_EXCL if want to create a new a queue but don’t reference existing one

  34. Key Values The server can create a new IPC structure by specifying a key of IPC_PRIVATE Kernel generates a uniqe id The client and the server can agree on a key by defining the key in a common header. The client and the server can agree on a pathname and project ID and call the function ftok to convert these two values into a key. #include <sys/ipc.h> key_t ftok(const char *path, int id); The path argument must refer to an existing file. Only the lower 8 bits of id are used when generating the key.

  35. Message Queues When a new queue is created, the following members of the msqid_ds structure are initialized. The ipc_perm structure is initialized msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are all set to 0. msg_ctime is set to the current time. msg_qbytes is set to the system limit. On success, msgget returns the non-negative queue ID. This value is then used with the other three message queue functions.

  36. Messages Each message is composed of a positive long integer type field, and the actual data bytes. Messages are always placed at the end of the queue. Messaeg Template Most applications define their own message structure according to the needs of the application

  37. Sending Messages • #include <sys/msg.h> int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag); • msqid is the id returned by msgget sys call • The ptr argument is a pointer to a message structure • Nbytes is the length of the user data i.e. sizeof(struct mesg) – size of(long). Length can be zero. • A flag value of 0 or IPC_NOWAIT can be specified • mssnd() is blocked until one of the following occurs • Room exists for the message • Message queue is removed (EIDRM error is returned) • Interrupted by a signal ( EINTR is returned)

  38. Receiving Messages • ptr points to the message structure where message will be stord • Length points to the size available on the message structure excluding size of (long) • Type indicates the message desired on the message queue • Flag can be 0 or IPC_NOWAIT or MSG_NOERROR

  39. Receiving Messages • The type argument lets us specify which message we want. • type == 0: The first message on the queue is returned. • type > 0:The first message on the queue whose message type equals type is returned. • type < 0:The first message on the queue whose message type is the lowest value less than or equal to the absolute value of type is returned. • A nonzero type is used to read the messages in an order other than first in, first out. • Priority to messages, Multiplexing

  40. Receiving Messages

  41. Receiving Messages • IPC_NOWAIT flag makes the operation nonblocking, causing msgrcv to return -1 with errno set to ENOMSG if a message of the specified type is not available. • If IPC_NOWAIT is not specified, the operation blocks until • a message of the specified type is available, • the queue is removed from the system (-1 is returned with errno set to EIDRM) • a signal is caught and the signal handler returns (causing msgrcv to return 1 with errno set to EINTR).

  42. Receiving Messages • If the returned message is larger than nbytes and the MSG_NOERROR bit in flag is set, the message is truncated. • no notification is given to us that the message was truncated, and the remainder of the message is discarded. • If the message is too big and MSG_NOERROR is not specified, an error of E2BIG is returned instead (and the message stays on the queue).

  43. Control Operations on Message Queues • #include <sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf ); • IPC_STAT: Fetch the msqid_ds structure for this queue, storing it in the structure pointed to by buf. • IPC_SET: Copy the following fields from the structure pointed to by buf to the msqid_ds structure associated with this queue: msg_perm.uid, msg_perm.gid, msg_perm.mode, and msg_qbytes. • IPC_RMID: Remove the message queue from the system and any data still on the queue. This removal is immediate. • Any other process still using the message queue will get an error of EIDRM on its next attempted operation on the queue. • Above two commands can be executed only by a process whose effective user ID equals msg_perm.cuid or msg_perm.uid or by a process with superuser privileges

  44. Server.c

  45. Client.c

  46. Multiplexing Messages • Possibility of dead lock

  47. Multiplexing Messages

  48. System V Semaphores • A semaphore is a primitive used to provide synchronization between various processes (or between various threads in a given process) • Binary Semaphores: a semaphore that can assume only values 0 or 1 • Counting Semaphores: semaphore is initialized to N indicating the number of resources

  49. System V Semaphores • Semaphores are maintained by kernel

  50. Semaphore operations • Create a semaphore and initialize it • should be atomically done • Wait for a semaphore: This tests the value of the semaphore. waits (blocks) if the value is less than or equal to 0 and then decrements the semaphore value once it is greater than 0 (aka P, lock, wait) • Testing and decrementing should be a single atomic operation • Post a semaphore. This increments the semaphore value. If any processes are blocked waiting for this semaphores’s value o be greater than 0, one of those processes are woken up (aka V, unlock, signal)

More Related