Upload
sophia-hunt
View
214
Download
0
Embed Size (px)
Citation preview
Slide 9-1
Copyright © 2004 Pearson Education, Inc.
Inter process Communication Mechanisms
Allow arbitrary processes to exchange data and synchronize execution
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-2
Copyright © 2004 Pearson Education, Inc.
• Inter process communication (IPC) is the transfer of data among processes. Some of the examples are
• A Web browser may request a Web page from a Web server, which then sends HTML data. This transfer of data usually uses sockets in a telephone-like connection.
• one may want to print the filenames in a directory using a command such as ls | lpr. The shell creates an ls process and a separate lpr process, cothe two with a pipe, represented by the “|” symbol.A pipe permits one-way communication between two related processes.The ls process writes data into the pipe, and the lpr process reads data from the pipennecting
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-3
Copyright © 2004 Pearson Education, Inc.
IPC Mechanisms
Info to beshared
Info copyMessage
OS IPCMechanismAddress Space for p0 Address Space for p1
Sharing of Information can be in User space Kernel space
Slide 9-4
Copyright © 2004 Pearson Education, Inc.
Main IPC methods
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-5
Copyright © 2004 Pearson Education, Inc.
IPC mechanisms
• System V IPC– Shared memory– Semaphore– Message queue
• Signals
• Pipes (named pipe also)
• Sockets
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-6
Copyright © 2004 Pearson Education, Inc.
System V IPC• System V IPC was first introduced in SVR2, but
is available now in most versions of unix
– Message Queues represent linked lists of messages, which can be written to and read from
– Shared memory allows two or more processes to share a region of memory, so that they may each read from and write to that memory region
– Semaphores synchronize access to shared resources by providing synchronized access among multiple processes trying to access those critical resources.
Slide 9-7
Copyright © 2004 Pearson Education, Inc.
SYS V IPC
Each mechanism contains Table Key Get
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-8
Copyright © 2004 Pearson Education, Inc. COP5570 – Advanced Unix Programming
• Identifiers and Keys– Each IPC structure (message queue, semaphore,
or share memory segment) is identified by a nonnegative integer identifier.
• Similar to a file descriptor, but can be a large value.
– A key must be specified when creating an IPC structure.
• Kernel convert the key to the identifier.• All process access to an IPC structure must have a
way to specify the same key?
Slide 9-9
Copyright © 2004 Pearson Education, Inc.
Shares the memory in user spaceShared memory is the special
address range in the address range of a process
Provides efficient access to large areas of memory .
Other process can attach this shared memory segment into their own address space (Virtual address space)
Slide 9-11
Copyright © 2004 Pearson Education, Inc.
shmget Create / retrieve
shmat Attach to the virtual address space
shmdt Detach form virtual address space
shmctl Manipulation of various parameters
Slide 9-12
Copyright © 2004 Pearson Education, Inc.
Syntax
Example
#include<sys/shm.h>int shmget(key_t key, size_t size, int shmflg);
shmid = shmget((key_t)1234,4,IPC_CREAT | IPC_EXCL) ;
if(shmid < 0){
printf("Shared memory already exists\n") ;
shmid = shmget((key_t)1234,4,0666) ;}
Slide 9-13
Copyright © 2004 Pearson Education, Inc.
Syntax:
Example
void *shmat(int shm_id, const void *shm_addr, int shmflg);
memory = shmat(shmid,NULL,0) ;if(memory == NULL ){ printf("attaching to the shared memory
has failed\n") ; exit(0) ;}
Slide 9-14
Copyright © 2004 Pearson Education, Inc.
Syntax:
Exampleint shmdt(const void*memory) ;
retval = shmdt(memory) ;if(retval < 0){ printf("process 1 could not detach the
memory\n") ; exit(0) ;}
Slide 9-15
Copyright © 2004 Pearson Education, Inc.
Syntax:
The shmid_ds structure has at least the following members:struct shmid_ds {
uid_t shm_perm.uid;uid_t shm_perm.gid;mode_t shm_perm.mode;
}
int shmctl(int shm_id, int command, struct shmid_ds *buf);
Slide 9-16
Copyright © 2004 Pearson Education, Inc.
retval = shmctl(shmid,IPC_RMID,NULL) ;if(retval < 0){ printf("removing the shared memory had failed\n") ;}
Command Description
IPC_STAT To retrieve the values associated with the shared memory.
IPC_SET Sets the values
IPC_RMID Deletes the shared memory segment.
Slide 9-17
Copyright © 2004 Pearson Education, Inc.
debugging• ipcs –shm
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 1627649 user 640 25600 0
• If this memory segment was erroneously left behind by a program, you can use the ipcrm command to remove it.
• ipcrm shm 1627649
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-18
Copyright © 2004 Pearson Education, Inc.
Pros Easy to access as it is in user space Fastest IPC mechanism
Cons Synchronization mechanism is not there.
Slide 9-19
Copyright © 2004 Pearson Education, Inc.
For synchronization
Different from posix semaphores
Between two unrelated processes
A number with post and signal operations
Slide 9-20
Copyright © 2004 Pearson Education, Inc.
Operations
• The semaphore system calls in System V are a generalization of Dijkstra’s P and V opeartions
• P(), V() . P and V stand for Dutch words "Proberen", to test, and "Verhogen", to increment.
• function V(semaphore S) { S ← S +1 }
• function P(semaphore S) { While (S= =0){ wait}; S=S-1; }
Slide 9-21
Copyright © 2004 Pearson Education, Inc.Copyright ©: Nahrstedt, Angrave,
Abdelzaher, Kravets, Gupta 21
Slide 9-22
Copyright © 2004 Pearson Education, Inc.
Free from busy wait typedef struct {
int value;queue tlist;
} semaphore;
sem_wait (semaphore *S) { // Must be executed atomicallyS->value--;if (S->value < 0) {
add this process to S->tlist;block();
}}sem_signal (semaphore *S) {// Must be executed atomically
S->value++;if (S->value <= 0) {
remove thread t from S->tlist;wakeup(t); } }
Slide 9-23
Copyright © 2004 Pearson Education, Inc.
Types Binary semaphores▪ Mutually exclusive binary semaphores – initial
value is 1▪ Signaling semaphores – initial value (n) is 0
Counting semaphores▪ Depends on the number of resources – initial
value is ‘n’ Operation Importance
P – Wait If ‘n’ > 0 , decrement ‘n’. If ‘n’ = 0, suspend execution of this process.
V – Signal If some other process has been suspended waiting for ‘n’, make it resume execution.If no process is suspended waiting for ‘n’, increment ‘n’
Slide 9-24
Copyright © 2004 Pearson Education, Inc.
semget Create / retrieve
semop Wait and signal operations use the same
call with different argumentssemctl
Manipulation of various parameters, controlling the semaphore
Slide 9-25
Copyright © 2004 Pearson Education, Inc.
Syntax
Example
#include<sys/sem.h>int semget(key_t key, int num_sems, int sem_flags);
semid = semget((key_t)1234,1,IPC_CREAT | IPC_EXCL) ;
if(semid < 0){
printf(“Semaphore already exists\n") ;semid = semget((key_t)1234,1,0666) ;
}
Slide 9-26
Copyright © 2004 Pearson Education, Inc.
Syntax:
Example
int semctl(int sem_id, int sem_num, int command, ...);union semun {
int val; /* Value for SETVAL */struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */unsigned short *array; /* Array for GETALL, SETALL */struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */} ;union semun sun ;sun.val = 1 ;retval = semctl(semid,0,SETVAL,sun) ; // init ialize the semaphore
Slide 9-27
Copyright © 2004 Pearson Education, Inc.
P() V() operations
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-28
Copyright © 2004 Pearson Education, Inc.
Message Queue
• Message queue.
– A linked list of messages stored within the kernel and identified by a message queue identifier.
• Every message has a type field, and a nonnegative length, and the actual data bytes.
• Msgsnd puts a message at the end of the queue
• Msgrcv gets a message, may not follow FIFO order (can be based on type)
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-29
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
Refined IPC Mechanism• OS manages the mailbox space
• More secure message system
Info to beshared
Info to beshared Info copyInfo copy
Address Space for p0 Address Space for p1
MessageMessageMessageMessageMessageMessage
Mailbox for p1
send function receive function
OS Interfacesend(… p1, …); receive(…);
Slide 9-30
Copyright © 2004 Pearson Education, Inc.
• Message queue operationsInt msgget(key_t, int flag)Int msgctl(int msgid, int cmd, struct msgid_ds *buf)Int msgsnd(int msgid, const void *ptr, size nbytes, int flag);Int msgrcv(int msgid, void *ptr, size_t nbytes, long type, int
flag);
• Message queue has a lot of problems:– Removal is handled in strange way– Limited by global resources: programs that use it can
be affected by other processes using message queues
Operating Systems: A Modern Perspective, Chapter 9
Slide 9-31
Copyright © 2004 Pearson Education, Inc.
msgget Returns a message descriptor to be used
by other system callsmsgsnd
Sends a message msgrcv
Receives a messagemsgctl
Controls the message queue
Slide 9-32
Copyright © 2004 Pearson Education, Inc.
Syntax
Example
#include<sys/msg.h>Int msgget(key_t key, int msgflg);
msgid = msgget((key_t)1234,IPC_CREAT | IPC_EXCL) ;if(msgid < 0){
printf(“Message queue already exists\n") ;msgid = msgget((key_t)1234,0666) ;
}
Slide 9-33
Copyright © 2004 Pearson Education, Inc.
Syntax:
Example
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
struct msgbuf {long mtype; /* message type, must be > 0 */char mtext[5]; /* message data */};
mbuf.mtype =1 ;strcpy(mbuf.mtext,"hello") ;retval = msgsnd(msgid,&mbuf,5,0) ;
Slide 9-34
Copyright © 2004 Pearson Education, Inc.
Syntax:
Example
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int
msgflg);
retval = msgrcv(msgid,&mbuf,5,1,0) ;if(retval < 0){perror("msgrcv: ") ;}
Slide 9-35
Copyright © 2004 Pearson Education, Inc.
Syntax:
The msqid_ds structure has at least the following members:struct msqid_ds {
uid_t msg_perm.uid;uid_t msg_perm.gidmode_t msg_perm.mode;}
Int msgctl(int msg_id, int command, struct shmid_ds *buf);
Slide 9-36
Copyright © 2004 Pearson Education, Inc.
retval = msgctl(msgid,IPC_RMID,NULL) ;if(retval < 0){ printf("removing the message queue had failed\n") ;}
Command Description
IPC_STAT To retrieve the values associated with the shared memory.
IPC_SET Sets the values
IPC_RMID Deletes the shared memory segment.
Slide 9-37
Copyright © 2004 Pearson Education, Inc.
Common fields (header files and the structure) to be declared by the user.#include<string.h>
#include<stdio.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>
struct msgbuf {long mtype; /* message type, must be >
0 */char mtext[5]; /* message data */ };
Slide 9-38
Copyright © 2004 Pearson Education, Inc.
int main() {int msgid, retval ;struct msgbuf mbuf ;
if(msgid = msgget((key_t)5678,IPC_CREAT | IPC_EXCL) ) < 0); {printf("msgget already created\n") ;msgid = msgget((key_t)5678,0666) ; }
mbuf.mtype =1 ;strcpy(mbuf.mtext,"hello") ; // trying to send hello If( (retval = msgsnd(msgid,&mbuf,5,0)) <0){
perror("msgsnd: ") ; }
return 0 ;}
Slide 9-39
Copyright © 2004 Pearson Education, Inc.
int main() {int msgid, retval ;struct msgbuf mbuf ;
If((msgid = msgget((key_t)5678,IPC_CREAT | IPC_EXCL))<0) {printf("msgget already created\n") ;msgid = msgget((key_t)5678,0666) ;
}
if((retval = msgrcv(msgid,&mbuf,5,1,0)) < 0){perror("msgrcv: ") ;
}
printf("message from process 1 is %s\n",mbuf.mtext) ;
return 0 ;}
Slide 9-40
Copyright © 2004 Pearson Education, Inc.
Pros Lies in the kernel space Doesn’t require any external
synchronization.
Cons Limit in the size of data to be sent Over all size limit in the complete
system
Slide 9-41
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
UNIX Pipes
Info to beshared
Info to beshared Info copyInfo copy
Address Space for p1
pipe for p1 and p2
write function read function
System Call Interfacewrite(pipe[1], …); read(pipe[0]);
Slide 9-42
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
UNIX Pipes (cont)
• The pipe interface is intended to look like a file interface– Analog of open is to create the pipe– File read/write system calls are used to
send/receive information on the pipe
• What is going on here?– Kernel creates a buffer when pipe is created– Processes can read/write into/out of their
address spaces from/to the buffer– Processes just need a handle to the buffer
Slide 9-43
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
UNIX Pipes (cont)• File handles are copied on fork
• … so are pipe handlesint pipeID[2];. . .pipe(pipeID);. . .if(fork() == 0) { /* the child */ . . . read(pipeID[0], childBuf, len); <process the message>; . . .} else { /* the parent */ . . . write(pipeID[1], msgToChild, len); . . .}
Slide 9-44
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
UNIX Pipes (cont)• The normal write is an asynchronous op
(that notifies of write errors)
• The normal read is a blocking read
• The read operation can be nonblocking#include <sys/ioctl.h>. . .int pipeID[2];. . .pipe(pipeID);ioctl(pipeID[0], FIONBIO, &on);. . .read(pipeID[0], buffer, len);if(errno != EWOULDBLOCK) { /* no data */} else { /* have data */
Slide 9-45
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9
Information Flow Through UNIX Pipes
Info to beshared
Info to beshared Info copyInfo copy
Address Space for p1
pipe for p1 and p2
write function read function
System Call Interfacewrite(pipe[1], …); read(pipe[0]);