1> 将一个文件中的数据打印到终端上,类似cat一个文件。要求如下
a. A线程读取文件中的数据
b. B线程将A线程读取到的数据打印到终端上
c. 文件打印完毕后,结束进程。
2> 用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。
提示:用多个条件变量
3> 要求用信号量的方式实现,打印一次倒置一次。不允许使用flag。
提示:用多个信号量
第一题代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int flag = 0;
char a=0;
int fd_r,res;
long cur;
void* callback1(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&cond, &mutex);
}
int fd_r=open("./moban.c",O_RDONLY);
lseek(fd_r,cur,SEEK_SET);
res=read(fd_r,&a,sizeof(a));
cur=lseek(fd_r,0,SEEK_CUR);
close(fd_r);
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(0==res)
break;
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&cond, &mutex);
}
write(1,&a,res);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
if(0==res)
break;
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(pthread_mutex_init(&mutex, NULL) != 0)
{
fprintf(stderr, "pthread_mutex_init failed __%d__\n", __LINE__);
return -1;
}
if(pthread_cond_init(&cond, NULL) != 0)
{
fprintf(stderr, "pthread_cond_init failed __%d__\n", __LINE__);
return -1;
}
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, callback1, NULL)!=0||pthread_create(&tid2, NULL, callback2, 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;
}
第二题代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t condA,condB,condC;
int flag = 0;
pthread_t tid1,tid2,tid3;
void* A(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&condA, &mutex);
}
printf("A线程号:%ld\n",tid1);
flag = 1;
pthread_cond_signal(&condB);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* B(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&condB, &mutex);
}
printf("B线程号:%ld\n",tid2);
flag = 2;
pthread_cond_signal(&condC);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *C(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag!=2)
{
pthread_cond_wait(&condC,&mutex);
}
printf("C线程号:%ld\n",tid3);
flag=0;
pthread_cond_signal(&condA);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(pthread_mutex_init(&mutex, NULL) != 0)
{
fprintf(stderr, "pthread_mutex_init failed __%d__\n", __LINE__);
return -1;
}
if(pthread_cond_init(&condA, NULL) != 0||pthread_cond_init(&condA, NULL) != 0\
||pthread_cond_init(&condA, NULL) != 0)
{
fprintf(stderr, "pthread_cond_init failed __%d__\n", __LINE__);
return -1;
}
if(pthread_create(&tid1, NULL, A, NULL)!=0||pthread_create(&tid2, NULL,B, NULL) != 0\
||pthread_create(&tid3, NULL,C, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condA);
pthread_cond_destroy(&condB);
pthread_cond_destroy(&condC);
return 0;
}
第三题带
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
sem_t sem1,sem2;
char buf[]="1234567";
void *Printf(void *arg)
{
while(1)
{
if(sem_wait(&sem1)<0)
{
perror("sem_wait");
break;
}
printf("%s\n",buf);
if(sem_post(&sem2)<0)
{
perror("sem_post");
break;
}
}
pthread_exit(NULL);
}
void *Rev(void *arg)
{
while(1)
{
if(sem_wait(&sem2)<0)
{
perror("sem_wait");
break;
}
char temp;
int i=0,j=strlen(buf)-1;
while(i<j)
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
i++;j--;
}
if(sem_post(&sem1)<0)
{
perror("sem_post");
break;
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(sem_init(&sem1,0,1)<0||sem_init(&sem2,0,0)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,Rev,NULL)<0||pthread_create(&tid2,NULL,Printf,NULL)<0)
{
printf("线程创建失败 %d\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}