Management process understanding and use of management process to realize producer and consumer issues

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
  1. Local variables can only be accessed by procedures in the supervisor
  2. The process enters the monitor by calling a process of the monitor
  3. 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)
    Insert picture description here
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) ;
}


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44722674/article/details/111570161