ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication

Pages 25
Views 4

Please download to get full document.

View again

of 25
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Description
ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication Risto Järvinen October 12, 2015 Lecture contents This lecture: Different ways processes can affect each other Classic IPC Stevens:
Transcript
ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication Risto Järvinen October 12, 2015 Lecture contents This lecture: Different ways processes can affect each other Classic IPC Stevens: ch10 (again), ch15 (except ), ch16-17 (except internet sockets) Kerrisk: ch22, ch44, ch49-50, ch56-57,60-61 (ch58-59 = internet sockets) Internet sockets are covered in Network Programming course, so they are informative and useful to study, but not required on this course. Next lecture: System V IPC POSIX IPC ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 2/25 How can one process affect another? Use of same resources (CPU time, memory, I/O bandwidth,..) Fork() and wait()ing for children. Signals. Using same filesystem, shared files and directories. File locks. Memory mapped files (mmap()) Terminals/pseudoterminals. Pipes and sockets. Dedicated IPC mechanisms: Message queues, Shared memory, Semaphores. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 3/25 Classic IPC Filesystem and Filelocks. (lect5) Pipes, named and unnamed. (today) Sockets, UNIX-domain and network. (today) Terminals. (lect3) Shared memory via mmap(). (lect3, briefly today) Signals, real-time signals. (lect4, RTS today) Some topics already partially covered. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 4/25 Signals Covered before. Basic idea is the have asynchronous notification to the process. Implemented as a function (signal handler) that can be called at any time. Limited usability (few signals, no ordering, may lose signals, etc..) ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 5/25 POSIX Real-Time Signals (RTS) Extension to signals: More signals. Queueing model, with priorities. Sender is identified. Can deliver data. Doesn t lose multiple signals, though there is limit on how many times a signal can arrive. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 6/25 Real-Time Signals limitations Amount of signals is implementation specific. Standard doesn t specify whether RTS operation may be used with normal signals. Delivered data is only an integer or a pointer. And a pointer usually has no meaning across processes. Limits available via sysconf() and signal.h: SIGRTMIN to SIGRTMAX, min _POSIX_RTSIG_MAX. _POSIX_SIGQUEUE_MAX, _SC_SIGQUEUE_MAX, RLIMIT_SIGPENDING. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 7/25 Real-Time Signals API 1/2 #include signal. h i n t sigqueue ( pid _t pid, i n t sig, const union sigval value ) ; i n t s i g a c t i o n ( i n t signum, const struct s i g a c t i o n act, struct sigaction oldact ) ; void sa_sigaction ( i n t signum, s ig info _ t info, void context ) ; value is union of integer (int) and pointer (void *). RTS mode handler is defined by setting SA_SIGINFO flag in struct sigaction. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 8/25 Real-Time Signals API 2/2 #include signal. h i nt s i g w a i t i n f o ( const s i g s e t _ t set, s i g i n f o _ t i n f o ) ; i nt sigtimedwait ( const s i g s e t _ t set, s i g i n f o _ t info, const struct timespec timeout ) ; Synchronous reception of signals, no need to write asynchronous handler. Blocks until a signal is received (or timeout occurs). If signal arrives between calls to sigwaitinfo(), it s handled by the given disposition. To avoid surprises, block signals with sigprocmask(). ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 9/25 Pipes Mentioned briefly before. Data in from one end, out from the other end. Two types: Unnamed and Named The most common use: stdin/stdout/stderr pipes between processes ls less type command chaining in shell ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 10/25 Unnamed pipes API #include unistd. h int pipe ( int pipefd [ 2 ] ) ; FILE popen ( const char command, const char type ) ; int pclose ( FILE stream ) ; Creates two special file descriptors: fd[0] can be read, fd[1] can be written. Pipe is unidirectional. Two-way communications need two pipes. No name or presense besides the FDs. Popen() wraps pipe creation, forking and exec()ing command. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 11/25 Unnamed pipes semantics For communication between processes within same process group: Parent process creates a pipe and gives the other end to the children. Within single process: f.ex. signal handler hack, or between threads. Pipe can hold at least PIPE_BUF bytes. (defined 4kiB on Linux, but actual default maxsize is 1MiB and it s adjustable.) Read operation: If data in buffer, returns data. If no data, blocks. If other end has been closed, returns remaining data and then returns EOF (0). Write operation: If space in buffer, writes atomically. If not enough, blocks until there is. If other end has closed, returns errno=epipe and process receives SIGPIPE. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 12/25 Named pipes (FIFO) API int m kfifo ( const char pathname, mode_t mode ) ; Pipe that is represented by a special type of file in the filesystem. Named pipe is used like a file; it has access rights, it s opened with open(), deleted with unlink(). Two-way communication still needs two pipes.. Also command-line utility of the same name. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 13/25 Named pipe semantics Usable between unrelated processes. Create with mkfifo(), delete with unlink() Open operation: Open with O_WRONLY blocks until other end has opened it the pipe for reading. Open with O_RDONLY blocks until other end has opened the pipe for writing. Read operation: If data in buffer, returns data. If no data, blocks. If other end has been closed, returns remaining data and then returns EOF (0). Write operation: If space in buffer, writes atomically. If not enough, blocks until there is. If other end has closed, returns errno=epipe and process receives SIGPIPE. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 14/25 Sockets BSD-derived communication method, basis of Internet communication. We ll stick to particular type of sockets: UNIX Domain sockets. Actual Internet communication will be covered in S Basic idea: socket is a file descriptor, with a bunch of additional calls. List of domains that Linux supporst on sockets: UNIX Domain, IPv4, IPv6, IPX, Netlink (kernel), X.25/AX.25, Appletalk and mechanisms to access raw frames of ATM and IP. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 15/25 Socket semantics 1/2 Sockets also have different types: SOCK_DGRAM is message-based, unreliable, connectionless. ( UDP ) SOCK_STREAM is stream-based, reliable, connected. ( TCP ) SOCK_RAW provides access to raw protocol frames. SOCK_RDM is message-based, reliable, connectionless. SOCK_SEQPACKET is message-based, reliable, connected SOCK_DGRAM and SOCK_STREAM are only commonly available. Others are for specialized protocols, like TIPC (Transparent Inter-Process Communication). ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 16/25 Socket semantics 2/2 Connectionless server: socket(), bind(), recvfrom()*n, close() Connectionless client: socket(), (bind(),) sendto()*n, close() Connected server: socket(), bind(), listen(), accept(), read/recv/write/send*n, close() Connected client: socket(), (bind(),) connect(), read/recv/write/send*n, close() ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 17/25 Socket API 1/2 # include sys / socket. h i nt socket ( i nt domain, i nt type, i nt p rotocol ) ; i nt bind ( i nt sockfd, const struct sockaddr addr, socklen_t addrlen ) ; i nt l i s t e n ( i nt sockfd, i nt backlog ) ; i nt accept ( i nt sockfd, struct sockaddr addr, socklen_t addrlen ) ; i nt connect ( i nt sockfd, const struct sockaddr addr, socklen_t addrlen ) ; ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 18/25 Socket API 2/2 ssize_t recv ( i n t sockfd, void buf, size_t len, i n t flags ) ; ssize_t recvfrom ( i n t sockfd, void buf, size_ t len, i n t flags, struct sockaddr src_addr, socklen_t addrlen ) ; ssize_t recvmsg ( i n t sockfd, struct msghdr msg, i n t flags ) ; ssize_t send ( i n t sockfd, const void buf, size_t len, i n t flags ) ; ssize_t sendto ( i n t sockfd, const void buf, size_ t len, i n t flags, const struct sockaddr dest_addr, socklen_t addrlen ) ; ssize_t sendmsg ( i n t sockfd, const struct msghdr msg, i n t flags ) ; sendto(fd,buf,len,flags,null,0) equals send(fd,buf,len,flags). send(fd,buf,len,0) equals write(fd,buf,len). ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 19/25 UNIX Domain Sockets Socket that communicates inside a UNIX system. Can be datagram or stream based. (Or even reliable datagram.) Address is a path, with a file (type = socket). If a regular file of the same name exists, EADDRINUSE. Can do both SOCK_DGRAM and SOCK_STREAM. struct sockaddr_un { unsigned short sun_family ; / Address family / char sun_path [ ] ; / Path name / } ; ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 20/25 UNIX Domain Sockets, the pipe edition #include sys / socket. h int socketpair ( int domain, int type, int protocol, int sv [ 2 ] ) ; Creates a pair of connected sockets, kind of like pipe(). Unlike pipes, sockets are bidirectional. Could create sockets of any type, but in practice only UNIX Domain is supported. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 21/25 UNIX Domain magic: Passing file descriptors It s possible to pass file descriptors and credentials from process to another, using sendmsg()/recvmsg(), as ancillary messages. Rights: Socket must have SO_PASSCRED. and then sendmsg() with SCM_RIGHTS. File descriptors: sendmsg() with SCM_CREDENTIALS. struct msghdr { void msg_name ; / o p t i o n a l address / socklen_t msg_namelen ; / size of address / struct iovec msg_iov ; / scatter / gather array / size_t msg_iovlen ; / # elements i n msg_iov / void msg_control ; / a n ci l la ry data, see below / si ze_t msg_controllen ; / a n ci l la ry data buffer len / i n t msg_flags ; / f l a g s on received message / } ; ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 22/25 Casualties of evolution Sockaddr types are commonly cast according to sockaddr_ family . Largest type is sockaddr_storage, that is defined to be large enough to hold all other socket types, and that is cast around to other types. But.. breaks strict-aliasing rules. C language structures have no actual relations between each other, and compiler is free to pad fields as it likes to optimize access. Solution: put structures in union. ref ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 23/25 Shared memory, classic Remember mmap()? Using MAP_SHARED flag allows mapped memory to be shared between processes, thus a IPC method. Anonymous mapped memory can be shared over fork(). File mapped memory can be shared by just mapping the same file. Synchronization? You do it. Or use some other IPC method to handle it. Cache coherency? Special situations may require manual flushing. How special? Between different CPUs. CPU and DSP on OMAP3 (N900, OpenPandora). Between CPU and GPU. Raspberry Pi, Sony PSP. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 24/25 To be continued.. Next time: Rest of the IPC. System V and POSIX IPC: Message queues. Semaphores. Shared memory. More in second period: Threads also include some IPC. ELEC-C7310 Sovellusohjelmointi Lecture 6: Inter-process Communication 25/25
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x