信号量和并发编程 [英] Semaphores and concurrent programming

查看:144
本文介绍了信号量和并发编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关家庭作业,我需要编程以下情形。这是要通过使用BACI(这是C--)信号量来完成

For a homework assignment i need to program the following scenario. This is going to be done using semaphores using BACI (which is C--)

有2有可各容纳4人的中性厕所。既然是男女皆宜的只有同性别的人可以在同一时间在洗手间和FIFO并不重要。我在我的脑袋基本的算法来处理4男4女1洗手间。但我不知道如何code这一点。任何帮助将大大AP preciated。下面是我。

There are 2 unisex restrooms that can hold 4 people each. Since it is unisex only people of the same sex can be in the restroom at the same time and FIFO is not important. I have the basic "algorithm" in my head to handle 4 men and 4 woman for 1 restroom. But i don't know how to code this. Any help would be greatly appreciated. Here is what I have.

Woman:

Check to see if there are any men in the restroom. If so "wait".
If no men check to see if there are 4 people. If so "wait".
If no men and not 4 use restroom. When leaving signal there is a vacancy.
If last woman signal the men if they are waiting if not signal the woman.


Man:

check to see if there are any woman in the restroom. if so "wait"
If no woman check to see if there are 4 people. If so "wait".
If no woman and not 4 use restroom. when leaving signal there is a vacancy.
if last man signal the women if they are waiting if not signal the men.

中提供这些额外的说明

These additional instructions were provided


  • 使用随机FOR循环来模拟在适当的地方时间的流逝。这可以通过使用一延迟函数很容易地完成:

  • Use random FOR loops to simulate the passage of time in the appropriate places. This can be easily done by using a Delay function:

void Delay (void)
{ 
  int i;
  int DelayTime;
  DelayTime = random (DELAY);
  for (i = 0; i < DelayTime; i++):
}


  • 其中const int的DELAY = 10〜100一定数量

  • where const int DELAY = some number from 10 to 100.

    推荐答案

    既然你想知道如何code的1厕所算法,我已经做了所以在C.​​这将是一个相当简单的任务,将其转换成C--,因为所有的信号灯结构显得颇为相似。

    Since you want to know how to code your algorithm for 1 restroom, I have done so in C. It will be a fairly simple task to convert it into C--, as all the semaphore constructs appear quite similar.

    从什么我可以做你的答案,

    From what I could make of your answer,

    C: sem_wait()  C--: wait()
       sem_post()       signal()
       sem_t            semaphore()
       sem_init()       initialsem() 
    

    记住,如前所述,我只制定出了问题对于 1洗手间。由于这是功课,我希望你把它扩展到在 2洗手间形成自己。

    Bear in mind, as stated, I have worked out the problem for 1-restroom only. Since this is homework, I expect you to expand it into the 2-restrooms form yourself.

    工作一个人的方式从读者,作家的问题,我们的中性厕所问题,我们使用以下全局变量:

    Working one's way from the Readers-writers problem to our "Unisex Restroom" problem, we make use of the following global variables:

    int mcount,wcount; // count of number of men/women in restroom
    sem_t x,y,z;       // semaphores for updating mcount & wcount values safely
    sem_t wsem,msem;   // semaphores to block other genders' entry  
    sem_t cap;         // capacity of the restroom
    

    将这些信号量&放大器;柜台到线程函数,

    void *man(void *param)
    {           
        sem_wait(&z);                
            sem_wait(&msem);        
                sem_wait(&x);
                    mcount++;
                    if(mcount==1)   
                    { sem_wait(&wsem); } // first man in, make women wait
                sem_post(&x);
            sem_post(&msem);
        sem_post(&z);
    
        sem_wait(&cap);  //wait here, if over capacity
    
        printf("\t\tman in!\n");
        delay();
        printf("\t\t\tman out!\n");
    
        sem_post(&cap);  //one man has left, increase capacity
    
        sem_wait(&x);
            mcount--;
            if(mcount==0)
            {sem_post(&wsem);}  // no man left, signal women 
        sem_post(&x);
    }
    

    同样,女人线程函数,替代品 mcount wcount MSEM WSEM X 。只有以Z 仍是在的功能,这样既&安培; 女人线程排队,在相同的公共信号。 (由于这一点,code总是有 FIFO样的行为,从而确保公平性/非饥饿

    Similarly, the woman thread function, substitutes mcount with wcount, msem with wsem, and x with y. Only z remains as is in the man function, so that both man & woman threads queue up on the same common semaphore. (Due to this, the code invariably has FIFO-like behaviour, which ensures fairness/non-starvation)

    完整code如下:(编译,使用海湾合作委员会的文件名-lpthread

    The complete code is as follows: (To compile, use gcc filename -lpthread)

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    
    int mcount,wcount;
    sem_t x,y,z,wsem,msem,cap;
    
    void delay(void)
    {
        int i;
        int delaytime;
        delaytime = random();
        for (i = 0; i<delaytime; i++);
    }
    
    void *woman(void *param)
    {
        sem_wait(&z);
            sem_wait(&wsem);
                sem_wait(&y);
                    wcount++;
                    if(wcount==1)
                    { sem_wait(&msem); }
                sem_post(&y);
            sem_post(&wsem);
        sem_post(&z);
    
        sem_wait(&cap);
    
        printf("woman in!\n");
        delay();
        printf("\twoman out!\n");
    
        sem_post(&cap);     
    
        sem_wait(&y);
            wcount--;
            if(wcount==0)
            { sem_post(&msem); }
        sem_post(&y);
    }
    
    void *man(void *param)
    {           
        sem_wait(&z);
            sem_wait(&msem);
                sem_wait(&x);
                    mcount++;
                    if(mcount==1)
                    { sem_wait(&wsem); }
                sem_post(&x);
            sem_post(&msem);
        sem_post(&z);
    
        sem_wait(&cap);
    
        printf("\t\tman in!\n");
        delay();
        printf("\t\t\tman out!\n");
    
        sem_post(&cap);
    
        sem_wait(&x);
            mcount--;
            if(mcount==0)
            {sem_post(&wsem);}
        sem_post(&x);
    }
    
    int main(void)
    {
        int i;
        srandom(60);
    
            mcount = 0;
            wcount = 0;
            sem_init(&x,0,1);  // for sem_init, initial value is 3rd argument
            sem_init(&y,0,1);
            sem_init(&z,0,1);
            sem_init(&wsem,0,1);
            sem_init(&msem,0,1);
            sem_init(&cap,0,4);  // eg. cap initialized to 4
    
            pthread_t *tid;
            tid = malloc(80*sizeof(pthread_t));
    
        // You can use your cobegin statement here, instead of pthread_create()     
        // I have forgone the use of pthread barriers although I suppose they would nicely imitate the functionality of cobegin. 
        // This is merely to retain simplicity.
    
        for(i=0;i<10;i++)
        {
            pthread_create(&tid[i],NULL,woman,NULL);
        }
        for(i=10;i<20;i++)
        {     
                pthread_create(&tid[i],NULL,man,NULL);
        }
        for(i=0;i<20;i++)
        {     
                pthread_join(tid[i],NULL);
        }
    
        return(0);
    }
    

    在转换成 2洗手间的形式,记下该信号灯和放大器;计数器变量,你将需要复制,以满足所有条件。快乐的 semaphoring 的!

    While converting into the 2-restrooms form, make note of which semaphores & counter variables you would need to duplicate to satisfy all the conditions. Happy semaphoring!

    这篇关于信号量和并发编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆