将一个文件中的数据打印到终端上,类似cat一个文件。要求如下
A线程读取文件中的数据
B线程将A线程读取到的数据打印到终端上
C文件打印完毕后,结束进程。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
void* readFile(void* arg);
void* printFile(void* arg);
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
char buf[N];
int flag=0;
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
char name[20];
strcpy(name,argv[1]);
if(pthread_create(&tid1,NULL,readFile,name)||\
pthread_create(&tid2,NULL,printFile,NULL))
{
fprintf(stderr,"pthread_create failed");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void* readFile(void* arg)
{
char *pathname=(char*)arg;
FILE *fp;
char *tmp;
if (!(fp=fopen(pathname,"r")))
{
perror("open");
exit(0);
}
pthread_mutex_lock(&mutex);
while(1)
{
if(flag)
{
pthread_cond_wait(&cond,&mutex);
}
tmp=fgets(buf,N,fp);
if(!tmp)
{
bzero(buf,N);
}
flag=1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(!tmp)
{
break;
}
}
fclose(fp);
pthread_exit(NULL);
}
void* printFile(void* arg)
{
pthread_mutex_lock(&mutex);
int tmp;
while(1)
{
if(!flag)
{
pthread_cond_wait(&cond,&mutex);
}
tmp=printf("%s",buf);
flag=0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(tmp<=0)
break;
}
pthread_exit(NULL);
}
用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。
提示:多个条件变量
#include <stdio.h>
#include <pthread.h>
void* callBackA(void* arg);
void* callBackB(void* arg);
void* callBackC(void* arg);
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3=PTHREAD_COND_INITIALIZER;
int flag=0;
int main(int argc, const char *argv[])
{
pthread_t p1,p2,p3;
if(pthread_create(&p1,NULL,callBackA,NULL)||\
pthread_create(&p2,NULL,callBackB,NULL)||\
pthread_create(&p3,NULL,callBackC,NULL))
{
fprintf(stderr,"pthread_create failed\n");
}
pthread_join(p1,NULL);
pthread_join(p2,NULL);
pthread_join(p3,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
void* callBackA(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=0)
{
pthread_cond_wait(&cond1,&mutex);
}
printf("A\n");
flag=1;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBackB(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=1)
{
pthread_cond_wait(&cond2,&mutex);
}
printf("B\n");
flag=2;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBackC(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=2)
{
pthread_cond_wait(&cond3,&mutex);
}
printf("C\n");
flag=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
要求用信号量的方式实现,打印一次倒置一次。不允许使用flag。
提示:用多个信号量
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
char buf[]="1234567";
sem_t sem1,sem2;
void *pthreadA(void *arg);
void *pthreadB(void *arg);
int main(int argc, const char *argv[])
{
pthread_t tidA,tidB;
if (sem_init(&sem1,0,1)||sem_init(&sem2,0,0))
{
perror("sem_init");
return -1;
}
int size=strlen(buf);
if(pthread_create(&tidA,NULL,pthreadA,NULL)\
||pthread_create(&tidB,NULL,pthreadB,&size))
{
fprintf(stderr,"pthread_create failed\n");
}
pthread_join(tidA,NULL);
pthread_join(tidB,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
void *pthreadA(void *arg)
{
while (1)
{
if(sem_wait(&sem1)<0)
{
perror("sem_wait");
break;
}
printf("%s\n",buf);
if(sem_post(&sem2)<0)
{
perror("sem_wait");
break;
}
}
pthread_exit(NULL);
}
void *pthreadB(void *arg)
{
int size=*(int*)arg;
int tmp;
while(1)
{
if(sem_wait(&sem2)<0)
{
perror("sem_wait");
break;
}
for (int i=0; i<size/2; i++)
{
tmp=buf[i];
buf[i]=buf[size-1-i];
buf[size-1-i]=tmp;
}
if(sem_post(&sem1)<0)
{
perror("sem_wait");
break;
}
}
pthread_exit(NULL);
}