Page 285 - DCAP403_Operating System
P. 285

Operating System




                    Notes                                Figure 13.9: System V IPC Message Queues
































                                   Each msqid_ds data structure contains an ipc_perm data structure and pointers to the messages
                                   entered onto this queue. In addition, Linux keeps queue modification times such as the last time

                                   that this queue was written to and so on. The msqid_ds also contains two wait queues; one for
                                   the writers to the queue and one for the readers of the message queue.
                                   Each time a process attempts to write a message to the write queue its effective user and group

                                   identifiers are compared with the mode in this queue’s ipc_perm data structure. If the process
                                   can write to the queue then the message may be copied from the process’s address space into a
                                   msg data structure and put at the end of this message queue. Each message is tagged with an

                                   application specific type, agreed between the cooperating processes. However, there may be no
                                   room for the message as Linux restricts the number and length of messages that can be written.
                                   In this case the process will be added to this message queue’s write wait queue and the scheduler
                                   will be called to select a new process to run. It will be woken up when one or more messages have
                                   been read from this message queue.
                                   Reading from the queue is a similar process. Again, the processes access rights to the write queue

                                   are checked. A reading process may choose to either get the first message in the queue regardless
                                   of its type or select messages with particular types. If no messages match this criteria the reading
                                   process will be added to the message queue’s read wait queue and the scheduler run. When a
                                   new message is written to the queue this process will be woken up and run again.

                                   13.8.5 Semaphores

                                   In its simplest form a semaphore is a location in memory whose value can be tested and set
                                   by more than one process. The test and set operation is, so far as each process is concerned,
                                   uninterruptible or atomic; once started nothing can stop it. The result of the test and set operation
                                   is the addition of the current value of the semaphore and the set value, which can be positive or
                                   negative. Depending on the result of the test and set operation one process may have to sleep
                                   until the semphore’s value is changed by another process. Semaphores can be used to implement
                                   critical regions, areas of critical code that only one process at a time should be executing.






          278                              LOVELY PROFESSIONAL UNIVERSITY
   280   281   282   283   284   285   286   287   288   289   290