实现进程互斥的软件方法

实现进程互斥的软件方法

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)
}

发布了51 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36267931/article/details/103199032