思維導圖
多線程打印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;
}