#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、两类线程及其执行可能性:
- 学生线程:n个学生,每个学生一个线程,在执行学生线程的函数thread_stu中,主要做三个操作:学生的信号量+1;ta的信号量-1;chair-1;但在此之前要判断椅子是否有空余,如果有,执行上述三个操作,若没有空余,则该学生直接离开。
- Ta线程:执行ta线程的函数void *thread_ta(void) 主要做三个操作:学生的信号量-1;ta的信号量+1;chair+1; 同时注意ta 睡觉的条件以及程序退出的条件:当chair = 3 时,ta睡觉,当chair = 3 且已经总共处理了n个学生时,使用break 退出程序。
4、变量说明:
- n:一共n个学生,由用户输入得到。
- pass: ta 辅导过的学生数量。
- leave:由于没有空余的chair数量,离开的学生数量。
在程序运行结束时,应该有n = pass + leave
题目: