操作系统 Homework4 Sleeping Assistant ~线程同步

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
pthread_mutex_t mutex_chair;
sem_t ta, stu;
void *thread_ta();
void *thread_stu(void *arg);
int chair = 3;

#define p(x) sem_wait(&x)
#define v(x) sem_post(&x)

int n;
int pass = 0;
int leave = 0;

int main()
{

    scanf("%d", &n);
    //printf("we have received %d", n);

    sem_init(&ta, 0, 1);
    sem_init(&stu, 0, 0);

    pthread_t ta_tid;
    pthread_t stu_tid[n];

    pthread_create(&ta_tid, NULL, thread_ta, NULL);
    int i;
    for (i = 0; i < n; i++)
    {

        pthread_create(&stu_tid[i], NULL, thread_stu, &i);
        sleep(1);
    }
    pthread_join(ta_tid, NULL);
    for (int i = 0; i < n; i++)
    {
        pthread_join(stu_tid[i], NULL);
    }

    return 0;
}

void *thread_ta(void)
{
    printf("this is in the office hour");
    while (1)
    {
        p(stu);
        pthread_mutex_lock(&mutex_chair);
        chair++;
        pthread_mutex_unlock(&mutex_chair);
        v(ta);
        pass++;
        printf("now we have the %d thta assistance done\n\n", pass);

        if (chair == 3)
        {
            printf("*****no more student, the TA can sleep!*****\n\n");
            if (pass + leave == n)
            {
                printf("well done!\n");
                break;
            }
        }

        sleep(2);
    }
}

void *thread_stu(void *arg)
{

    int stu_no = *(int *)arg;
    stu_no = stu_no + 1;
    printf("----the %d th student is comming\n", stu_no);
    pthread_mutex_lock(&mutex_chair);
    if (chair <= 0)
    {
        printf("no more chair for waiting! The %d the student leave\n\n", stu_no);
        leave++;
        pthread_mutex_unlock(&mutex_chair);
    }
    else if (chair > 0)
    {
        chair--;
        printf("the %d th sit on the chair\n\n", stu_no);
        v(stu);
        pthread_mutex_unlock(&mutex_chair);

        p(ta);

        sleep(2);
    }
}

编译及运行指令:

gcc thread2.c -o thread2 -lpthread

./thread2

设计思路:

1、两个信号量: stu(初始值为0)ta(初始值为1)

2、一个互斥变量:chair(0-3)

3、两类线程及其执行可能性:

  1. 学生线程:n个学生,每个学生一个线程,在执行学生线程的函数thread_stu中,主要做三个操作:学生的信号量+1;ta的信号量-1;chair-1;但在此之前要判断椅子是否有空余,如果有,执行上述三个操作,若没有空余,则该学生直接离开。
  2. Ta线程:执行ta线程的函数void *thread_ta(void) 主要做三个操作:学生的信号量-1;ta的信号量+1;chair+1;  同时注意ta 睡觉的条件以及程序退出的条件:当chair = 3 时,ta睡觉,当chair = 3 且已经总共处理了n个学生时,使用break 退出程序。

4、变量说明:

  1. n:一共n个学生,由用户输入得到。
  2. pass: ta 辅导过的学生数量。
  3. leave:由于没有空余的chair数量,离开的学生数量。

在程序运行结束时,应该有n = pass + leave

题目:

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值