Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
IPC Interprocess Communication • Processes within a system may be independent or cooperating • Cooperating processes need interprocess communication (IPC) • Two models of IPC – Shared memory – Message passing Communications Models Cooperating Processes • Advantages of process cooperation – Information sharing – Computation speed-up – Modularity – Convenience Message passing systems • For distributed environment • Message passing implemented using system calls, (kernel intervention required each time) • Message passing is easy to implement • Message passing is useful for exchanging smaller amount of data. Cooperating Processes via Message Passing • IPC facility provides two operations. send(message) - message size can be fixed or variable receive(message) • If processes P and Q wish to communicate, they need to: – establish a communication link between them – exchange messages via send/receive • Fixed vs. Variable size message Direct communication • Each process that wants to communicate must explicitly name the recipient or sender of the communication. • send(P, msg) - send a message to process P • receive(Q, msg) - receive a message from process Q Indirect communication – Indirect communication • receive/send(A, msg) • A: is the mailbox IPC using “pipes” • Ordinary pipes: data is written by a process on one end (write-end) and read by another process at other end (read-end). • Unidirectional • Pipe is treated as a special type of file, can be accessed by read() or write() system calls. • Ordinary pipe can be used within a process only (parent and child processes) Pipes as a Mechanism for Inter-process Communication pipe - create an interprocess communication channel int pipe(fd) ; int fd[2]; •The pipe() system call creates an I/O mechanism called a pipe and returns two file descriptors, fd[0] and fd[1]. • fd[0] is opened for reading and fd[1] is opened for writing. RETURN VALUES pipe() returns: 0 on success. -1 on failure and sets errno to indicate the error. • The pipe() system call gives parent-child processes a way to communicate with each other. • It is called as follows: int pipe(int fd[2]); • In other words, you pass it an array of two integers. It fills in that array with two file descriptors that can talk to each other. Anything that is written on fd[1] may be read by fd[0]. This is of no use in a single process. However, between processes, it gives a method of communication. #include <stdio.h> main() { int pipefd[2]; int i; char s[1000]; char *s2; if (pipe(pipefd) < 0) { perror("pipe"); exit(1); } s2 = "Rex Morgan MD"; write(pipefd[1], s2, strlen(s2)); i = read(pipefd[0], s, 1000); s[i] = '\0'; printf("Read %d bytes from the pipe: '%s'\n", i, s); } • This first calls pipe() to set up two file descriptors pipefd[0] and pipefd[1]. • Anything written to pipefd[1] can be read by pipefd[0]. • To put this in another way, whenever you call write(fd, buf, size), your process sends size bytes starting at the address specified by buf to the operating system. • The fd tells the operating system what to do with those bytes. Usually fd is a file descriptor returned by open() -- thus, your write() call tells the operating system to write those bytes to a file. • However, there are other types of file descriptors. For example, when you say write(1, buf, size), you are saying to print those bytes to standard output, which often is not a disk file, but instead is a terminal. When fd is the writing end of a pipe, the write() specifies for the operating system to hold those bytes in a buffer until some process requests for them by performing a read() on the read end of the pipe. It is important to note the following about pipes as an IPC mechanism: 1. Unix pipes are buffers managed from within the kernel. 2. Note that as a channel of communication, a pipe operates in one direction only. 3. Some plumbing (closing of ends) is required to use a pipe. 4. Pipes are useful when both the processes are schedulable and are resident on the same machine. So, pipes are not useful for processes across networks. 5. The read end of a pipe reads any way. It does not matter which process is connected to the write end of the pipe. Therefore, this is a very insecure mode of communication. 6. Pipes cannot support broadcast.