1.将一个文件中的数据打印到终端上,类似cat一个文件。要求如下
a.线程读取文件中的数据
b.线程将A线程读取到的数据打印到终端上
c.文件打印完毕后,结束进程。
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdlib.h>
char buf[128] = "";
sem_t sem;
void* callback1(void* arg)
{
FILE *p = fopen("./07.c","r");
if(NULL == p)
{
perror("fopen");
pthread_exit(NULL);
}
while(1)
{
//P操作
if(sem_wait(&sem) < 0)
{
perror("sem_wait");
break;
}
//读文件内容
if(fgets(buf,sizeof(buf),p) == NULL)
{
sem_post(&sem);
break;
}
//V操作
if(sem_post(&sem) < 0)
{
perror("sem_post");
break;
}
fclose(p);
pthread_exit(NULL);
}
}
void* callback2(void* arg)
{
while(1)
{
//P操作
if(sem_wait(&sem) < 0)
{
perror("sem_wait");
break;
}
//打印到终端
if(strlen(buf) == 0)
break;
printf("%s",buf);
fflush(stdout);
bzero(buf,sizeof(buf));
//V操作
if(sem_post(&sem) < 0)
{
perror("sem_post");
break;
}
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,callback1,NULL)!=0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_detach(tid1);
//创建线程,打印到终端
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid2,NULL);
sem_destroy(&sem);
return 0;
}
2.用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。
提示:多个条件变量
#include <stdio.h>
#include <string.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int flag = 0;//0为A,1为B,2为C
int i=0;
void* fun1(void* arg)
{
while(i<1)
{
//上锁
pthread_mutex_lock(&mutex);
if(0!=flag)
{
pthread_cond_wait(&cond,&mutex);
}
else
{
printf("A\n");
fflush(stdout);
flag=1;
}
//唤醒cond上的线程
pthread_cond_signal(&cond);
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* fun2(void* arg)
{
while(i<1)
{
//上锁
pthread_mutex_lock(&mutex);
if(1!=flag)
{
pthread_cond_wait(&cond,&mutex);
}
else
{
printf("B\n");
fflush(stdout);
flag=2;
}
//唤醒cond上的线程
pthread_cond_signal(&cond);
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* fun3(void* arg)
{
while(i<1)
{
//上锁
pthread_mutex_lock(&mutex);
if(2!=flag)
{
pthread_cond_wait(&cond,&mutex);
}
else
{
printf("C\n");
fflush(stdout);
flag=0;
i++;
}
//唤醒cond上的线程
pthread_cond_signal(&cond);
//解锁
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(&cond,NULL)!=0)
{
fprintf(stderr,"pthread_cond_init failed __%d__\n",__LINE__);
return -1;
}
pthread_t A,B,C;
//创建线程A
if(pthread_create(&A, NULL, fun1, NULL) != 0)
{
fprintf(stderr, "pthread_create failed\n");
return -1;
}
//创建线程B
if(pthread_create(&B, NULL, fun2, NULL) != 0)
{
fprintf(stderr, "pthread_create failed\n");
return -1;
}
//创建线程C
if(pthread_create(&C, NULL, fun3, NULL) != 0)
{
fprintf(stderr, "pthread_create failed\n");
return -1;
}
pthread_join(A,NULL);
pthread_join(B,NULL);
pthread_join(C,NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond);
return 0;
}
3.要求用信号量的方式实现,打印一次倒置一次
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
char arr[]="1234567";
sem_t sem1, sem2;
void* callback1(void* arg)
{
while(1)
{
//sem1 P操作
if(sem_wait(&sem1) < 0)
{
perror("sem_wait");
break;
}
printf("%s\n", arr);
//sem2 V操作
if(sem_post(&sem2) < 0)
{
perror("sem_post");
break;
}
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
char temp = 0;
//sem2 P操作
while(1)
{
if(sem_wait(&sem2) < 0)
{
perror("sem_wait");
break;
}
//倒置
for(int i=0;i<strlen(arr)/2;i++)
{
temp=arr[i];
arr[i]=arr[strlen(arr)-i-1];
arr[strlen(arr)-i-1]=temp;
}
//sem1 V操作
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)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2,0,1) < 0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
//创建线程,打印
if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
{
fprintf(stderr, "pthread_create failed\n");
return -1;
}
pthread_detach(tid1);
//创建线程,倒置
if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
{
fprintf(stderr, "pthread_create failed\n");
return -1;
}
pthread_join(tid2,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}