Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
Outline
Inter-Process Communication
Semaphores
Message Queues
Shared Memory Segments
What is (System V) IPC? ā What is IPC?
What is (System V) IPC? ā Semaphores (sys/sem.h) ā Message Queues (sys/msg.h) ā Shared Memory Segments (sys/shm.h) Process A Process B
IPC Three IPC mechanisms (sys/ipc.h):
Creation of an IPC Process A Process B
IPC 22937 My key is 20 , I want an ID! Your ID is 22937!
Creation of an IPC Process A Process B
IPC 22937 My key is 20 , I want an ID! Your ID is 22937!
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:
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):
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)
[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