互斥锁
#include <myhead.h>
char buf[128];//创建临界资源
pthread_mutex_t mutex;//创建锁
void *task(void *arg)//分支线程
{while(1){pthread_mutex_lock(&mutex);//上锁printf("分支线程:buf=%s\n",buf);strcpy(buf,"I Love China");pthread_mutex_unlock(&mutex);//解锁}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//创建分支线程pthread_t tid;pthread_mutex_init(&mutex,NULL);//初始化锁if(pthread_create(&tid,NULL,task,NULL)!=0){printf("tid create error");return -1;}while(1){pthread_mutex_lock(&mutex);printf("主线程:buf=%s\n",buf);strcpy(buf,"hello world");pthread_mutex_unlock(&mutex);}pthread_join(tid,NULL);//回收分支线程pthread_mutex_destroy(&mutex);//销毁锁return 0;
}
无名信号量
#include <myhead.h>
char buf[128];//创建临界资源
sem_t sem;//创建无名信号量
void *task(void *arg)//分支线程
{while(1){sleep(1);sem_post(&sem);//释放资源printf("我生产了一辆特斯拉\n");}pthread_exit(NULL);
}
void *task2(void *arg)//分支线程
{while(1){sem_wait(&sem);//申请资源printf("我消费了一辆特斯拉\n");}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//创建分支线程pthread_t tid,tid2;sem_init(&sem,0,0);//初始化无名信号量if(pthread_create(&tid,NULL,task,NULL)!=0){printf("tid create error");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error");return -1;}pthread_join(tid2,NULL);pthread_join(tid,NULL);//回收分支线程sem_destroy(&sem);//销毁无名信号量return 0;
}
三个线程拷贝
#include <myhead.h>
void *task1(void *arg)
{int len=*((int *)arg);int fd,cfd;if((fd=open("./test.bmp",O_RDONLY))==-1){perror("open error");}if((cfd=open("./aa.bmp",O_WRONLY))==-1){perror("open error");}char buf[128]="";int res;int sum=0;lseek(fd,0,SEEK_SET);lseek(cfd,0,SEEK_SET);while((res=read(fd,buf,sizeof(buf)))!=0){sum+=res;if(sum>=len){write(cfd,buf,res-(sum-len));break;}write(cfd,buf,res);}close(fd);close(cfd);pthread_exit(NULL);
}
void *task2(void *arg)
{int len=*((int *)arg);int fd,cfd;if((fd=open("./test.bmp",O_RDONLY))==-1){perror("open error");}if((cfd=open("./aa.bmp",O_WRONLY))==-1){perror("open error");}char buf[128]="";int res;lseek(fd,len,SEEK_SET);lseek(cfd,len,SEEK_SET);while((res=read(fd,buf,sizeof(buf)))!=0){write(cfd,buf,res);}close(fd);close(cfd);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{int fd,cfd;if((fd=open("./test.bmp",O_RDONLY))==-1){perror("open error");return -1;}if((cfd=open("./aa.bmp",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1){perror("open error");return -1;}int len=lseek(fd,0,SEEK_END)/2;pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,task1,&len)!=0){printf("tid1 create error\n");return -1;}if(pthread_create(&tid2,NULL,task2,&len)!=0){printf("tid2 create error\n");return -1;}close(fd);close(cfd);pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
三个线程输出ABC
#include <myhead.h>
char buf[128];//创建临界资源
sem_t sem1;//创建无名信号量
sem_t sem2;//创建无名信号量
sem_t sem3;//创建无名信号量
void *task(void *arg)//分支线程
{while(1){sem_wait(&sem1);//申请资源printf("A");sem_post(&sem2);//释放资源}pthread_exit(NULL);
}
void *task2(void *arg)//分支线程
{while(1){sem_wait(&sem2);//申请资源printf("B");sem_post(&sem3);//释放资源}pthread_exit(NULL);
}
void *task3(void *arg)//分支线程
{while(1){sem_wait(&sem3);//申请资源printf("C\n");sem_post(&sem1);//释放资源}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{//创建分支线程pthread_t tid,tid2,tid3;sem_init(&sem1,0,1);//初始化无名信号量sem1sem_init(&sem2,0,0);//初始化无名信号量sem2sem_init(&sem3,0,0);//初始化无名信号量sem3if(pthread_create(&tid,NULL,task,NULL)!=0){printf("tid create error");return -1;}if(pthread_create(&tid2,NULL,task2,NULL)!=0){printf("tid2 create error");return -1;}if(pthread_create(&tid3,NULL,task3,NULL)!=0){printf("tid3 create error");return -1;}pthread_join(tid,NULL);//回收分支线程pthread_join(tid2,NULL);//回收分支线程pthread_join(tid3,NULL);//回收分支线程sem_destroy(&sem1);//销毁无名信号量sem1sem_destroy(&sem2);//销毁无名信号量sem2sem_destroy(&sem3);//销毁无名信号量sem3return 0;
}