the reason why the condition variable is needed is essentially the uncertainty of the program execution order.
the monitor only ensures that only one process is active in the pipeline at the same time, that is, the operations defined in the pipeline are called by only one process at the same time (implemented by the compiler). However, this does not guarantee that the processes are executed in the designed order, so it is necessary to set the condition variable to block the processes that enter the pipeline but cannot continue to execute.
It can also be said that due to the uncertainty of the execution order of the process, the order of the processes in the entry sequence is not necessarily what we want, and the condition variable is used to operate the entry sequence. With the condition variable, we can make the process that entered the pipeline ahead of its predecessor suspend itself and return to the entry sequence to queue up again.
specifically, take the producer-consumer problem (also known as the finite buffer problem) as an example. If we simply use the integer variable count to record the number of data items in the buffer instead of using the semaphore, we will use the system call sleep () to block the process and wakeup the process.
when the process synchronization is realized by using the pipeline, when a process obtains critical resources through the pipeline request but fails to meet them, the pipeline calls the wait primitive to make the process wait and put it on the waiting queue. Only when another process accesses and releases the resource, the pipeline calls the signal primitive again to wake up the queue leader process.
However, consider the case that when a process calls a pipeline, it is blocked or suspended while in the pipeline until the reason for blocking or suspension is released; In the meantime, if the process does not release the pipeline, other processes cannot enter the pipeline and are forced to wait for a long time.