# Unisex Bathroom Synchronization Problem
Unisex bathroom problem solved using pthreads, mutex locks and condition variables. Written in C
## Synchronization: Mutex Locks
Mutex Locks were used in my code in order to prevent different threads from
accessing critical sections of the code at the same time. Their purpose in the code is to
avoid the mutual exclusion problem since this program is multi-threaded that happens
concurrently. Therefore mutexes are important so that only one thread can perform
modification on shared values, such as the values of men and women currently in the
bathroom.
There are two mutexes declared, one for each gender. This is because each
thread from each gender has access to the value of the number of people or threads
currently in the bathroom from their gender.
In the functions man_wants_to_enter() and woman_wants_to_enter(), the locks
exist so that each thread would be able to individually access the bathroom. This means
that each thread will be able to increment the bathroom variable without the variable
being simultaneously accessed by another thread. In the critical code there are also
constraints that have to be met therefore the thread has to wait, this will be discussed in
the next section.
Similarly, in the functions man_leaves() and woman_leaves(), what is in the
critical section is the decrimentation of the number of each gender in the bathroom
depending on the function. It is important to keep that part of the code in the critical
section so that one thread at a time can perform this function
## Synchronization: Conditional Variables
Conditional variables are used in the program so that one of the threads could be
blocked or wait until another thread signals it to continue. If the male gender threads are
taken as an example, in order for a man to enter the bathroom, there must be two
criterias that have to be met. One is that there are no females in the bathroom, the
second one is that there are no more than 2 men in there, assuming that there can be a
maximum of 3 people at a time in the bathroom. As a result conditional variables are
used in this case. When at least one of these criterias are not met, the thread is put on
hold until it gets a signal from another thread which would serve each criteria and
unblock the thread. One of the signals comes to male threads is when there is less than
3 men currently in the bathroom:
```
if (men < 3) pthread_cond_signal(&ManCon);
```
The other thread comes from the women_leave() function, so when one woman leaves
the bathroom, it checks whether or not there are any women left. If there aren't any
women left in the bathroom, a male thread that was kept on hold is unlocked and
signaled to continue:
```
if (women == 0)
{
pthread_mutex_lock(&ManMx);
pthread_cond_signal(&ManCon);
unblockedpthread_mutex_unlock(&ManMx);
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
男女皆宜的pthreads:使用pthreads,互斥锁和条件变量解决了中性浴室问题。 用C写
共2个文件
md:1个
c:1个
需积分: 27 2 下载量 165 浏览量
2021-02-14
21:29:38
上传
评论
收藏 3KB ZIP 举报
温馨提示
中性浴室同步问题 使用pthreads,互斥锁和条件变量解决了中性浴室问题。 用C写 同步:互斥锁 我的代码中使用了互斥锁,以防止不同的线程同时访问代码的关键部分。 它们在代码中的目的是避免互斥问题,因为该程序是同时发生的多线程。 因此,互斥锁很重要,因此只有一个线程可以对共享值(例如当前在浴室中的男女值)执行修改。 声明了两个互斥锁,每个性别一个。 这是因为来自每个性别的每个线程都可以从性别中获得当前在浴室中的人数或线程的值。 在man_wants_to_enter()和woman_wants_to_enter()函数中,存在锁,以便每个线程都可以单独访问浴室。 这意味着每个线程将能够递增bathroom变量,而该变量不会被另一个线程同时访问。 在关键代码中,还必须满足一些约束,因此线程必须等待,这将在下一节中讨论。 类似地,在功能man_leaves()和woman_leaves(
资源推荐
资源详情
资源评论
收起资源包目录
unisex-pthreads-master.zip (2个子文件)
unisex-pthreads-master
README.md 3KB
demo.c 4KB
共 2 条
- 1
资源评论
BugHunter666
- 粉丝: 22
- 资源: 4699
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功