IO进程线程;多线程;线程互斥同步;互斥锁;无名信号量;条件变量;0905
思维导图
多线程打印ABC
运用无名面量 实现进程同步
#include<myhead.h>
//定义 无名信号量
sem_t sem1;
sem_t sem2;
sem_t sem3;
//线程1
void* task1(void *arg)
{while(1){sem_wait(&sem1);printf("A");fflush(stdout);sleep(1);sem_post(&sem2);}
}
//线程2
void* task2(void *arg)
{while(1){sem_wait(&sem2);printf("B");fflush(stdout);sleep(1);sem_post(&sem3);}
}
//线程3
void* task3(void *arg)
{while(1){sem_wait(&sem3);printf("C");fflush(stdout);sleep(1);sem_post(&sem1);}
}
int main()
{//初始化无名信号量sem_init(&sem1,0,1);sem_init(&sem2,0,0);sem_init(&sem3,0,0);pthread_t tid1,tid2,tid3;pthread_create(&tid1,NULL,task1,NULL);pthread_create(&tid2,NULL,task2,NULL);pthread_create(&tid3,NULL,task3,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);//销毁 无名信号sem_destroy(&sem1);sem_destroy(&sem1);sem_destroy(&sem3);return 0;
}
一个生产者和多个消费者
互斥锁
#include<myhead.h>//创建互斥锁
pthread_mutex_t mutex;
//创建 条件变量 等待队列
pthread_cond_t cond;
//线程体 生产者
void *task(void *arg)
{//for(int i=0;i<5;i++)//{sleep(1);//printf("富士康公司组装了一台iPhone手机\n");printf("富士康公司组装了三台iPhone手机\n");//将等待队列中的 线程唤醒//唤醒等待队列中的第一个线程//pthread_cond_signal(&cond);//唤醒等待队列中的所有线程pthread_cond_broadcast(&cond);//}
}
//线程体1 消费者
void *task1(void *arg)
{//获取 互斥锁pthread_mutex_lock(&mutex);//进入等待队列 条件变量pthread_cond_wait(&cond,&mutex);printf("我将购买一台iPhone手机\n");//释放互斥锁pthread_mutex_unlock(&mutex);
}int main()
{//初始化互斥锁pthread_mutex_init(&mutex,NULL);//初始化 条件变量pthread_cond_init(&cond,NULL);//创建多线程pthread_t tid;//生产者线程pthread_t tid1;//和他的 三个消费线程pthread_t tid2;pthread_t tid3;//多线程pthread_create(&tid,NULL,task,NULL);pthread_create(&tid1,NULL,task1,NULL);pthread_create(&tid2,NULL,task1,NULL);pthread_create(&tid3,NULL,task1,NULL);//阻塞回收线程资源pthread_join(tid,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);//销毁互斥锁pthread_mutex_destroy(&mutex);//销毁 信号变量pthread_cond_destroy(&cond);return 0;
}
多线程文件拷贝
尝试用 互斥锁 实现
线程a拷贝前一半
线程b拷贝后一半
认为是,线程传参,出现了错误,我能力有限写不出来
#include<myhead.h>
//创建互斥锁
pthread_mutex_t mutex;//源文件 目标文件 结构体
struct copy
{char w[128];char r[128];
};
//线程结构体1 拷贝前一半文件
void *task1(void *arg)
{//申请 互斥锁pthread_mutex_lock(&mutex);//打开文件//打开源文件int rfd=open((struct copy *)arg->r,O_RDONLY);//求文件的大小int S=lseek(rfd,0,SEEK_END);//再把光标移动到开头lseek(rfd,0,SEEK_SET);//打开 目标文件int wfd=open((struct copy *)arg->w,O_WRONLY|O_CREAT|O_TRUNC,0664);//不存在就创建 存在就清空//拷贝前一半文件for(int i=0;i<S/2;i++){char buff;read(rfd,&buff,1);write(wfd,&buff,1);}//关闭文件close(rfd); close(wfd); //释放 互斥锁pthread_mutex_unlock(&mutex);
}
//线程结构体2 拷贝后一半文件
void *task2(void *arg)
{//申请 互斥锁pthread_mutex_lock(&mutex);//打开文件//打开源文件int rfd=open((struct copy *)arg->r,O_RDONLY);//求文件的大小int S=lseek(rfd,0,SEEK_END);//再把光标移动到一半的位置lseek(rfd,S/2,SEEK_SET);//打开 目标文件int wfd=open((struct copy *)arg->w,O_WRONLY|O_CREAT|O_TRUNC,0664);//不存在就创建 存在就清空//拷后一半文件char buff;while(read(rfd,&buff,1)){write(wfd,&buff,1);}//关闭文件close(rfd);close(wfd);//释放 互斥锁pthread_mutex_unlock(&mutex);
}int main(int argc,const char *argv[])
{//初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建多线程pthread_t tid1;pthread_t tid2;//多线程 函数传参数 结构体struct copy word;struct copy *arg=&word;strcpy(arg->r,argv[1]);strcpy(arg->w,*argv[2]);pthread_create(&tid1,NULL,task1,arg);pthread_create(&tid2,NULL,task2,arg);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}