资源简介
参考教材中的生产者消费者算法,创建5个进程,其中两个进程为生产者进程,3个进程为消费者进程。一个生产者进程试图不断地在一个缓冲中写入大写字母,另一个生产者进程试图不断地在缓冲中写入小写字母。3个消费者不断地从缓冲中读取一个字符并输出。为了使得程序的输出易于看到结果,仿照的实例程序,分别在生产者和消费者进程的合适的位置加入一些随机睡眠时间。
可选的实验:在上面实验的基础上实现部分消费者有选择地消费某些产品。例如一个消费者只消费小写字符,一个消费者只消费大写字母,而另一个消费者则无选择地消费任何产品。消费者要消费的产品没有时,消费者进程被阻塞。注意缓冲的管理。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 15
struct shared{
int written;
char text[maxn];
int inout;
};
union semun{
int val;
struct semid_ds *buf;
unsigned short *arry;
};
int Vsem(int semidint sem_num){
struct sembuf buf={sem_num+1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“V failed.\n“);
return 0;
}
return 1;
}
int Psem(int semidint sem_num){
struct sembuf buf={sem_num-1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“P failed.\n“);
return 0;
}
return 1;
}
int main()
{
//
key_t key;
char * path = “/home/crazycat/123“;
key = ftok(path 1);
if(key==-1)perror(“key error.\n“);
//
int shmid = shmget(key sizeof(struct shared) IPC_CREAT|0604);
if(shmid==-1) perror(“shmid error.\n“);
//
int semid = semget(key 4 IPC_CREAT|0666);
if(semid==-1){
printf(“%s\n“strerror(errno));
return 0;
}
union semun arg;
arg.val = 1;
semctl(semid 0 SETVAL arg); //mutex1
arg.val=1;
semctl(semid 1 SETVAL arg); //mutex2
arg.val=0;
semctl(semid 2 SETVAL arg); //full_sem
arg.val=maxn;
semctl(semid 3 SETVAL arg); //empty_sem
//
struct shared * cache;
if ((cache = (struct shared *)shmat(shmid NULL 0)) == (void *)-1) {
perror(“shmat error.\n“);
}
cache->written=0;
cache->in=0;
cache->out=0;
//
pid_t pid[5];
int i;
for(i=0;i<5;i++){
pid[i]=f
- 上一篇:单片机数字钟+万年历数码管显示
- 下一篇:海思ADC驱动源码
相关资源
- 升腾Win终端系统升级方法新版.doc
- Uninstall_Cortana_WINCLIENT.CN.rar
- STM32基于rt_thread操作系统的SDHC卡文件
- 操作系统 LRU算法 实验报告 及 程序代
- [免费]车载CE6.0操作系统
- 分页系统模拟实验 操作系统 课程设
- 模拟段页式虚拟存储管理中地址转换
- 操作系统实验——虚存管理实验
- 广工操作系统实验
- 广东工业大学操作系统实验四文件系
- Bochs入门教程[操作系统第一步]
- 操作系统课程设计完整版
- 磁盘调度算法的模拟实现及对比
- 模拟一个文件管理系统
- 二级文件系统(操作系统)
- uCOS编译环境建立 BC45 TASM
- Linux 操作系统实验(全)
- 操作系统实验综合设计【附代码】
- 操作系统共享内存实验
- 操作系统循环首次适应算法
- 操作系统课程设计实现可变分区存储
- 基于GTK的Linux环境下的简易任务管理器
- 操作系统课程设计 二级文件管理系统
- 加快Windows XP操作系统开机速度
- 操作系统教程课后答案华中科技大学
- 51单片机中使用ucos ii的优缺点转
- 51单片机中使用ucos ii的优缺点
- 嵌入式实时操作系统ucos-II 第二版 源
- 计算机操作系统课后_汤小丹_第四版
- 计算机操作系统(第四版)汤小丹课
评论
共有 条评论