资源简介
基于共享内存实现的理发师问题.
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if defined (__GNU_LIBRARY__)&& !defined (_SEM_SEMUN_UNDEFINED)
#else
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
#define SHMDATASIZE 1000
#define BUFFERSIZE (SHMDATASIZE -sizeof(int))
#define WAITING 8
#define MUTEX 16
#define CHAIRS 3
#define CUS 20
#define BAR 28
#define MAXSIZE 100
int Semid = 0;
void customer (int shmid);
void barbers(int shmid);
void v(int *mutex);
void p(int *mutex);
int masterinit (void);
int *standardinit (int shmidint *semid);
void delete (void);
void sigdelete (int signum);
void cuthair(int shmidint semidchar *buffer);
int mysemget (key_t kyeint nsemsint semflg);
int mysemctl (int shmidint semnumint cmdunion semun arg);
int mysemop (int semidstruct sembuf *sopsunsigned nsops);
int myshmget (key_t keyint sizeint shmflg);
void *myshmat (int shmidconst void *shmaddrint shmflg);
int myshmctl (int shmidint cmdstruct shmid_ds *buf);
int main (int argcchar *argv[])
{
char selection[3];
int shmid;
if (argc < 2)
{
shmid = masterinit();
}
else
{
shmid = atoi (argv[1]);
}
printf (“Who are you?customer(1)?barber(2)?\n“);
fgets (selectionsizeof (selection)stdin);
switch (selection[0])
{
case ‘1‘:customer(shmid);break;
case ‘2‘:barbers(shmid);break;
default:printf (“invalid choicequit \n“);
}
return 0;
}
void customer(int shmid)
{
int semid=0;
int *buffer=0;
int *waiting=0;
int *customers=0;
int *mutex=0;
int *barbers=0;
char selection[3];
buffer=standardinit(shmid&semid);
waiting=buffer+WAITING;
mutex=buffer+MUTEX;
customers=buffer+CUS;
barbers=buffer+BAR;
printf(“ begin to run the id of share memory is %d semaphore id is %d \n“shmidsemid);
// printf(“Press any key when you want to cut hair... \n“);
// fgets (selectionsizeof (selection)stdin);
// fflush(stdout);
printf(“Now waiting for mutex... \n“);
p(mutex);
*waiting=*waiting+1;
if(*waiting
v(customers);
v(mutex);
printf(“waiting for barber...\n“);
p(barbers);
printf(“you are getting hair cut...\n“);
*waiting=*waiting-1;
printf(“finished\n“);
}
else {v(mutex);*waiting=*waiting-1;
printf(“so many peoplelet‘s go!\n“);
}
}
void barbers(int shmid)
{ int semid=0;
int *buffer=0;
int *waiting=0;
int *customers=0;
int *mutex=0;
int *barbers=0;
char selection[3];
buffer=standardinit(shmid&semid);
waiting=buffer+WAITING;
mutex=buffer+MUTEX;
customers=buffer+CUS;
barbers=buffer+BAR;
while(1){char input[3];
printf(“waiting for customer\n“);
p(customers);
// printf(“waiting for mutex\n“);
//p(mutex);
//*waiting=*waiting-1;
printf(“Press any key when
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 245726 2007-12-24 23:12 report.pdf
文件 9297 2007-12-17 17:12 c
文件 5906 2007-12-17 16:57 cusbar.c
文件 160388 2007-12-24 23:11 readme.pdf
----------- --------- ---------- ----- ----
421317 4
相关资源
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
- 基于Linux的串口服务器设计
- Windows下访问LINUX的利器-SSH
- Linux操作系统课件PPT
- 《操作系统原理及应用Linux》PPT
- Linux版的telnet安装包telnet、telnet-serv
- 快速掌握LINUX 学习笔记
- 基于Linux、QT的视频监控系统的设计与
- Linux Vim操作手册
- 剖析Linux系统下基于NUMA构建的服务
- linux SPI设备注册和驱动小结
- 周立功PCI CAN卡LINUX驱动ubuntu16.04内核
- 实现Windows与Linux两系统间自由切换
- 在双引导Linux系统上实现OS自动切换
- 如何删除Linux系统后找回Windows的启动
- Linux命令详解,循序渐进Linux
- Linux系统文件命令精通指南(下)
- Linux系统命令及Shell脚本实践指南
- RK3308 LINUX开发者指南(1).pdf
- S3C4510 开发板中uCLinux系统开发
- 嵌入式操作系统的解析
评论
共有 条评论