1.用信号量的方式实现倒置线程打印线程顺序执行。
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
char buf[]="1234567";
sem_t sem,sem2;
//pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void* output(void* arg)
{
while(1)
{
//pthread_mutex_lock(&mutex);
if(sem_wait(&sem)<0)
{
perror("sem_wait");
break;
}
sleep(1);
printf("%s\n",buf);
//v操作
if(sem_post(&sem2)<0)
{
perror("sem_psot");
break;
}
}
pthread_exit(NULL);
}
void* revel(void* arg)
{
while(1)
{
//pthread_mutex_lock(&mutex);
if(sem_wait(&sem2)<0)
{
perror("sem_wait");
break;
}
char t;
int i=0,j=strlen(buf)-1;
while(i<j)
{
t=buf[i];
buf[i]=buf[j];
buf[j]=t;
i++;
j--;
}
//pthread_mutex_unlock(&mutex);
if(sem_post(&sem)<0)
{
perror("sem_psot");
break;
}
}
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(&sem2,0,0)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
//输出
if(pthread_create(&tid1,NULL,output,NULL)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
//逆置
if(pthread_create(&tid2,NULL,revel,NULL)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
pthread_detach(tid1);
pthread_join(tid2,NULL);
sem_destroy(&sem);
sem_destroy(&sem2);
return 0;
}
运行截图:
2.将一个文件中的数据打印到终端,类似cat一个文件,要求如下:
a.一个线程读取文件中的数据
b.另外一个线程打印文件中的数据
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<semaphore.h>
sem_t sem;//信号量
void* output(void* arg)
{
int fd_r=open("./09_pthread.c",O_RDONLY);
if(fd_r<0)
{
perror("open");
pthread_exit(NULL);
}
char c=0;
while(1)
{
//p操作
if(sem_wait(&sem)<0)
{
perror("sem_wait");
break;
}
int res=read(fd_r,&c,1);
if(0==res)
break;
for(int i=0;i<res;i++)
{
printf("%c",c);
}
//v操作
if(sem_post(&sem)<0)
{
perror("sem_post");
break;
}
}
close(fd_r);
pthread_exit(NULL);
}
void* revel(void* arg)
{
int fd_r=open("./01_pthread.c",O_RDONLY);
if(fd_r<0)
{
perror("open");
pthread_exit(NULL);
}
int fd_w=open("./09_pthread.c",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd_w<0)
{
perror("open");
pthread_exit(NULL);
}
char c=0;
while(1)
{
//p操作
if(sem_wait(&sem)<0)
{
perror("sem_wait");
break;
}
int res=read(fd_r,&c,1);
if(0==res)
{
printf("文件读取完毕\n");
break;
}
else if(res<0)
{
perror("read");
return NULL;
}
write(fd_w,&c,res);
//v操作
if(sem_post(&sem)<0)
{
perror("sem_post");
break;
}
}
close(fd_r);
close(fd_w);
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,output,NULL)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
//读取文件数据
if(pthread_create(&tid2,NULL,revel,NULL)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
pthread_detach(tid1);
pthread_join(tid2,NULL);
//销毁信号量
sem_destroy(&sem);
return 0;
}
运行截图:
3.现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
//互斥锁
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; //0-a,1-b,2-c
void* th_A(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
//当不是当前线程要访问的时机,则让当前线程休眠
if(0!=flag)
{
pthread_cond_wait(&cond1,&mutex);
}
printf("%c",*(char*)arg);
flag=1;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* th_B(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
//当不是当前线程要访问的时机,则让当前线程休眠
if(1!=flag)
{
pthread_cond_wait(&cond2,&mutex);
}
printf("%c",*(char*)arg);
flag=2;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* th_C(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
//当不是当前线程要访问的时机,则让当前线程休眠
if(2!=flag)
{
pthread_cond_wait(&cond3,&mutex);
}
printf("%c\n",*(char*)arg);
flag=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc,const char *argv[])
{
pthread_t tid1,tid2,tid3;
char name1='a',name2='b',name3='c';
int *p1=(int *)&name1;
int *p2=(int *)&name2;
int *p3=(int *)&name3;
if(pthread_create(&tid1,NULL,th_A,(void*)p1)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid1,NULL,th_B,(void*)p2)!=0)
{
fprintf(stderr,"pthread_create failed!! __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid1,NULL,th_C,(void*)p3)!=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(&cond1); //销毁条件变量
pthread_cond_destroy(&cond2); //销毁条件变量
pthread_cond_destroy(&cond3); //销毁条件变量
return 0;
}
运行截图: