今日任务:
1.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//创建互斥锁
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;//创建条件变量
int flag=0;//0为str空,1为有str值
char str[64]={0};
void *fun1(void*arg){
//读取
int fd_rd=open("./1.txt",O_RDONLY);
if(fd_rd==-1){
perror("open");
return NULL;
}
while(1){
//上锁
pthread_mutex_lock(&mutex);
if(flag==0){
memset(str,0,sizeof(str));
if(read(fd_rd,str,sizeof(str))==0){
flag=1;
memset(str,0,sizeof(str));
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
break;
}
flag=1;
pthread_cond_signal(&cond);
}else{
pthread_cond_wait(&cond,&mutex);
}
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *fun2(void*arg){
//写出
while(1){
pthread_mutex_lock(&mutex);
if(flag==1){
if(0==strlen(str)){//退出条件
puts("read end");
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
break;
}
printf("%s",str);
flag=0;
pthread_cond_signal(&cond);
}else{
pthread_cond_wait(&cond,&mutex);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建两个线程,一个读取,一个写出
pthread_t th1,th2;
if(pthread_create(&th1,NULL,fun1,NULL)!=0){
fprintf(stderr,"create failed");
return -1;
}
if(pthread_create(&th2,NULL,fun2,NULL)!=0){
fprintf(stderr,"create failed");
return -1;
}
pthread_join(th1,NULL);
pthread_join(th2,NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
//摧毁条件变量
pthread_cond_destroy(&cond);
return 0;
}
运行结果:
2.代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.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=1;
void *fun1(void*arg){
while(1){
//上锁
pthread_mutex_lock(&mutex);
if(flag==1){
printf("A");
flag=2;
pthread_cond_signal(&cond2);
}else{
pthread_cond_wait(&cond1,&mutex);
}
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *fun2(void*arg){
while(1){
//上锁
pthread_mutex_lock(&mutex);
if(flag==2){
printf("B");
flag=3;
pthread_cond_signal(&cond3);
}else{
pthread_cond_wait(&cond2,&mutex);
}
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *fun3(void*arg){
while(1){
//上锁
pthread_mutex_lock(&mutex);
if(flag==3){
printf("C");
flag=1;
pthread_cond_signal(&cond1);
}else{
pthread_cond_wait(&cond3,&mutex);
}
//解锁
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t th1,th2,th3;
if(pthread_create(&th1,NULL,fun1,NULL)!=0){
fprintf(stderr,"thread create");
return -1;
}
if(pthread_create(&th2,NULL,fun2,NULL)!=0){
fprintf(stderr,"thread create");
return -1;
}
if(pthread_create(&th3,NULL,fun3,NULL)!=0){
fprintf(stderr,"thread create");
return -1;
}
pthread_join(th1,NULL);
pthread_join(th2,NULL);
pthread_join(th3,NULL);
//销毁锁
pthread_mutex_destroy(&mutex);
//销毁条件变量
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
运行结果
3.代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
char buf[]="1234567";
sem_t sem1;
sem_t sem2;
void * fun1(void*arg){
while(1){
if(sem_wait(&sem1)!=0){
perror("fun1 wait");
return NULL;
}
//逆置
for (int i=0; i<sizeof(buf)/2; i++)
{
char temp=buf[i];
buf[i]=buf[strlen(buf)-1-i];
buf[strlen(buf)-1-i]=temp;
}
if(sem_post(&sem2)!=0){
perror("fun1 ");
}
}
}
void* fun2(void*arg){
while(1){
if(sem_wait(&sem2)!=0){
perror("fun1 wait");
return NULL;
}
printf("%s\n",buf);
if(sem_post(&sem1)!=0){
perror("fun1 ");
}
}
}
int main(int argc, const char *argv[])
{
//创建信号量
if(-1==sem_init(&sem1,0,1)){
perror("sem create");
}
if(-1==sem_init(&sem2,0,0)){
perror("sem create");
}
//创建进程
pthread_t th1,th2;
if(pthread_create(&th1,NULL,fun1,NULL)!=0){
fprintf(stderr,"th1 create failed");
return -1;
}
if(pthread_create(&th2,NULL,fun2,NULL)!=0){
fprintf(stderr,"th2 create failed");
return -1;
}
pthread_join(th1,NULL);
pthread_join(th2,NULL);
//销毁
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
运行结果:
今日思维导图
今天 的脑子不太够用