120 likes | 257 Vues
This project involves creating two built-in shell commands, 'who' and 'msg', utilizing interprocess communication techniques such as shared memory and semaphores. The 'who' command will list the IDs of shell processes in the same shell environment, while the 'msg' command will facilitate message passing between processes. The design requires careful handling of shared memory segmentation, semaphore operations to avoid race conditions, and proper cleanup of resources upon shell exit. Focus on effective communication and synchronization between multiple processes is essential.
E N D
CS230 System Programming Project #3 : IPC (Interprocess Communication)
1. Specification(1/2) • Develop ‘who’ command using ‘shared memory’ • List IDs of shell processes – the same shell • Unlink shared memory & semaphore when exit the shell • You need to think about structure to save process’ pid
1. Specification(2/2) • Develop built-in command ‘msg’ using shared memory & signal • msg pid ‘Message’ • Use single shared memory to send and receive message • Send a signal to receiving shell to consume the message • Issue : semaphore to deal a race condition • Do not use busy-waiting policy
2. Interprocess Communication • IPC : communication between processes • Shared memory • Message queuing • Semaphore • cf. Socket
3. Shared Memory(1/3) • Memory space that some processes use together Process I Process II Shared memory
3. Shared Memory(2/3) • shmget • Make shared memory segment • Int shmget(key_t key, int size, int shmflg); • shmat • Get address of shared memory segment • Int shmat(int shmid, char *shmaddr, int shmflg);
3. Shared Memory(3/3) • shmdt • Unload shared memory segment • Be careful with unload because there can be other process who is dealing with segment • int shmdt(char *shmaddr);
4. Semaphore (1/4) • Certain condition : • Two processes want to write something on same position of shared memory at same time-> collision! • Use semaphore to give the permission to only one process
4. Semaphore(2/4) • P() : get semaphore to contact critical point • V() : release semaphore after using critical point
4. Semaphore(3/4) • semget • Create semaphore • int semget(key_t key, int nsems, int semflg); • semop • Get or release semaphrore • int semop(int semid, struct sembuf *sops, unsigned nsops);
4. Semaphore(4/4) • semctl • Control semaphore • Int semctl(int semid, int semnum, int cmd, union semun arg);
5. ipcs, ipcrm • Deal IPC control • shared memory • Semaphore • Message queue • ipcs : show using IPC control • Ex : ipcs –m • ipcrm : remove IPC control • Ex : ipcrm –M shm_key