第一题:
1.将一个文件中的数据打印到终端上,类似cat一个文件。要求如下
a.A线程读取文件中的数据
b.B线程将A线程读取到的数据打印到终端上
c.文件打印完毕后,结束进程。
代码
1:使用条件变量和互斥锁
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
char c=0;
FILE* fb ;
int flag=1;
void* callBackread(void* arg)
{
fb= fopen("./zy1.c","r");
if(NULL == fb)
{
perror("fopen");
return NULL;
}
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&cond,&mutex);
}
c=fgetc(fb);
if(EOF == c)
{
printf("---------------------\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
break;
}
flag=0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
fclose(fb);
pthread_exit(NULL);
}
void* callBackprint(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&cond,&mutex);
}
if(c==EOF)
{
printf("_______________\n");
break;
}
fprintf(stdout,"%c",c);
flag=1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
// if(sem_init(&sem,0,1)<0)
/// {
// perror("sem_init:");
// return -1;
// }
//
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callBackread,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBackprint,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
2:使用信号量
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
sem_t sem,sem1;
char c=0;
FILE* fb ;
void* callBackread(void* arg)
{
while(1)
{
sem_wait(&sem);
c=fgetc(fb);
if(EOF==c)
{
sem_post(&sem1);
break;
}
sem_post(&sem1);
}
pthread_exit(NULL);
}
void* callBackprint(void* arg)
{
while(1)
{
if(c==EOF)
{
break;
}
sem_wait(&sem1);
fprintf(stdout,"%c",c);
sem_post(&sem);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem,0,1)<0)
{
perror("sem_init:");
return -1;
}
if(sem_init(&sem1,0,0)<0)
{
perror("sem1_init:");
return -1;
}
pthread_t tid1,tid2;
fb = fopen("./zy1.c","r");
if(fb==NULL)
{
perror("fopen");
return -1;
}
if(pthread_create(&tid1,NULL,callBackread,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBackprint,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
fclose(fb);
sem_destroy(&sem);
sem_destroy(&sem1);
return 0;
}
输出结果:
第二题:
用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC.
a.提示:多个条件变量
代码段:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
sem_t sem1,sem2,sem3;
void* callBackA(void* arg)
{
while(1)
{
sem_wait(&sem1);
printf("A");
sem_post(&sem2);
}
pthread_exit(NULL);
}
void* callBackB(void* arg)
{
while(1)
{
sem_wait(&sem2);
printf("B");
sem_post(&sem3);
}
pthread_exit(NULL);
}
void* callBackC(void* arg)
{
while(1)
{
sem_wait(&sem3);
printf("C");
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2,0,0)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem3,0,0)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,callBackA,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callBackB,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid3,NULL,callBackC,NULL)<0)
{
fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
输出结果:
第三题:
要求用信号量的方式实现,打印一次倒置一次。不允许使用flag
代码段
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
char buf[]="1234567";
//信号量
sem_t sem;
sem_t sem1;
void* callback1(void *arg)
{
char t = 0;
int n = strlen(buf)-1;
while(1)
{
for(int i = 0;i<n/2;i++)
{
t = buf[i];
buf[i] = buf[n-i];
buf[n-i] = t;
}
sem_post(&sem);
sem_wait(&sem1);
}
pthread_exit(NULL);
}
void* callback(void *arg)
{
while(1)
{
sem_wait(&sem);
printf("buf = %s\n",buf);
sem_post(&sem);
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem1,0,0)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_t tid1;
if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid,NULL);
sem_destroy(&sem);
sem_destroy(&sem1);
return 0;
}
输出结果: