APUE(Interprocess Communication) [Ch. 14]

APUE(Interprocess Communication) [Ch. 14]

컴퓨터 특강 (UNIX System Programming) APUE(Interprocess Communication) [Ch. 14] 2006년 봄학기 문양세 강원대학교 컴퓨터과학과 Contents APUE (Interprocess Communication Pipes FIFOs System V IPC • Message Queues • Shared Memory •Semaphores UNIX System Programming Page 2 by Yang-Sae Moon 1 IPC using Pipes APUE (Interprocess Communication IPC using regular files • unrelated processes can share •fixed size • lack of synchronization IPC using pipes • for transmitting data between related processes • can transmit an unlimited amount of data • automatic synchronization on open() UNIX System Programming Page 3 by Yang-Sae Moon Pipes in a UNIX Shell APUE (Interprocess Communication In a UNIX shell, the pipe symbol is: | (the vertical bar) In a shell, UNIX pipes look like: $ ls -alg | more • where the standard output of the program at the left (i.e., the producer) becomes the standard input of the program at the right (i.e., the consumer). We can have longer pipes: $ pic paper.ms | tbl | eqn | ditroff -ms UNIX System Programming Page 4 by Yang-Sae Moon 2 Example (1/2) APUE (Interprocess Communication $ who | sort whopipe sort write pointer of read pointer of another process one process UNIX System Programming Page 5 by Yang-Sae Moon Example (2/2) APUE (Interprocess Communication UNIX System Programming Page 6 by Yang-Sae Moon 3 IPC using Pipes APUE (Interprocess Communication Data transmitting • data is written into pipes using the write() system call • data is read from a pipe using the read() system call • automatic blocking when full or empty Types of pipes • (unnamed) pipes • named pipes (FIFOs) UNIX System Programming Page 7 by Yang-Sae Moon Pipes (1/4) APUE (Interprocess Communication In UNIX, pipes are the oldest form of IPC. Limitations of Pipes: • Half duplex (data flows in one direction) • Can only be used between processes that have a common ancestor (Usually used between the parent and child processes) • Processes cannot pass pipes and must inherit them from their parent • If a process creates a pipe, all its children will inherit it UNIX System Programming Page 8 by Yang-Sae Moon 4 Pipes (2/4) APUE (Interprocess Communication #include <unistd.h> int pipe(int fd[2]) Returns: 0 if OK, -1 on error Two file descriptors are returned through the fd argument • fd[0]: can be used to read from the pipe, and • fd[1]: can be used to write to the pipe 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 The pipe() system call gives parent-child processes a way to communicate with each other. UNIX System Programming Page 9 by Yang-Sae Moon Pipes (3/4) APUE (Interprocess Communication parent Æ child: parent Å child: parent closes fd[0] parent closes fd[1] child closes fd[1] child closes fd[0] parent child parent child fd[1] fd[0] fd[0] fd[1] pipe pipe kernel kernel UNIX System Programming Page 10 by Yang-Sae Moon 5 Pipes (4/4) APUE (Interprocess Communication Read from a pipe with write end closed: (fd[1]이 close된경우) • returns 0 to indicate EOF Write to a pipe with read end closed: (fd[0]가 close된경우) •SIGPIPE generated, • write() returns error (errno == EPIPE) UNIX System Programming Page 11 by Yang-Sae Moon 예제: pipe.c (1/2) APUE (Interprocess Communication #include <stdio.h> // pipe.c #define READ 0 #define WRITE 1 char* phrase = "Stuff this in your pipe and smoke it"; main( ) { int fd[2], bytesRead; char message[100]; pipe(fd); if (fork() == 0) { // child close(fd[READ]); write(fd[WRITE], phrase, strlen(phrase)+1); fprintf(stdout, "[%d, parent] write completed.\n", getpid()); close(fd[WRITE]); } else { // parent close(fd[WRITE]); bytesRead = read(fd[READ], message, 100); fprintf(stdout, "[%d, child] read completed.\n", getpid()); printf("Read %d bytes: %s\n", bytesRead,message); close(fd[READ]); } } UNIX System Programming Page 12 by Yang-Sae Moon 6 예제: pipe.c (2/2) APUE (Interprocess Communication 실행 결과 UNIX System Programming Page 13 by Yang-Sae Moon Contents APUE (Interprocess Communication Pipes FIFOs System V IPC • Message Queues • Shared Memory •Semaphores UNIX System Programming Page 14 by Yang-Sae Moon 7 FIFOs (1/3) APUE (Interprocess Communication Pipes can be used only between related processes. (e.g., parent and child processes) FIFOs are "named pipes" that can be used between unrelated processes. A type of file • stat.st_mode == FIFO • Test with S_ISFIFO() macro UNIX System Programming Page 15 by Yang-Sae Moon FIFOs (2/3) APUE (Interprocess Communication #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode); Returns: 0 if OK, -1 on error Creating FIFOs is similar to creating a file. • pathname: filename • mode: permissons, same as for open() function Using a FIFO is similar to using a file. • we can open, close, read, write, unlink, etc., to the FIFO. UNIX System Programming Page 16 by Yang-Sae Moon 8 FIFOs (3/3) APUE (Interprocess Communication if FIFO opened without O_NONBLOCK flag • an open for read-only blocks until some other process opens the FIFO for writing • an open for write-only blocks until some other process opens the FIFO for reading if O_NONBLOCK is specified (nonblocking) • an open for read-only returns immediately if no process has the FIFO open for writing • an open for write-only returns an error (errno=ENXIO) if no process has the FIFO open for reading Like a pipe, if we write to a FIFO that no process has open for reading, the signal SIGPIPE is generated. When the last writer for a FIFO closes the FIFO, an end of file (EOF) is generated for the reader of the FIFO. UNIX System Programming Page 17 by Yang-Sae Moon Uses of FIFOs APUE (Interprocess Communication Used by shell commands to pass data from one shell pipeline to another, without creating intermediate files. Used in client-server application to pass data between clients and server. UNIX System Programming Page 18 by Yang-Sae Moon 9 Using FIFOs to Duplicate Output Streams APUE (Interprocess Communication tee(1) copies its standard input to both its standard output and to the file named on its command line. $ mkfifo fifo1 $ prog3 < fifo1 & $ prog1 < infile | tee fifo1 | prog2 fifo1 prog3 infile prog1 tee prog2 UNIX System Programming Page 19 by Yang-Sae Moon An Example using a FIFO APUE (Interprocess Communication UNIX System Programming Page 20 by Yang-Sae Moon 10 Client-Server Communication Using a FIFO APUE (Interprocess Communication Server creates a “well-known” FIFO to communicate with clients. client write request . well-known read server FIFO request write request client Problem: Server can't reply clients using a single “well-known” FIFO UNIX System Programming Page 21 by Yang-Sae Moon Contents APUE (Interprocess Communication Pipes FIFOs System V IPC • Message Queues • Shared Memory •Semaphores UNIX System Programming Page 22 by Yang-Sae Moon 11 System V IPC APUE (Interprocess Communication Message Queues • Send and receive amount of data called “messages”. • The sender classifies each message with a type. Shared Memory • Shared memory allows two or more processes to share a given region of memory. • Readers and writers may use semaphore for synchronization. Semaphores • Process synchronization and resource management • For example, a semaphore might be used to control access to a device like printer. UNIX System Programming Page 23 by Yang-Sae Moon Identifiers & Keys APUE (Interprocess Communication Identifier: each IPC structure has a nonnegative integer Key: when creating an IPC structure, a key must be specified (key_t) id = xxxget(key, …) How to access the same IPC? Æ key in a common header • Define a key in a common header • Client and server agree to use that key • Server creates a new IPC structure using that key • Problem when the key is already in use − (msgget, semget, shmget returns error) − Solution: delete existing key, create a new one again! UNIX System Programming Page 24 by Yang-Sae Moon 12 IPC System Calls APUE (Interprocess Communication msg/sem/shm get • Create new or open existing IPC structure. • Returns an IPC identifier msg/sem/shm ctl • Determine status, set options and/or permissions • Remove an IPC identifier msg/sem/shm op • Operate on an IPC identifier • For example(Message queue) − add new msg to a queue (msgsnd) − receive msg from a queue (msgrcv) UNIX System Programming Page 25 by Yang-Sae Moon Permission Structure APUE (Interprocess Communication ipc_perm is associated with each IPC structure. Defines the permissions and owner. 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 */ ulong seq; /* slot usage sequence number */ key_t key; /* key */ }; UNIX System Programming Page 26 by Yang-Sae Moon 13 Message Queues (1/2) APUE (Interprocess Communication Linked list of messages • Stored in kernel • Identified by message queue identifier (in kernel) msgget • Create a new queue or open exiting queue. msgsnd • Add a new message to a queue msgrcv • Receive a message from a queue • Fetching order: based on type UNIX System Programming Page 27 by Yang-Sae Moon Message Queues (2/2) APUE (Interprocess Communication Each queue has a structure struct msqid_ds { struct ipc_perm msg_perm; struct msg *msg_first; /* ptr to first msg on queue */ struct msg *msg_last; /* ptr to last msg on queue */ ulong msg_cbytes; /* current # bytes on queue */ ulong msg_qnum; /* # msgs on queue */ ulong msg_qbytes; /* max # bytes on queue */ pid_t msg_lspid; /* pid of last msgsnd() */ pid_t msg_lrpid; /* pid of last msgrcv() */ time_t msg_stime; /* last-msgsnd() time */ time_t msg_rtime; /* last-msgrcv() time */ time_t msg_ctime; /* last-change time */ }; We can get the structure using msgctl() function.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    26 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us