操作系统实验上机
更多技术请访问:www.xuanworld.top
部分审核不通过的文章将发至个人博客:www.xuanworld.top
欢迎来52破解论坛阅读帖子:https://www.52pojie.cn/thread-1891208-1-1.html
实验名称 | 实验序号 | 实验日期 | 实验人 |
---|---|---|---|
多线程与信号量编程 | 4 | 公元2077年 | Rytter |
一、实验题目
二、相关原理和知识
- linux有关于信号量的相关函数,可以直接调用
- 利用pthread进行创建进程,每个进程都会有一个id
- 创建进程后每个进程都会对buffer进行互斥操作
三、实验过程
- 编写关于信号量与进程id的函数
- 编写对buffer进行操作的两个函数
- 编写消费者与生产者的函数
- 编写创建生产者与消费者的函数
- 放到Linux服务器上进行编译和运行
四、实验结果
五、问题总结
- 编译时出现问题,原因位命令行没有使用pthread
- 消费者的printf代码位置不对,导致还没消费就产生输出
六、源码
首先是buffer.h文件
typedef int buffer_item;
#define BUFFER_SIZE 5
之后是main.c文件
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <unistd.h>
#include "buffer.h"
buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t full,empty;
void buffer_out(){int i;for ( i = 0; i <BUFFER_SIZE ; ++i) {printf("%d",buffer[i]);}printf("\n");
}
int insert_item(int a,int b){sem_wait(&empty);pthread_mutex_lock(&mutex);int i;for(i = 0;i < BUFFER_SIZE;i++){if(buffer[i] == 0){buffer[i] = 1;//找到空的位置break;}}if(i == BUFFER_SIZE) return -1;printf("producer %d produced %d\n",a,b);buffer_out();pthread_mutex_unlock(&mutex);sem_post(&full);return 0;
}
int remove_item(int a,int b){sem_wait(&full);pthread_mutex_lock(&mutex);int i;for(i = 0;i < BUFFER_SIZE;i++){if(buffer[i] != 0){buffer[i] = 0;break;}}if(i == BUFFER_SIZE) return -1;printf("consumer %d consumed %d\n",a,b);//第几个吃了几个食物buffer_out();pthread_mutex_unlock(&mutex);sem_post(&empty);return 0;
}void * producer(void * param){int* num = (int*)param;int food_id=0;while(1){sleep(((rand() % 10) + 1));if(insert_item(*num,++food_id))printf("wrong\n");}
}void * consumer(void *param){int* num = (int*)param;int food_id=0;while(1){sleep(((rand() % 10) + 1));if(remove_item(*num,++food_id))printf("wrong\n");}
}int main(int argc,char *argv[])
{//1.获取命令行输入的数字if(argc != 4) return -1;int sleep_time = atoi(argv[1]);int producer_num = atoi(argv[2]);int consumer_num = atoi(argv[3]);//2.进行初始化pthread_mutex_init(&mutex,NULL);sem_init(&empty,0,BUFFER_SIZE);sem_init(&full,0,0);int i;for(i=0;i<BUFFER_SIZE;i++) buffer[i]=0;//3.创建线程pthread_t producer_tid[producer_num];int producer_id[producer_num];for(i = 0;i < producer_num;i++){producer_id[i] = i + 1;pthread_create(&producer_tid[i],NULL,producer,&producer_id[i]);}pthread_t consumer_tid[consumer_num];int consumer_id[consumer_num];for(i = 0;i < consumer_num;i++){consumer_id[i] = i + 1;pthread_create(&consumer_tid[i],NULL,consumer,&consumer_id[i]);}//4.休眠sleep(sleep_time);//5.退出return 0;
}