Compare commits

..

3 Commits
master ... 01

@ -4,30 +4,30 @@
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
int n=10, in=0,out=0,buffer[10];//定义变量并创建数组。
sem_t empty,full;//定义信号量
int n=10, in=0,out=0,buffer[10];
sem_t empty,full;
void* producer(void *arg){
int tag= pthread_self()%100;//线程信号
int tag= pthread_self()%100;
int nextPro;
srand(time(NULL)+tag);//随机数种子
srand(time(NULL)+tag);
while(1){
nextPro = rand()%97;
sem_wait(&empty);//empty1
sem_wait(&empty);
buffer[in] = nextPro;
printf("production:%d %d\n",nextPro,buffer[in]);
usleep(1000*1000/20);
in = (in+1)%n;
sem_post(&full);//full+1
sem_post(&full);
usleep(1000*1000/2);
}
}
void* consumer(void *arg) {
int item;
while(1){
sem_wait(&full)//full1
sem_wait(&full);
item = buffer[out];
sem_post(&empty);//empty+1
sem_post(&empty);
printf("consume:%d\n",buffer[out]);
out = (out+1)%n;
usleep(1000*1000/10);
@ -35,12 +35,13 @@ void* consumer(void *arg) {
}
int main() {
pthread_t tid[7];
//初始有10个缓冲区
sem_init( &empty, 0,10);
sem_init( &full, 0,0);//初始化信号量
sem_init( &full, 0,0);
for(int i=0;i<6;i++)
pthread_create(&tid[i], NULL, producer, NULL);//创建生产者
pthread_create(&tid[6], NULL, consumer, NULL);//创建消费者
pthread_create(&tid[i], NULL, producer, NULL);
pthread_create(&tid[6], NULL, consumer, NULL);
for(int i = 0; i<7; i++)
pthread_join(tid[i], NULL);//等待线程结束
pthread_join(tid[i], NULL);
printf("main is over\n");
}

@ -25,24 +25,22 @@ void* p2(void *arg){
}
void* p3(void *arg){
for(int i=0;i<10;i++){
sem_wait(&p3_C); //p3开始等待等p2指示完从此执行
sem_wait(&p3_C); //这里还是等待。。。。。
usleep(1000*1000/2); //等待0.5秒
printf("C\n");
sem_post(&p1_A); //p3执行完继续跳转到p1再继续执行
}
}
}
int main()
{
pthread_t tid[3];
sem_init(&p1_A, 0, 1);
sem_init(&p1_A, 0, 1);//这里第3个参数设为“1”可以理解为第一个执行
sem_init(&p2_B, 0, 0);
sem_init(&p3_C, 0, 0);
pthread_create(&tid[0], NULL, p1, NULL);
pthread_create(&tid[1], NULL, p2, NULL);
pthread_create(&tid[2], NULL, p3, NULL);
pthread_create(&tid[2], NULL, p3, NULL);
for(int i = 0; i < 3; i++)
pthread_join(tid[i], NULL);
printf("main is over\n");

@ -4,62 +4,29 @@
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
//定义信号量——1
sem_t p1_A, p2_B, p3_C;
void* p1(void *arg){
int num_1 = 0; //初始化p1的信号量
for(int i=0;i<30;i++){
sem_wait(&p1_A);
printf("A\n");
num_1++; //信号量自加
if(num_1 % 3 == 0){
sem_post(&p2_B); //激活p2
}else{
sem_post(&p1_A); //不然一直在该线程执行
}
for(int i=0;i<10;i++){
printf("A\n");
}
}
void* p2(void *arg){
int num_2 = 0; //初始化p2的信号量
for(int i=0;i<30;i++){
sem_wait(&p2_B); //p2开始等待等p1指示完从此执行
printf("B\n");
num_2++;
if(num_2 % 3 == 0){
sem_post(&p3_C); //激活p3
}else{
sem_post(&p2_B); //
}
}
for(int i=0;i<10;i++){
printf("B\n");
}
}
void* p3(void *arg){
int num_3 = 0; //初始化p3的信号量
for(int i=0;i<30;i++){
sem_wait(&p3_C); //
printf("C\n");
num_3++;
if(num_3 % 3 == 0){
sem_post(&p1_A); //激活p1进入循环
}else{
sem_post(&p3_C); //
}
}
for(int i=0;i<10;i++){
printf("C\n");
}
}
int main()
{
pthread_t tid[3];
sem_init(&p1_A, 0, 1);
sem_init(&p2_B, 0, 0);
sem_init(&p3_C, 0, 0);
pthread_create(&tid[0], NULL, p1, NULL);
pthread_create(&tid[1], NULL, p2, NULL);
pthread_create(&tid[2], NULL, p3, NULL);
for(int i = 0; i < 3; i++)
pthread_join(tid[i], NULL);
printf("main is over\n");
}
}

Loading…
Cancel
Save