xiumuzi003 2015-08-21 17:50 采纳率: 0%
浏览 1387

各位大侠,小弟练习写了个代码,求解答

为什么运行的结果不能输出“>”,而且当我在终端输入quit时 出现不停打印“>”的死循环
代码如下:

#include
#include
#include
#include
#include
#include
#include
#include

#define MAX 1024
pthread_mutex_t rwlock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t rwcond =PTHREAD_COND_INITIALIZER;
void* read_file(void *arg)
{
int fd;
int ret,n;
char buf[MAX];
if(fd = open ((char *)arg ,O_CREAT|O_RDONLY,0666) == -1)
{
perror("open fail");
pthread_exit(NULL);
}
while(1){
ret =pthread_mutex_lock(&rwlock);
if (ret != 0)
{
perror("pthread_mutex_lock");
close(fd);
pthread_exit(NULL);
}
n = read(fd ,buf,sizeof(buf));
if(n< 0)
{
perror("read fail");
close(fd);
pthread_exit(NULL);
}
while(n == 0)
{
ret = pthread_cond_wait(&rwcond,&rwlock);
if(ret != 0)
{
perror("pthread_cond_wait fail");
close(fd);
pthread_exit(NULL);
}

    }
    buf[n] ='\0';
    printf("read %d character :%s \n",strlen(buf),buf);
    if(strncmp(buf,"quit",4) == 0)
    {
        ret =pthread_mutex_unlock(&rwlock);
        if(ret !=0)
        {
            perror("pthread_mutex_unlock fail");
            close(fd);
            pthread_exit(NULL);
        }
        break;
    }
    ret = pthread_mutex_unlock(&rwlock);
    if(ret != 0)
    {
        perror("pthread_mutex_unlock fail");
        close(fd);
        pthread_exit(NULL);
    }

}
close(fd);
pthread_exit(NULL);

}
void* write_file(void arg)
{
int fd;
int ret,n;
char buf[MAX];
char buf_bak[MAX];
fd = open((char
)arg,O_CREAT|O_TRUNC|O_WRONLY,0666);
if(fd < 0)
{
perror("open fail");
pthread_exit(NULL);
}
while(1){
ret = pthread_mutex_lock(&rwlock);
if(ret != 0)
{
perror("pthread_mutex_lock fail");
close(fd);
pthread_exit(NULL);
}
printf(">\n");
fgets(buf,strlen(buf),stdin);
//snprintf(buf_bak,sizeof(buf_bak),"%s",buf);
buf[strlen(buf)-1] ='\0';
n = write(fd,buf,sizeof(buf));
if(n < 0)
{
perror("write fail");
close(fd);
pthread_exit(NULL);
}
if(strncmp(buf,"quit",4) == 0)
{
ret = pthread_mutex_unlock(&rwlock);
if(ret != 0)
{
perror("pthread_mutex_unlock fail");
close(fd);
pthread_exit(NULL);
}
ret = pthread_cond_signal(&rwcond);
if(ret != 0)
{
perror("pthread_cond_signal fail");
close(fd);
pthread_exit(NULL);
}
usleep(500);
break;
}
ret = pthread_mutex_unlock(&rwlock);
if(ret != 0)
{
perror("pthread_mutex_unlock fail");
close(fd);
pthread_exit(NULL);
}
ret =pthread_cond_signal(&rwcond);
if(ret != 0)
{
perror("pthread_cond_signal fail");
close(fd);
pthread_exit(NULL);
}
usleep(500);
}
close(fd);
pthread_exit(NULL);
}

int main (int argc,char *argv[])
{
pthread_t rpth_id,wpth_id;
int ret;
if(argc < 2)
{
fprintf(stderr,"usage: %s argv[1]\n",argv[0]);
exit(EXIT_FAILURE);
}
ret = pthread_create(&rpth_id,NULL,read_file,(void *)argv[1]);
if(ret != 0)
{
perror("prhread_create fail");

exit(EXIT_FAILURE);
}
usleep(500);
ret = pthread_create(&wpth_id,NULL,write_file,(void *)argv[1]);
if (ret != 0)
{
perror("pthread_create fail");
exit(EXIT_FAILURE);
}

pthread_join(rpth_id,NULL);
pthread_join(wpth_id,NULL);
return 0;

}

  • 写回答

1条回答 默认 最新

  • devmiao 2015-08-21 23:11
    关注

    看看是不是线程没有正确退出

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序