Skip to content

2.16 信号量实现同步互斥

Giovanna

About 150 wordsLess than 1 minute

2024-08-31

利用信号量实现进程互斥

利用信号量实现进程互斥的进程可描述如下:

P1:
Semaphore mutex=1;
while(1)
{
    P(mutex);
    Critical Section; // 临界区
    V(mutex);
    ...;
}

P2:
Semaphore mutex=1;
while(1)
{
    P(mutex);
    Critical Section; // 临界区
    V(mutex);
    ...;
}

利用信号量实现前趋关系

image.png

Semaphore a=0,b=0,c=0,d=0,e=0,f=0,g=0;
S1; V(a); V(b);

P(a); S2; V(c); V(d);

P(b); S3; V(e);

P(c); S4; V(f);

P(d); S5; V(g);

P(e); P(f); P(g); S6;