免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1652 | 回复: 5
打印 上一主题 下一主题

求助:有关linux下的线程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-16 16:11 |只看该作者 |倒序浏览
我写了一个程序,关于经典模型:生产者和消费者的问题,我采用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...


说明:就运行了这几次就不再生产和消费了,请问这是怎么回事呢??
谢谢大家了阿~~

论坛徽章:
0
2 [报告]
发表于 2010-07-16 16:36 |只看该作者
没人帮我阿。。。。

论坛徽章:
0
3 [报告]
发表于 2010-07-16 20:33 |只看该作者
我也没分析过,不过你可以参考http://topic.csdn.net/t/20051109/19/4382819.html

论坛徽章:
0
4 [报告]
发表于 2010-07-17 10:50 |只看该作者
我自己解决了,哈哈~

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
5 [报告]
发表于 2010-07-17 12:03 |只看该作者
回复 4# 刘子一


    问题出在哪?

论坛徽章:
0
6 [报告]
发表于 2010-07-17 22:18 |只看该作者
分享一下处理方案
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP