问题1
要求
编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10
遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推;
提示:A只能叫醒B,B只能唤醒C,C只能唤醒A
代码
头文件和全局变量
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>
#define PER(per) { fprintf(stderr,"line:%d ",__LINE__);\
perror(per);\
return -1;}
//全局变量
int flag=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; //定义互斥锁
pthread_cond_t cond=PTHREAD_COND_INITIALIZER; //定义条件变量
输出A
void* funca(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag!=0)
{
pthread_cond_wait(&cond,&mutex);//睡眠
}
else
{
printf("A ");
flag=1;
}
pthread_cond_signal(&cond);//唤醒
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
输出B
void* funcb(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag!=1)
{
pthread_cond_wait(&cond,&mutex);//睡眠
}
else
{
printf("B ");
flag=2;
}
pthread_cond_signal(&cond);//唤醒
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
输出C
void* funcc(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag!=2)
{
pthread_cond_wait(&cond,&mutex);//睡眠
}
else
{
printf("C\n");
flag=0;
}
pthread_cond_signal(&cond);//唤醒
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
主函数
int main(int argc, const char *argv[])
{
//创建3个分线程
pthread_t tid1,tid2,tid3;
if(pthread_create(&tid1,NULL,funca,NULL)!=0)
{
PER("pthread_create")
}
if(pthread_create(&tid2,NULL,funcb,NULL)!=0)
{
PER("pthread_create")
}
if(pthread_create(&tid3,NULL,funcc,NULL)!=0)
{
PER("pthread_create")
}
//销毁子进程
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
问题2
用信号量的方式,创建两个线程 A B
要求
- A线程读取文件中的内容
- B线程打印A读取到的内容到终端,
- 全部打印完毕后,结束进程;
- 现象类似cat一个文件
代码
头文件和全局变量
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<semaphore.h>
#include<unistd.h>
#include<pthread.h>
#define PER(per) {fprintf(stderr,"line:%d ",__LINE__);\
perror(per);\
return -1;}
#define PER1(per) {fprintf(stderr,"line:%d ",__LINE__);\
perror(per);\
return NULL;}
//全局变量
int fd_r;
sem_t sem1,sem2;
char buff[20]="";
ssize_t res=1;
读取函数
void* func_r(void* arg)
{
while(1)
{
//P操作
if(sem_wait(&sem1)<0)
{
PER1("sem_wait")
}
//读取操作
res=read(fd_r,buff,sizeof(buff));
if(res==0)
{
break;
}
if(res==-1)
{
PER1("read")
}
//V操作
if(sem_post(&sem2)<0)
{
PER1("sem_post")
}
}
pthread_exit(NULL);
}
输出函数
//输出函数
void* func_w(void* arg)
{
while(1)
{
//P操作
if(sem_wait(&sem2)<0)
{
PER1("sem_wait")
}
//输出操作
if(write(1,buff,res)<0)
{
PER1("write")
}
//V操作
if(sem_post(&sem1)<0)
{
PER1("sem_post")
}
}
pthread_exit(NULL);
}
主函数
//主函数
int main(int argc, const char *argv[])
{
//创建2个信号量
if(sem_init(&sem1,0,1)<0)
{
PER("sem_init")
}
if(sem_init(&sem2,0,0)<0)
{
PER("sem_init")
}
//打开源文件
fd_r=open(argv[1],O_RDONLY);
if(fd_r==-1)
{
PER("open")
}
//创建AB线程
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,func_r,NULL))
{
PER("pthread_create")
}
if(pthread_create(&tid2,NULL,func_w,NULL))
{
PER("pthread_create")
}
//释放线程资源,防止主线程结束
pthread_join(tid1,NULL);
pthread_cancel(tid2);
pthread_join(tid2,NULL);
//关闭源文件
if(close(fd_r)==-1)
{
PER("close")
}
//销毁信号量
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}