實現進程互斥的軟件方法
Peterson算法
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define true 1
#define false 0
typedef int bool;
bool flag[2];
int turn;
void procedure0()
{
while(true)
{
flag[0] = true; //表示進程0有進程臨界區的意願
turn = 1;
while(flag[1] && turn == 1)//當另一個進程想要使用臨界區並且擁有訪問權限,則當前進程一直忙等待,退出while循環的條件就是,要麼另一個線程
//不想要使用臨界區,要麼此線程擁有訪問權限。
{
sleep(1);
printf("procedure0 is waiting!\n");
}
//critical section
printf("procedure0 is in critical section\n");
//end critical section
flag[0] = false;
}
}
void procedure1()
{
while(true)
{
flag[1] = true;
turn = 0;
while(flag[0] && turn == 0)
{
sleep(1);
printf("procedure1 is waiting!\n");
}
//critical section
printf("procedure1 is in critical section\n");
//end critical section
flag[1] = false;
}
}
void main()
{
pthread_t t1,t2;
flag[0] = flag[1] = false;
int err;
turn = 0;
err = pthread_create(&t1,NULL,(void*)procedure0,NULL);
if(err != 0) exit(-1);
err = pthread_create(&t2,NULL,(void*)procedure1,NULL);
if(err != 0 ) exit(-1);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
exit(0);
}
輸出
procedure0 is in critical section
procedure1 is waiting!
procedure0 is waiting!
procedure1 is in critical section
procedure1 is waiting!
procedure0 is waiting!
procedure0 is in critical section
procedure1 is waiting!
procedure1 is in critical section
procedure0 is waiting!
procedure0 is in critical section
procedure0 is waiting!
procedure1 is waiting!
procedure1 is in critical section
procedure0 is waiting!
procedure0 is in critical section
procedure1 is waiting!
procedure1 is in critical section
...
Dekker算法
https://www.geeksforgeeks.org/dekkers-algorithm-in-process-synchronization/
在上述鏈接找到了java版本的代碼
Main()
{
// to denote which thread will enter next
int favouredthread = 1;
// flags to indicate if each thread is in
// queue to enter its critical section
boolean thread1wantstoenter = false;
boolean thread2wantstoenter = false;
startThreads();
}
Thread1()
{
do {
thread1wantstoenter = true;
// entry section
// wait until thread2 wants to enter
// its critical section
while (thread2wantstoenter == true) {
// if 2nd thread is more favored
if (favaouredthread == 2) {
// gives access to other thread
thread1wantstoenter = false;
// wait until this thread is favored
while (favouredthread == 2)
;
thread1wantstoenter = true;
}
}
// critical section
// favor the 2nd thread
favouredthread = 2;
// exit section
// indicate thread1 has completed
// its critical section
thread1wantstoenter = false;
// remainder section
} while (completed == false)
}
Thread2()
{
do {
thread2wantstoenter = true;
// entry section
// wait until thread1 wants to enter
// its critical section
while (thread1wantstoenter == true) {
// if 1st thread is more favored
if (favaouredthread == 1) {
// gives access to other thread
thread2wantstoenter = false;
// wait until this thread is favored
while (favouredthread == 1)
;
thread2wantstoenter = true;
}
}
// critical section
// favour the 1st thread
favouredthread = 1;
// exit section
// indicate thread2 has completed
// its critical section
thread2wantstoenter = false;
// remainder section
} while (completed == false)
}