Using Events and Interlocks

Shared objects and task attributes provide a relatively simple means of communicating information if all the tasks involved are synchronous tasks. If the tasks in a process family are all synchronous, then only one process is executing at a time. The order in which processes access shared objects is therefore fixed.

However, in cases where asynchronous processes access the same object, the order in which they access the shared objects is not fixed. This fact can create many unexpected side effects in communication. For example, suppose two processes communicate a vital bit of information by way of a shared integer variable. How is one process to know that the other process has updated the variable so that it is now ready to be read?

The answer is that a programmer must implement flags to indicate whether a particular variable is to be accessed at this time. You can implement many types of flags. For example, a process could reset the value of a variable to zero after reading it. Another process could be designed to write a new value to the variable whenever the variable contains a zero. In this example, the zero value is being used as a flag to show that the variable has been read and is ready to have a new value written into it.

One problem with these types of flags is that the processes involved have to keep checking the flag periodically to see if it has been set. These repeated checks waste processor time. Another problem is that, between the time that one process reads the flag and the time it sets the flag, another process might have written to or read the flag. The flag is, therefore, not completely reliable.

You can avoid both of these problems by using events. An event is a special type of object that is used only for regulating the timing of asynchronous processes. A process can wait for an event to assume a certain state, without using any processor time while it waits. When the event assumes the desired state, the process resumes execution automatically.

Aside from events, the system supports objects called interlocks, which can also be used for timing purposes. Interlocks provide faster execution than events in some situations. Interlocks also make it easy for processes to detect and recover from cases where the process owning an interlock is discontinued.