将一个文件中的数据打印到终端上,类似cat一个文件。要求如下:
1.A线程读取文件中的数据
2.B线程将A线程读取到的数据打印到终端上
3.文件打印完毕后,结束进程。
#include "/home/ubuntu/li/IO/head.h"
int fd;
char temp[20];
ssize_t res;
int flag = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *callback_read(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&cond,&mutex);
}
bzero(temp,strlen(temp));
res = read(fd,temp,sizeof(temp));
if(res<0)//读取失败
{
ERR_MSG("read\n");
return NULL;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(0 == res)
{
break;
}
}
pthread_exit(NULL);
}
void *callback_write(void *arg)
{
ssize_t res_w;
while(1)
{
pthread_mutex_lock(&mutex);
if(flag !=1)
{
pthread_cond_wait(&cond,&mutex);
}
res_w = write(1,temp,res);
if(res_w<0)
{
ERR_MSG("write\n");
return NULL;
}
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(0 == res_w)//写完
{
break;
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
fd = open("./text.c",O_RDONLY);
if(fd<0)
{
ERR_MSG("open");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback_read,NULL)<0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,callback_write,NULL)<0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd);
return 0;
}
用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。
#include "/home/ubuntu/li/IO/head.h"
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag = 1;
void *callback_A(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag !=1 )
{
pthread_cond_wait(&cond1,&mutex);
}
printf("A");
flag = 2;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *callback_B(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag !=2 )
{
pthread_cond_wait(&cond2,&mutex);
}
printf("B");
flag = 3;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *callback_C(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag !=3 )
{
pthread_cond_wait(&cond3,&mutex);
}
printf("C\n");
flag = 1;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,callback_A,NULL)<0)
{
fprintf(stderr,"pthread_create A failed\n");
return -1;
}
pthread_detach(tid1);
if(pthread_create(&tid2,NULL,callback_B,NULL)<0)
{
fprintf(stderr,"pthread_create B failed\n");
return -1;
}
pthread_detach(tid2);
if(pthread_create(&tid3,NULL,callback_C,NULL)<0)
{
fprintf(stderr,"pthread_create C failed\n");
return -1;
}
pthread_join(tid3,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
要求用信号量的方式实现,打印一次倒置一次。不允许使用flag。
#include "/home/ubuntu/li/IO/head.h"
char str[]="lzphuaqing";
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t sem_revcond;
sem_t sem_boolcond;
void strRev(char *p,int len)
{
char *q = p+len-1;
char temp;
while(p<q)
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
void *callback_print(void *arg)
{
while(1)
{
if(sem_wait(&sem_revcond)<0) //初始1
{
ERR_MSG("sem_wait");
break;
}
printf("%s\n",str);
if(sem_post(&sem_boolcond)<0) //初始0
{
ERR_MSG("sem_post");
break;
}
}
pthread_exit(NULL);
}
void *callback_rev(void *arg)
{
while(1)
{
if(sem_wait(&sem_boolcond)<0) //初始0
{
ERR_MSG("sem_wait");
break;
}
strRev(str,strlen(str));
if(sem_post(&sem_revcond) < 0)//初始1
{
perror("sem_post");
break;
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem_revcond,0,1)<0)
{
ERR_MSG("sem_init");
return -1;
}
if(sem_init(&sem_boolcond,0,0)<0)
{
ERR_MSG("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback_print,NULL)<0)
{
fprintf(stderr,"pthread_create print failed\n");
return -1;
}
if(pthread_create(&tid2,NULL,callback_rev,NULL)<0)
{
fprintf(stderr,"pthread_create print failed\n");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destroy(&sem_revcond);
sem_destroy(&sem_boolcond);
return 0;
}