Lab4 FAQ

This document contains Frequently Asked Questions, which are relevant to completing the week's lab assignment. Any Questions may be posted to cspp51081.

How to remove Message Queues, Shared Memory Seqments and Semaphores

When you create an IPC structure, the Operating System Kernel allocates memory to store and maintain the structure. This means you can reuse the structure. You should also make sure you remove the structure when you are finished, otherwise it can sit in memory a long, long time. (The Kernel cannot really garbage collect these structures. Why? The reason these structures were introduced was to allow unrelated processes to communicate; so the Kernel can have no way of knowing when user processes are finished with the IPC structures.

Removal can take place in two ways: On the command line and in your program. You can see a display of what structures your machine is maintianing by the command ipcs. This will display the key value, id, and owner of all Shared Memory segments, Message Queues and Semaphores the system is maintaining. When you find the id for the structure you want to remove, you execute one of the commands

       ipcrm msg id
       ipcrm shm id
       ipcrm sem id

Depending on which type of structure you want to remove. man 1 has details on using these functions.

To remove an IPC structure from within your program, you will use one of the ctl functions (depending on the type of structure):

       int msgctl(msqid, IPC_RMID, NULL)
       int shmctl(shmid, IPC_RMID, NULL);
       int semctl (semid, 0, IPC_RMID, 0)

These functions must be called by a process whose user ID is that of the creator of the structure. Any processes using the structure at the time it is removed, return immediately with error messages. See man 2 for the type and use of these functions: they do ALOT.

Declaring union semun for use with semctl

This posting is if you recieve the following message, when implementing semaphores:

       union semun my_semun;       // a declaration in your program

COMPILER RESPONSE:
       storage size of `my_semun' isn't known

The problem is that although the fourth argument to semctl is of type union semun

       int semctl ( int semid, int semnun, int cmd, union semun arg);

there is actually no type union semun defined. To remedy this, you will have to define this union type in your program, before you enter main:

union semun {
      int val; /* used for SETVAL only */
      struct semid_ds *buf; /* for IPC_STAT and IPC_SET */
      ushort *array; /* used for GETALL and SETALL */
};