Docsity

Inter-process Communication, Study notes of Operating Systems

Partial preview of the text

Download Inter-process Communication and more Study notes Operating Systems in PDF only on Docsity!

(System V)

Inter-process

Communication

Emmanuel Fleury

B1-

[email protected]

Outline

Inter-Process Communication

Semaphores

Message Queues

Shared Memory Segments

What is (System V) IPC? ā— What is IPC?

  • (^) All processes are active at communication time
  • (^) Processes resides in different protected domains ā— What is NOT IPC?
  • (^) Persistent data communication (files, pipes)
  • (^) Process/Kernel communication (signals) IPC is live communication between processes!

What is (System V) IPC? ā— Semaphores (sys/sem.h) ā— Message Queues (sys/msg.h) ā— Shared Memory Segments (sys/shm.h) Process A Process B

Kernel

IPC Three IPC mechanisms (sys/ipc.h):

Creation of an IPC Process A Process B

Kernel

IPC 22937 My key is 20 , I want an ID! Your ID is 22937!

  1. The user give a key
  2. The kernel create an IPC object if necessary
  3. The kernel return an ID

Creation of an IPC Process A Process B

Kernel

IPC 22937 My key is 20 , I want an ID! Your ID is 22937!

  1. The user give a key
  2. The kernel create an IPC object if necessary
  3. The kernel return an ID

ipcrm (IPC Remove) [fleury@hermes]$ ipcs Shared Memory Segments key shmid owner perms bytes nattch status 0x00000000 98305 fleury 600 393216 2 dest Semaphore Arrays key semid owner perms nsems Message Queues key msqid owner perms used bytes messages [fleury@hermes]$ ipcrm m 98305 [fleury@hermes]$ ipcs Shared Memory Segments key shmid owner perms bytes nattch status Semaphore Arrays key semid owner perms nsems Message Queues key msqid owner perms used bytes messages ipcrm is used to remove IPC objects from your system

Creation of an IPC ā— key:

  • (^) An integer
  • (^) IPC_PRIVATE: Create a new key and a new IPC ā— flags:
  • (^) IPC_CREAT: Create entry if key does not exist
  • (^) IPC_EXCL: Fail if key exists
  • (^) IPC_NOWAIT: Fail if request must wait

Note: The choice of IPC_PRIVATE

was unfortunate. IPC_NEW would

better fit to this keyword.

#include <sys/types.h>

#include <sys/ipc.h>

int XXXget(key_t key, int flags);

Creating an IPC Object #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/types.h> int main() { key_t key = IPC_PRIVATE; int msqid; if ((msqid = msgget(key, 0)) == 1) { perror("myipc"); exit(1); } printf("The key is %i\n", key); printf("The identifier is %i\n", msqid); exit(0); } [fleury@hermes]$ ./myipc The key is 0 The identifier is 262144 [fleury@hermes]$ ipcs q Message Queues key msqid owner perms used bytes messages [fleury@hermes]$ su c 'ipcs q' Message Queues key msqid owner perms used bytes messages 0x00000000 262144 fleury 0 0 0 Note: The permissions are not set properly !!!

Ownership & Access Policy ā— Each IPC has an ownership and access data (ipc_perm):

  • (^) uid_t uid: Owner's user ID
  • (^) gid_t gid: Owner's group ID
  • (^) uid_t cuid: Creator's user ID
  • (^) gid_t cgid: Creator's group ID
  • (^) mode_t mode: Read/write permissions ā— At creation time, the values are:
  • (^) uid_t uid: Effective user ID of the creating process
  • (^) gid_t gid: Effective group ID of the creating process
  • (^) uid_t cuid: Effective user ID of the creating process
  • (^) gid_t cgid: Effective group ID of the creating process
  • (^) mode_t mode: Read/write permissions from the umask of the creating process

Creating an IPC (given key) #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/types.h> int main() { int msqid; if ((msqid = msgget(20, 0666)) == 1) { perror("myipc"); exit(1); } printf("The identifier is %i\n", msqid); exit(0); } [fleury@hermes]$ ./myipc myipc: No such file or directory The identifier is 1 Note: By default, a new key is not created, IPC_CREAT must be specified

Creating an IPC (given key)

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <sys/types.h>

int main() {

int msqd;

if ((msqid = msgget(20, 0666 | IPC_CREAT)) == 1) {

perror("myipc");

exit(1);

printf("The identifier is %i\n", msqid);

exit(0);

[fleury@hermes]$ ./myipc The identifier is 425984 [fleury@hermes]$ ipcs q Message Queues key msqid owner perms used bytes messages 0x00000014 425984 fleury 666 0 0

Deleting an IPC (given key) #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/types.h> int main() { int msqid; if ((msqid = msgget(key, 0666 | IPC_CREAT)) == 1) { perror("myipc"); exit(1); } printf("The identifier is %i\n", msqid); if ((msgctl(msqid, IPC_RMID, NULL) == 1)) { perror("myipc"); exit(1); } exit(0); } [fleury@hermes]$ ./myipc The identifier is 688128 [fleury@hermes]$ ipcs q Message Queues key msqid owner perms used bytes messages [fleury@hermes]$

Getting an IPC status #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/types.h> int main() { int msqid; struct msqid_ds *status; if ((msqid = msgget(key, 0666 | IPC_CREAT)) == 1) { perror("myipc"); exit(1); } printf("The identifier is %i\n", msqid); if ((msgctl(msqid, IPC_STAT, NULL) == 1)) { perror("myipc"); exit(1); } printf("Messages in queue: %i\n", (int) status.msg_qnum); printf("Bytes in queue: %i\n", (int) status.msg_qbytes); printf("Last process sending: %i\n", (int) status.msg_lspid); printf("Last process receiving: %i\n", (int) status.msg_lrpid); exit(0); } [fleury@hermes]$ ./myipc The identifier is 65536 Messages in queue: 0 Bytes in queue: 0 Last process sending: 0 Last process receiving: 0 [fleury@hermes] ipcs q Message Queues key msqid owner perms used bytes messages 0x00000014 65536 fleury 666 0 0