- 论坛徽章:
- 0
|
我写了一个程序,关于经典模型:生产者和消费者的问题,我采用linux下的线程的互斥的方式实现的,其中有三个信号量:avail和full:表示生产者和消费这线程间的同步问题,mutex表示两个线程之间的互斥问题。其中avail:有界缓冲区的空单元数,开始初始值为N,full:有界缓冲区的非空单元数,初始值为0,mutex为互斥信号,初始值为1;程序如下:
目前的问题是:这个程序经过
gcc -o producer_customer.o -g -Wall -lrt producer_customer.c
运行后,没有像想象的那么运行,应该是无限循环的,可是这个程序之运行了几次生产和消费的过程,就听下来了,不再运行了,请教大虾们阿.........................
程序代码:
/*
函数名:producer_customer.c
函数功能:实现生产者和消费者之间的关系。采用线程机制,在同一个线程中实现这一机制,采用信号量实现他们之间的流程
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/ipc.h>
#include<errno.h>
#include<fcntl.h>
#include<semaphore.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define MYFIFO "myfifo"//缓冲区管道名
#define BUFFER_SIZE 3//缓冲区单元数
#define UNIT_SIZE 6//缓冲区的单元大小
#define RUN_TIME 30//运行时间
#define DELAY_TIME 5//最大周期时间
int fd;
time_t end_time;
sem_t mutex,avail,full;//信号量 mutex:互斥信号量,avail:有界缓冲区的空单元数,full:有界缓冲区的非空单元数
int real_read;
char buff[UNIT_SIZE];
int real_write_size;
int delay_time_p=2;
int delay_time_c=5;
void *producer();//生产者
void *customer();//消费者进程
int main()
{
pthread_t pth_pro_id,pth_cus_id;
int res;
end_time = time(NULL)+RUN_TIME;
//mutex = 1;
//fd = mkfifo(MYFIFO,O_CREAT|O_EXCL|0x666);
if((mkfifo(MYFIFO,O_CREAT|O_EXCL|0x666)<0)&&(errno != EEXIST))
{
printf("error in creating fifo...\n");
exit(1);
}
fd = open(MYFIFO,O_RDWR);
if(fd <0)
{
printf("can not open fifo...\n");
exit(1);
}
/*互斥量的初始化*/
res = sem_init(&mutex, 0, 1);
if(res)
{
printf("error in initializing mutex semaphore...\n");
exit(1);
}
/*有界缓冲区的初始化:avail*/
res= sem_init(&avail, 0, BUFFER_SIZE);
if(res)
{
printf("error in initializing availsemaphore...\n");
exit(1);
}
/*有界缓冲区的初始化:full*/
res= sem_init(&full, 0, 1);
if(res)
{
printf("error in initializing full semaphore...\n");
exit(1);
}
/*创建生产者线程*/
res = pthread_create(&pth_pro_id,NULL,producer,NULL);
if(res)
{
printf("pthread producer created error...\n");
exit(1);
}
/*创建消费者线程*/
res = pthread_create(&pth_cus_id,NULL,customer,NULL);
if(res)
{
printf("pthread customer created error...\n");
exit(1);
}
/*阻塞等待*/
pthread_join(pth_pro_id,NULL);
pthread_join(pth_cus_id,NULL);
/*收尾*/
close(fd);
unlink(MYFIFO);
exit(0);
}
void *producer()
{
while(1)
{
sleep(delay_time_p);//延时一段时间再生产
sem_wait(&mutex); //P
sem_wait(&avail); //P
printf("producer delay time_p= %d...\n",delay_time_p);
printf("producer:fd=%d...\n",fd);
real_write_size=write(fd,"hello",UNIT_SIZE);//生产
if(real_write_size<0)
{
if(errno == EAGAIN)
{
printf("The fifo has not been read yet ,try later...\n");
}
}
else
{
printf("Produce sucess:write %d to myfifo...\n\n",real_write_size);
}
sem_post(&mutex); //V
sem_post(&full); //V
}
pthread_exit(NULL); //退出
}
void * customer()
{
while(1)
{
sleep(delay_time_c);//延时time_c时间
sem_wait(&mutex); //P
sem_wait(&full); //P
printf("customer delaytime=%d...\n",delay_time_c);
printf("customer:fd=%d...\n",fd);
real_read = read(fd,buff,UNIT_SIZE);//消费
if(real_read < 0)
{
if(errno == EAGAIN)
{
printf("no data yet...\n ");
}
}
else
{
printf("Cunstomer:read %s from myfifo...\n\n",buff);
}
sem_post(&mutex); //V
sem_post(&avail); //V
}
pthread_exit(NULL); //退出
}
运行结果(我认为是错误的):
producer delay time_p= 2...
producer:fd=3...
Produce sucess:write 6 to myfifo...
producer delay time_p= 2...
producer:fd=3...
Produce sucess:write 6 to myfifo...
customer delaytime=5...
customer:fd=3...
Cunstomer:read hello from myfifo...
producer delay time_p= 2...
producer:fd=3...
Produce sucess:write 6 to myfifo...
producer delay time_p= 2...
producer:fd=3...
Produce sucess:write 6 to myfifo...
customer delaytime=5...
customer:fd=3...
Cunstomer:read hello from myfifo...
producer delay time_p= 2...
producer:fd=3...
Produce sucess:write 6 to myfifo...
说明:就运行了这几次就不再生产和消费了,请问这是怎么回事呢??
谢谢大家了阿~~ |
|