目录
1.编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推;提示:A只能叫醒B,B只能唤醒C,C只能唤醒A;
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include<unistd.h>
int flag=0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥锁
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;
int count=1;
void* callBack_printA(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag !=0)
{
pthread_cond_wait(&cond1,&mutex);
}
printf("A");
flag=1;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(NULL);
}
void* callBack_printB(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag !=1)
{
pthread_cond_wait(&cond2,&mutex);
}
printf("B");
flag=2;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(NULL);
}
void* callBack_printC(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex); //上锁
if(flag!=2 )
{
pthread_cond_wait(&cond3,&mutex);
}
printf("C\n");
if(count==10)
{
break;
}
count++;
flag=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex); //解锁
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建互斥锁,并初始化
if(pthread_mutex_init(&mutex, NULL) != 0)
{
perror("pthread_mutex_init");
return -1;
}
printf("mutex_init success\n");
if (pthread_cond_init(&cond1,NULL)!=0)
{
perror("pthread_cond_init");
return -1;
}
if (pthread_cond_init(&cond2,NULL)!=0)
{
perror("pthread_cond_init");
return -1;
}
if (pthread_cond_init(&cond3,NULL)!=0)
{
perror("pthread_cond_init");
return -1;
}
pthread_t tid1;
if(pthread_create(&tid1, NULL, callBack_printA, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_t tid2;
if(pthread_create(&tid2, NULL, callBack_printB, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_t tid3;
if(pthread_create(&tid3, NULL, callBack_printC, NULL) != 0)
{
perror("pthread_create");
return -1;
}
printf("线程创建成功\n");
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);
while(1);
return 0;
}
2.用信号量的方式,创建两个线程 A B
1)A线程读取文件中的内容
2)B线程打印A读取到的内容到终端,
3)全部打印完毕后,结束进程;
4)现象类似cat一个文件
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
//信号量
sem_t sem1,sem2;
int pic;
char str;
ssize_t res;
void* callBack_print(void* arg)
{
while(res=read(pic,&str,1))
{
sem_wait(&sem1); //P操作
sem_post(&sem2); //V操作
}
pthread_exit(NULL);
}
void* callBack_reserve(void* arg)
{
sem_wait(&sem2);
sem_post(&sem2);
while(res) //创建并初始化信号量
{
sem_wait(&sem2); //P操作
write(1,&str,1);
sem_post(&sem1); //V操作
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pic=open("./pthread_cond2.c",O_RDONLY );
if(pic < 0)
{
perror("open");
}
//创建并初始化信号量
if(sem_init(&sem1, 0, 1) != 0)
{
perror("sem1_init");
return -1;
}
if(sem_init(&sem2, 0, 0) != 0)
{
perror("sem2_init");
return -1;
}
printf("sem init success\n");
pthread_t tid1;
//创建一个线程,循环打印
if(pthread_create(&tid1, NULL, callBack_print, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_t tid2;
//创建一个线程,循环倒置
if(pthread_create(&tid2, NULL, callBack_reserve, NULL) != 0)
{
perror("pthread_create");
return -1;
}
printf("线程创建成功\n");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
// close(pic);
return 0;
}