Carnegie Mellon Inter-Process Communication Mechanisms

Slide Note
Embed
Share

Carnegie Mellon Inter-Process Communication (IPC) mechanisms enable communication between processes living in different memory address spaces. This involves cooperating processes needing data transfer, resource sharing, event notification, and process control. IPC methods include Pipes, Shared Memory, Message Queues, Signals, and more, facilitating communication within a single computer or over a network.


Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Carnegie Mellon Inter-Process Communication (IPC) Mechanisms

  2. Carnegie Mellon Communication between processes Processes live in different worlds , a.k.a. memory address spaces due to virtual memory. Communication between processes is needed e.g. killing a process from a shell command e.g. sending data between processes Process A s address space Process B s address space

  3. Carnegie Mellon Inter-process Communication (IPC) Cooperating processes or threads need inter-process communication (IPC) Data transfer Sharing data Event notification Resource sharing Process control Processes may be running on one or more computers connected by a network. The method of IPC used may vary based on the bandwidth and latency of communication between the threads, and the type of data being communicated. IPC may also be referred to as inter-application communication.

  4. Carnegie Mellon IPC mechanisms Pipes (Unnamed) Pipes FIFO (Named Pipes) Shared Memory Message Queues Signals Memory Mapped Files Remote processes, over network: Remote Procedure Calls (RPC) Sockets (in network course)

  5. Carnegie Mellon IPC: (Unnamed) Pipes A byte-stream among processes. Bytes written by a process is readable by another process in the other end. Process A s address space Process B s address space User Space Kernel Space Pipe

  6. Carnegie Mellon (Unnamed) Pipes Pipe is a data structure in the kernel. Data is stored in kernel buffers temporarily. No random access (seek) on pipes. A pipe is created by using the pipe system call int pipe(int* fd); fdis an array of size 2 Two file descriptors are returned fd[0] is open for reading fd[1] is open for writing Some systems implement bidirectional pipes, both ends are readable/writable. Typical buffer size is 512 bytes (Minimum limit defined by POSIX). Reads and writes may be blocked by the buffer.

  7. Carnegie Mellon (Unnamed) Pipes Typical use: Pipe created by a process Process calls fork() File descriptors are inherited by child, so pipe is shared with the child. Pipe used as a communication medium between parent and child A pipe provides a one-way flow of data example: who | sort| lpr output of who is input to sort output of sort is input to lpr

  8. Carnegie Mellon (Unnamed) Pipes example #include <unistd.h> #include <stdio.h> int main(void){ int n; int fd[2]; // to hold fds of both ends of pipe pid_t pid; // pid of child process char line[80]; // buffer to hold text read/written if (pipe(fd) < 0) perror("pipe error"); // to keep track of num bytes read // create the pipe if ((pid = fork()) < 0) { // fork off a child perror("fork error"); } else if (pid > 0) { close(fd[0]); // close read end write(fd[1], "hello world\n", 12); // write to it }else { close(fd[1]); // close write end n = read(fd[0], line, 80); write(1, line, n); } exit(0); } // parent process // child process // read from pipe // echo to screen

  9. Carnegie Mellon After the fork() call Descriptor table For parent stdin fd 0 fd 1 fd 2 fd 3 fd 4 stdout stderr filedes[2] gets {3, 4} as a result of pipe() call

  10. Carnegie Mellon After the fork() call Descriptor table For parent Descriptor table For child stdin stdin fd 0 fd 1 fd 2 fd 3 fd 4 fd 0 fd 1 fd 2 fd 3 fd 4 stdout stdout stderr stderr

  11. Carnegie Mellon After the close() calls Descriptor table For parent Descriptor table For child stdin stdin fd 0 fd 1 fd 2 fd 3 fd 4 fd 0 fd 1 fd 2 fd 3 fd 4 stdout stdout stderr stderr X X This pipe allows parent to send data to the child. If two way communication is needed, then the parent needs to create two pipes before fork() and use the second pipe as a second channel. Systems with bidirectional pipes can do it in single pipe. The other end does not get EOF if at least one process is keeping one end open. Always close the end that process will not use!

  12. Carnegie Mellon (Unnamed) Pipes Unnamed pipes in shell are used with redirection cat /etc/passwd | wc -l Shell setups stdout of a child to pipe write, stdin of next one as pipe read int fd[2], c; if (pipe(fd) < 0) if (fork()) { if (fork()) { close(fd[0]); close(fd[1]); wait(&c); wait(&c); } else { // pipe reader child close(fd[1]); // not using write end dup2(fd[0],0); // redirect stdin to read end close(fd[0]); // it is duplicated, close execl("/usr/bin/wc","wc","-l",NULL); // run binary } } else { close(fd[0]); // not using read end dup2(fd[1],1); // redirect stdout to write end close(fd[1]); // it is duplicated, close execl("/bin/cat","cat","/etc/passwd",NULL);// run binary } // to hold fds of both ends of pipe perror("pipe error"); // create the pipe // parent process // not going to use it // wait for both

  13. Carnegie Mellon shell$ cat /etc/passwd | wc -l int fd[2], c; if (pipe(fd) < 0) if (fork()) { if (fork()) { close(fd[0]); close(fd[1]); wait(&c); wait(&c); } else { // pipe reader child close(fd[1]); // not using write end dup2(fd[0],0); // redirect stdin to read end close(fd[0]); // it is duplicated, close execl("/usr/bin/wc","wc","-l",NULL); // run binary } } else { close(fd[0]); // not using read end dup2(fd[1],1); // redirect stdout to write end close(fd[1]); // it is duplicated, close execl("/bin/cat","cat","/etc/passwd",NULL);// run binary } // to hold fds of both ends of pipe perror("pipe error"); // create the pipe // parent process // not going to use it // wait for both

  14. Carnegie Mellon (Unnamed) Pipes - discussion Applications: in shell passing output of one program to another program e.g. cat file1 file2 | sort Limitations: Processes need to be relatives (parent-child or siblings) cannot be used for broadcasting; Data in pipe is a byte stream has no structure No way to distinguish between several readers or write Implementation: Internal kernel buffers, socket buffers or STREAM interface.

  15. Carnegie Mellon IPC: FIFO (Named Pipe) FIFOs are pipes named as a path on the file system. has a name and permissions just like an ordinary file and appears in a directory listing can be accessed by any process that knows the name and have the appropriate permissions. special files that persist even after all processes have closed them FIFO s can be created by: executing the mkfifo command from a command shell or, calling the mkfifo() system call within a program

  16. Carnegie Mellon IPC: FIFO (Named Pipe) Pipes are restricted to processes of same family (parent/child, siblings). Relies on file descriptor inheritance. Pipes are temporary. They disappear when last process closes. FIFOs or named pipes, are special files that persist even after all processes have closed them Any process with the appropriate permissions can access a FIFO A user creates a FIFO by executing the mkfifo command from a command shell or by calling the mkfifo()system call within a program

  17. Carnegie Mellon FIFO Creation in shell FIFO are created using the mknod or the mkfifo commands $ mkfifo name $ mkfifo m mode name $ mknod name p Make sure you remove (rm) your pipes after use! >man mknod mknod - make block or character special files mknod [OPTION]... NAME TYPE [MAJOR MINOR] . Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they must be omitted when TYPE is p. ..... p create a FIFO >man mkfifo mkfifo -- make fifos mkfifo [-m mode] fifo_name ... mkfifo creates the fifos requested, in the order specified. By default, the resulting fifos have mode 0666 (rw-rw-rw-), limited by the current umask(2).

  18. Carnegie Mellon Using FIFO through shell commands First, create your pipes $ mkfifo pipe1 $ mkfifo pipe2 $ mkfifo pipe3 Then, attach a data source to your pipes $ ls -l >> pipe1 & $ cat myfile >> pipe2 & $ who >> pipe3 & Then, read from the pipes with your reader process $ cat < pipe1 | lpr $ spell < pipe2 $ sort < pipe3 o Finally, delete your pipes $ rm pipe[1-3]

  19. Carnegie Mellon mkfifo() system call int mkfifo(const char *path, mode_t mode); Themkfifo()function creates a new FIFO special file corresponding to the path name specified in the path parameter The mode parameter specifies the permissions for the newly created FIFO If successful, the function returns zero; otherwise, it returns 1 and sets errno

  20. Carnegie Mellon FIFO: example #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main() { char *path = "/tmp/fifo"; pid_t pid; void child(char *path) { int fd; char buf[] = "123456789"; setlinebuf(stdout); unlink(path); mkfifo(path, 0600); pid = fork(); if (pid == 0) { child(path); } else { parent(path); } return 0; fd = open(path, O_WRONLY); write(fd, buf, sizeof(buf)); printf("Child sends: %s\n", buf); close(fd); } void parent(char *path) { int fd; char buf[512]; } fd = open(path, O_RDONLY); read(fd, buf, sizeof(buf)); printf("Parent receives: %s\n", buf); close(fd); Parent receives: 123456789 Child sends: 123456789 }

  21. Carnegie Mellon FIFO- discussion Applications: Limitations: Implementation:

  22. Carnegie Mellon Signals A signal is a small message that notifies a process that an event of some type has occurred in the system Sometimes referred to as software interrupts . Process A s address space Process B s address space User Space Kernel Space Signal number 0 1 2 3 4 5 6 7 8 Signals for A Signals for B

  23. Carnegie Mellon Signals A signal is a small message that notifies a process that an event of some type has occurred in the system Akin to exceptions and interrupts. Interrupts: Hardware to OS, Signals: OS to processes Signal type is identified by small integer ID s (1-30) Only information in a signal is its ID and the fact that it arrived Most of them causes termination but process can block, define a "handler" or ignore them (except SIGKILL and SIGSTOP).

  24. Carnegie Mellon Signals ID Name Default Action Corresponding Event 1 SIGHUP Terminate Terminal line close 2 SIGINT Terminate User typed ctrl-c 3 SIGQUIT Terminate Ctrl+ \ 4 SIGILL Terminate Illegal instruction on CPU 8 SIGFPE Terminate Floating point exception Thumbnail. Thumbnail. 9 SIGKILL Terminate Kill program (cannot override or ignore) 11 SIGSEGV Terminate Segmentation violation 13 SIGPIPE Terminate Write on a closed pipe 14 SIGALRM Terminate User timer 15 SIGTERM Terminate Terminate process (can be overwritten) 17 SIGCHLD Ignore Child stopped or terminated 19 SIGSTOP Suspend Suspend process execution 18 SIGCONT Continue Continue suspended process 10 12 SIGUSR1 SIGUSR2 Ignore User defined

  25. Carnegie Mellon Signal Concepts: Sending a Signal Kernel sends (delivers) a signal to a destination process by updating some state in the context of the destination process Signals can be initiated by: Hardware event, interrupt: SIGFPE, SIGSEGV, SIGILL. OS event: SIGPIPE, SIGHUP, SIGCHLD, SIGALRM, User input Process request: kill() system call PCB stores signal delivery status and setup for a process. Each is a bitmap, a bit per signal: Pending: Signal is sent to the process, waiting to be delivered Block: Delivery of signal is to be blocked by the process

  26. Carnegie Mellon Signal Concepts: Sending a Signal User level Process B Process A Process C kernel Pending for A Pending for B Pending for C Blocked for A Blocked for B Blocked for C

  27. Carnegie Mellon Signal Concepts: Sending a Signal User level Process B Process A Process C kernel Pending for A Pending for B Pending for C Blocked for A Blocked for B Blocked for C 0

  28. Carnegie Mellon Signal Concepts: Sending a Signal User level Process B Process A Process C kernel Pending for A Pending for B Pending for C Blocked for A Blocked for B Blocked for C 1

  29. Carnegie Mellon Signal Concepts: Sending a Signal User level Process B Process A Process C kernel Pending for A Pending for B Pending for C Blocked for A Blocked for B Blocked for C 1

  30. Carnegie Mellon Signal Concepts: Sending a Signal User level Process B Process A Process C kernel Pending for A Pending for B Pending for C Blocked for A Blocked for B Blocked for C 0

  31. Carnegie Mellon Signal Concepts: Receiving a Signal A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal Some possible ways to react: Ignore the signal (do nothing) Terminate the process (with optional core dump) Catchthe signal by executing a user-level function called signal handler Akin to a hardware exception handler being called in response to an asynchronous interrupt: (1) Signal received by process (2) Control passes to signal handler Icurr Inext (3) Signal handler runs (4) Signal handler returns to next instruction

  32. Carnegie Mellon Signal Concepts: Pending and Blocked Signals A signal is pending if sent but not yet received There can be at most one pending signal of any particular type Important: Signals are not queued If a process has a pending signal of type k, then subsequent signals of type k that are sent to that process are discarded A process can block the receipt of certain signals Blocked signals can be delivered, but will not be received until the signal is unblocked A pending signal is received at most once

  33. Carnegie Mellon Signal Concepts: Pending/Blocked Bits Kernel maintains pending and blocked bit vectors in the context of each process pending: represents the set of pending signals Kernel sets bit k in pending when a signal of type k is delivered Kernel clears bit k in pending when a signal of type k is received blocked: represents the set of blocked signals Can be set and cleared by using the sigprocmask function Also referred to as the signal mask.

  34. Carnegie Mellon Sending Signals: Process Groups A process group is used to control the distribution of a signal; when a signal is directed to a process group, the signal is delivered to each process that is a member of the group. Every process belongs to exactly one process group pid=10 pgid=10 Shell Back- ground job #1 Fore- ground job Back- ground job #2 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 Background process group 32 Background process group 40 Child Child getpgrp() Return process group of current process pid=21 pgid=20 pid=22 pgid=20 setpgid() Change process group of a process Foreground process group 20

  35. Carnegie Mellon Sending Signals with /bin/kill Program /bin/kill program sends arbitrary signal to a process or process group linux> ./forks 16 Child1: pid=24818 pgrp=24817 Child2: pid=24819 pgrp=24817 linux> ps PID TTY TIME CMD 24788 pts/2 00:00:00 tcsh 24818 pts/2 00:00:02 forks 24819 pts/2 00:00:02 forks 24820 pts/2 00:00:00 ps linux> /bin/kill -9 -24817 linux> ps PID TTY TIME CMD 24788 pts/2 00:00:00 tcsh 24823 pts/2 00:00:00 ps linux> Examples /bin/kill 9 24818 Send SIGKILL to process 24818 /bin/kill 9 24817 Send SIGKILL to every process in process group 24817

  36. Carnegie Mellon Sending Signals from the Keyboard Typing ctrl-c (ctrl-z) causes the kernel to send a SIGINT (SIGTSTP) to every job in the foreground process group. SIGINT default action is to terminate each process SIGTSTP default action is to stop (suspend) each process pid=10 pgid=10 Shell Back- ground job #1 Fore- ground job Back- ground job #2 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 Background process group 32 Background process group 40 Child Child pid=21 pgid=20 pid=22 pgid=20 Foreground process group 20

  37. Carnegie Mellon Sending Signals with kill Function void fork12() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) { /* Child: Infinite Loop */ while(1) ; } for (i = 0; i < N; i++) { printf("Killing process %d\n", pid[i]); kill(pid[i], SIGINT); } for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } } forks.c

  38. Carnegie Mellon Receiving Signals Suppose kernel is returning from an exception handler and is ready to pass control to process p Process A Process B user code context switch kernel code Time user code context switch kernel code user code

  39. Carnegie Mellon Receiving Signals Suppose kernel is returning from an exception handler and is ready to pass control to process p Kernel computes pnb = pending & ~blocked The set of pending nonblocked signals for process p If (pnb == 0) Pass control to next instruction in the logical flow for p Else Choose least nonzero bit k in pnband force process p to receive signal k The receipt of the signal triggers some action by p Repeat for all nonzero k in pnb Pass control to next instruction in logical flow for p

  40. Carnegie Mellon Default Actions Each signal type has a predefined default action, which is one of: The process terminates The process stops until restarted by a SIGCONT signal The process ignores the signal

  41. Carnegie Mellon Installing Signal Handlers The signal function modifies the default action associated with the receipt of signal signum: handler_t *signal(int signum, handler_t *handler) Different values for handler: SIG_IGN: ignore signals of type signum SIG_DFL: revert to the default action on receipt of signals of type signum Otherwise, handler is the address of a user-level signal handler Called when process receives signal of type signum Referred to as installing the handler Executing handler is called catching or handling the signal When the handler executes its return statement, control passes back to instruction in the control flow of the process that was interrupted by receipt of the signal

  42. Carnegie Mellon Signal Handling Example void sigint_handler(int sig) /* SIGINT handler */ { printf("So you think you can stop the bomb with ctrl-c, do you?\n"); sleep(2); printf("Well..."); fflush(stdout); sleep(1); printf("OK. :-)\n"); exit(0); } int main(int argc, char** argv) { /* Install the SIGINT handler */ if (signal(SIGINT, sigint_handler) == SIG_ERR) unix_error("signal error"); /* Wait for the receipt of a signal */ pause(); return 0; } sigint.c

  43. Carnegie Mellon Signal Handler In normal operation, when leaving kernel mode, your process will simply return to the next instruction after the point where it originally left user mode. However if a signal is pending for your process, the kernel will re-write your processes context such that the return to user mode will instead go to the first instruction of your signal handler and your stack will have been modified to look like you had made a "special" subroutine call to the signal handler at the point where you originally left user mode. NOTE that the return from this "special" subroutine call involves making a system call to restore the original state. https://unix.stackexchange.com/questions/241115/signal-execution-details

  44. Carnegie Mellon Signal Handler Usually works in same stack. Kernel pushes handler activation on stack. User mode Kernel mode User mode Process stack is modified as a call to handler function SP and IP registers are modified SP locals Handler Returns param IPcur SP SP IP: IPcur IP: handleraddr IP: IPcur Signal raised Return

  45. Carnegie Mellon Nested Signal Handlers Handlers can be interrupted by other handlers Main program Handler S Handler T (2) Control passes to handler S (1) Program catches signal s Icurr (4) Control passes to handler T (3) Program catches signal t Inext (7) Main program resumes (5) Handler T returns to handler S (6) Handler S returns to main program

  46. Carnegie Mellon Blocking and Unblocking Signals Implicit blocking mechanism Kernel blocks any pending signals of type currently being handled. E.g., A SIGINThandler can t be interrupted by another SIGINT Explicit blocking and unblocking mechanism sigprocmaskfunction Supporting functions sigemptyset Create empty set sigfillset Add every signal number to set sigaddset Add signal number to set sigdelset Delete signal number from set

  47. Carnegie Mellon Safe Signal Handling Handlers are tricky because they are concurrent with main program and share the same global data structures. Shared data structures can become corrupted. Pending signals are not queued For each signal type, one bit indicates whether or not signal is pending thus at most one pending signal of any particular type. You can t use signals to count events, such as children terminating. Waiting for Signals int sigsuspend(const sigset_t *mask)

  48. Carnegie Mellon IPC: Shared Memory Allows multiple processes to share virtual memory space. Fastest but not necessarily the easiest (synchronization- wise) way for processes to communicate with one another. Process B s address space Process A s address space User Space Kernel Space

  49. Carnegie Mellon Shared Memory Implemented by mapping virtual pages from different processes onto the same physical page frames. Process B Process A Shared memory region 0x30000 0x50000 0x50000 0x70000

  50. Carnegie Mellon Shared Memory One process creates or allocates the shared memory segment. size and access permissions set at creation. The process then attaches the shared segment, causing it to be mapped into its current data space. If needed, the creating process then initializes the shared memory. Once created, and if permissions permit, other processes can gain access to the shared memory segment and map it into their data space. Each process accesses the shared memory relative to its attachment address. For each process involved, the mapped memory appears to be no different from any other of its memory addresses.

Related