2.17 Pipeline
Guan Cheng is a programming language structure, using a centralized process synchronization method, providing the same functions as semaphores, but easier to control
concept
Consists of a shared data structure (abstraction of resources) and a set of operations performed for concurrent processes. This set of operations can synchronize processes and change data in the monitor
Features
- Local variables can only be accessed by procedures in the supervisor
- The process enters the monitor by calling a process of the monitor
- Whenever there is only one process executing in the monitor, the monitor can be called by other processes only when the process ends or is blocked (non-preemptive)
The mutual exclusion realized by the monitor is a kind of mutual exclusion that depends on the data structure, and the monitor is equivalent to a mutually exclusive function
Synchronization of tubes
Use condition variables (local variables) to
manipulate two functions of condition variables:
- cwait©: The process is blocked on c, at this time the monitor can be called by other processes
- csignal©: Resume a process blocked on c
Management solves producer and consumer problems
/* program producerconsumer * /
monitor boundedbuffer;
char buffer [N]; /*space for N items */
int nextin,nextout; /*buffer pointers */
int count; /* number of items in buffer */
cond notfull,notempty; /*condition variables for synchronization */
//定义append()
void append (char x){
if (count == N) cwait ( notfull); /*buffer is full; avoid overflow * /
buffer[nextin] = x;
nextin = (nextin + 1) % N;
count++;
/*one more item in buffer */
csignal(notempty); /*resume any waiting consumer * /
}
//定义take()
void take (char x){
if (count == 0 ) cwait(notempty); /* buffer is empty; avoid underflow */
x = buffer[ nextout];
nextout = ( nextout + 1)% N;
count--; /*one fewer item in buffer */
csignal (notfull); /*resume any waiting producer * /
}
{ /* monitor body */
nextin = 0; nextout = 0; count = 0; /* buffer initially empty */
}
void producer()
{
char x;
while (true) {
produce(x);
append(x);
}
}
void consumer
{
char x;
while (true) {
take(x);
consume(x);
}
}
void main
{
parbegin (producer,
consumer) ;
}