Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 8 лет назад пользователемЛеонид Чаплин
1 IPC Linux
2 Agenda Intro Intro Signals Signals Pipes Pipes System V IPC Mechanisms System V IPC Mechanisms Message Queues Message Queues Semaphores Semaphores Shared memory Shared memory
3 Intro Processes communicate with each other and with the kernel to coordinate their activities. Linux supports a number of Inter-Process Communication (IPC) mechanisms. Signals and pipes are two of them but Linux also supports the System V IPC mechanisms named after the Unix TM release in which they first appeared. Processes communicate with each other and with the kernel to coordinate their activities. Linux supports a number of Inter-Process Communication (IPC) mechanisms. Signals and pipes are two of them but Linux also supports the System V IPC mechanisms named after the Unix TM release in which they first appeared.
4 Signals Signals are one of the oldest inter-process communication methods used by Unix TM systems. They are used to signal asynchronous events to one or more processes. A signal could be generated by a keyboard interrupt or an error condition such as the process attempting to access a non-existent location in its virtual memory. Signals are also used by the shells to signal job control commands to their child processes. Signals are one of the oldest inter-process communication methods used by Unix TM systems. They are used to signal asynchronous events to one or more processes. A signal could be generated by a keyboard interrupt or an error condition such as the process attempting to access a non-existent location in its virtual memory. Signals are also used by the shells to signal job control commands to their child processes.
5 Signals There are a set of defined signals that the kernel can generate or that can be generated by other processes in the system, provided that they have the correct privileges. You can list a system's set of signals using the kill command (kill -l), on my Intel Linux box this gives: There are a set of defined signals that the kernel can generate or that can be generated by other processes in the system, provided that they have the correct privileges. You can list a system's set of signals using the kill command (kill -l), on my Intel Linux box this gives:
6 Signals 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGIOT 7) SIGBUS 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGIOT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 30) SIGPWR
7 Pipes Simply put, a pipe is a method of connecting the standard output of one process to the standard input of another. Pipes are the eldest of the IPC tools, having been around since the earliest incarnations of the UNIX operating system. They provide a method of one-way communications (hence the term half-duplex) between processes. Simply put, a pipe is a method of connecting the standard output of one process to the standard input of another. Pipes are the eldest of the IPC tools, having been around since the earliest incarnations of the UNIX operating system. They provide a method of one-way communications (hence the term half-duplex) between processes.
8 Pipes ls | sort | lp ls | sort | lp The above sets up a pipeline, taking the output of ls as the input of sort, and the output of sort as the input of lp. The data is running through a half duplex pipe, traveling (visually) left to right through the pipeline. The above sets up a pipeline, taking the output of ls as the input of sort, and the output of sort as the input of lp. The data is running through a half duplex pipe, traveling (visually) left to right through the pipeline.
9 Pipes When a process creates a pipe, the kernel sets up two file descriptors for use by the pipe. One descriptor is used to allow a path of input into the pipe (write), while the other is used to obtain data from the pipe (read). At this point, the pipe is of little practical use, as the creating process can only use the pipe to communicate with itself. Consider this representation of a process and the kernel after a pipe has been created: When a process creates a pipe, the kernel sets up two file descriptors for use by the pipe. One descriptor is used to allow a path of input into the pipe (write), while the other is used to obtain data from the pipe (read). At this point, the pipe is of little practical use, as the creating process can only use the pipe to communicate with itself. Consider this representation of a process and the kernel after a pipe has been created:
10 Pipes
11 Pipes
12 Pipes SYSTEM CALL: pipe(); PROTOTYPE: int pipe( int fd[2] ); RETURNS: 0 on success -1 on error error: errno EMFILE (no free descriptors) EMFILE (system file table is full) EFAULT (fd array is not valid) NOTES: fd[0] is set up for reading, fd[1] is set up for writing
13 Pipes #include #include
14 Named pipes A named pipe works much like a regular pipe, but does have some noticeable differences. A named pipe works much like a regular pipe, but does have some noticeable differences. Named pipes exist as a device special file in the file system. Named pipes exist as a device special file in the file system. Processes of different ancestry can share data through a named pipe. Processes of different ancestry can share data through a named pipe. When all I/O is done by sharing processes, the named pipe remains in the file system for later use. When all I/O is done by sharing processes, the named pipe remains in the file system for later use.
15 Named pipes There are several ways of creating a named pipe. The first two can be done directly from the shell. There are several ways of creating a named pipe. The first two can be done directly from the shell. mknod MYFIFO p mknod MYFIFO p mkfifo a=rw MYFIFO mkfifo a=rw MYFIFO The above two commands perform identical operations, with one exception. The mkfifo command provides a hook for altering the permissions on the FIFO file directly after creation. With mknod, a quick call to the chmod command will be necessary. The above two commands perform identical operations, with one exception. The mkfifo command provides a hook for altering the permissions on the FIFO file directly after creation. With mknod, a quick call to the chmod command will be necessary.
16 Named pipes LIBRARY FUNCTION: mknod(); PROTOTYPE: int mknod( char *pathname, mode_t mode, dev_t dev); LIBRARY FUNCTION: mknod(); PROTOTYPE: int mknod( char *pathname, mode_t mode, dev_t dev); RETURNS: 0 on success, -1 on error RETURNS: 0 on success, -1 on error error: errno error: errno EFAULT (pathname invalid) EFAULT (pathname invalid) EACCES (permission denied) EACCES (permission denied) ENAMETOOLONG (pathname too long) ENAMETOOLONG (pathname too long) ENOENT (invalid pathname) ENOENT (invalid pathname) ENOTDIR (invalid pathname) (see man page for mknod for others) ENOTDIR (invalid pathname) (see man page for mknod for others) NOTES: Creates a filesystem node (file, device file, or FIFO) NOTES: Creates a filesystem node (file, device file, or FIFO)
17 System V IPC Mechanisms With System V, AT&T introduced three new forms of IPC facilities (message queues, semaphores, and shared memory). While the POSIX committee has not yet completed its standardization of these facilities, most implementations do support these. In addition, Berkeley (BSD) uses sockets as its primary form of IPC, rather than the System V elements. Linux has the ability to use both forms of IPC (BSD and System V), although we will not discuss sockets until a later chapter. With System V, AT&T introduced three new forms of IPC facilities (message queues, semaphores, and shared memory). While the POSIX committee has not yet completed its standardization of these facilities, most implementations do support these. In addition, Berkeley (BSD) uses sockets as its primary form of IPC, rather than the System V elements. Linux has the ability to use both forms of IPC (BSD and System V), although we will not discuss sockets until a later chapter.
18 Message Queues Message queues can be best described as an internal linked list within the kernel's addressing space. Messages can be sent to the queue in order and retrieved from the queue in several different ways. Each message queue (of course) is uniquely identified by an IPC identifier. Message queues can be best described as an internal linked list within the kernel's addressing space. Messages can be sent to the queue in order and retrieved from the queue in several different ways. Each message queue (of course) is uniquely identified by an IPC identifier.
19 Message Queues In order to create a new message queue, or access an existing queue, the msgget() system call is used. In order to create a new message queue, or access an existing queue, the msgget() system call is used.
20 Message Queues SYSTEM CALL: msgget(); SYSTEM CALL: msgget(); PROTOTYPE: int msgget ( key_t key, int msgflg ); PROTOTYPE: int msgget ( key_t key, int msgflg ); RETURNS: message queue identifier on success -1 on error RETURNS: message queue identifier on success -1 on error error: errno error: errno EACCESS (permission denied) EACCESS (permission denied) EEXIST (Queue exists, cannot create) EEXIST (Queue exists, cannot create) EIDRM (Queue is marked for deletion) EIDRM (Queue is marked for deletion) ENOENT (Queue does not exist) ENOENT (Queue does not exist) ENOMEM (Not enough memory to create queue) ENOSPC (Maximum queue limit exceeded) ENOMEM (Not enough memory to create queue) ENOSPC (Maximum queue limit exceeded)
21 Message Queues Once we have the queue identifier, we can begin performing operations on it. To deliver a message to a queue, you use the msgsnd system call: Once we have the queue identifier, we can begin performing operations on it. To deliver a message to a queue, you use the msgsnd system call:
22 Message Queues SYSTEM CALL: msgsnd(); SYSTEM CALL: msgsnd(); PROTOTYPE: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg ); PROTOTYPE: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg ); RETURNS: 0 on success -1 on error RETURNS: 0 on success -1 on error error: errno error: errno EAGAIN (queue is full, and IPC_NOWAIT was asserted) EACCES (permission denied, no write permission) EAGAIN (queue is full, and IPC_NOWAIT was asserted) EACCES (permission denied, no write permission) EFAULT (msgp address isn't accessable - invalid) EFAULT (msgp address isn't accessable - invalid) EIDRM (The message queue has been removed) EIDRM (The message queue has been removed) EINTR (Received a signal while waiting to write) EINTR (Received a signal while waiting to write) EINVAL (Invalid message queue identifier, nonpositive message type, or invalid message size) EINVAL (Invalid message queue identifier, nonpositive message type, or invalid message size) ENOMEM (Not enough memory to copy message buffer) ENOMEM (Not enough memory to copy message buffer)
23 Message Queues Through the development of the wrapper functions presented earlier, you now have a simple, somewhat elegant approach to creating and utilizing message queues in your applications. Now, we will turn the discussion to directly manipulating the internal structures associated with a given message queue. Through the development of the wrapper functions presented earlier, you now have a simple, somewhat elegant approach to creating and utilizing message queues in your applications. Now, we will turn the discussion to directly manipulating the internal structures associated with a given message queue. To perform control operations on a message queue, you use the msgctl() system call. To perform control operations on a message queue, you use the msgctl() system call.
24 Message Queues SYSTEM CALL: msgctl(); SYSTEM CALL: msgctl(); PROTOTYPE: int msgctl ( int msgqid, int cmd, struct msqid_ds *buf ); PROTOTYPE: int msgctl ( int msgqid, int cmd, struct msqid_ds *buf ); RETURNS: 0 on success -1 on error RETURNS: 0 on success -1 on error error: errno error: errno EACCES (No read permission and cmd is IPC_STAT) EACCES (No read permission and cmd is IPC_STAT) EFAULT (Address pointed to by buf is invalid with IPC_SET and IPC_STAT commands) EFAULT (Address pointed to by buf is invalid with IPC_SET and IPC_STAT commands) EIDRM (Queue was removed during retrieval) EIDRM (Queue was removed during retrieval) EINVAL (msgqid invalid, or msgsz less than 0) EINVAL (msgqid invalid, or msgsz less than 0) EPERM (IPC_SET or IPC_RMID command was issued, but calling process does not have write (alter) access to the queue) EPERM (IPC_SET or IPC_RMID command was issued, but calling process does not have write (alter) access to the queue)
25 Semaphores & Shared memory
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.