網(wǎng)站建設(shè)需要公司谷歌搜索引擎免費
作業(yè)1:
????????多線程實現(xiàn)文件拷貝,線程1拷貝一半,線程2拷貝另一半,主線程回收子線程資源。
代碼:
#include <myhead.h>
sem_t sem1;
void *copy1()//子線程1函數(shù) 拷貝前一半內(nèi)容
{int fd1=open("./1.txt",O_RDONLY);int fd2=open("./2.txt",O_CREAT|O_RDWR|O_APPEND,0664);if(fd1==-1){perror("open");}if(fd2==-1){perror("open");}char s[100];int sum1=0,res1=0;int len=lseek(fd1,0,SEEK_END);//統(tǒng)計文件字節(jié)數(shù)lseek(fd1,0,SEEK_SET);//統(tǒng)計完光標要返回while(1){res1=read(fd1,s,sizeof(s));sum1+=res1;//每讀一次,把讀取長度加起來if(res1==0||sum1>len/2){int k=res1-(sum1-len/2);//該次讀取字節(jié)數(shù)-超過len/2的字節(jié)數(shù)write(fd2,s,k);break;}write(fd2,s,sizeof(s));//把讀取到的字節(jié)寫入2.txt}printf("線程1拷貝完成\n");close(fd1);close(fd2);sem_post(&sem1);pthread_exit(NULL);
}
void *copy2()//子線程2函數(shù) 拷貝后一半內(nèi)容
{sem_wait(&sem1);int fd1=open("./1.txt",O_RDONLY);int fd2=open("./2.txt",O_CREAT|O_RDWR|O_APPEND,0664);if(fd1==-1){perror("open");}if(fd2==-1){perror("open");}int fd3;int res2=0;int len=lseek(fd1,0,SEEK_END);//統(tǒng)計文件字節(jié)數(shù)lseek(fd1,0,SEEK_SET);//統(tǒng)計完光標要返回dup2(fd1,fd3);//fd3重定向fd1lseek(fd3,len/2,SEEK_SET);//fd3光標移動中間char s[100];while(1){res2=read(fd3,s,sizeof(s));if(res2==0){break;}write(fd2,s,res2);}printf("線程2拷貝完成\n");close(fd1);close(fd2);close(fd3);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2;sem_init(&sem1,0,0);if(pthread_create(&tid1,NULL,copy1,NULL)){perror("pthread_create");return -1;}if(pthread_create(&tid2,NULL,copy2,NULL)){perror("pthread_create");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);printf("回收完成\n");sem_destroy(&sem1);return 0;
}
運行測試結(jié)果:
作業(yè)2:線程同步之條件變量
????????生產(chǎn)者先讓消費者組成一個隊列,生產(chǎn)者生產(chǎn)了一臺勞斯萊斯,喚醒第一個消費者來消費,然后再生產(chǎn)第二臺勞斯萊斯,喚醒第二個消費者來消費,這樣可以精確化的控制生產(chǎn)者線程和消費者線程的協(xié)同
代碼:
#include <myhead.h>
#define MAX 10
pthread_cond_t cond;
pthread_mutex_t mtx;
sem_t sem;
int n=0,count=0;
void *producer()
{for(int i=0;i<MAX;i++){sem_wait(&sem);//用條件變量卡一下n++;printf("生產(chǎn)了一臺特斯拉%d\n",n);pthread_cond_signal(&cond);//喚醒一個等待線程}pthread_exit(NULL);
}
void *consumer()
{pthread_mutex_lock(&mtx);//進入后鎖住pthread_cond_wait(&cond,&mtx);//條件變量每接受一次進入后重新上鎖count++;printf("消費了一臺特斯拉%d\n",count);usleep(200000);pthread_mutex_unlock(&mtx);sem_post(&sem);//條件變量放行pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2[MAX];pthread_cond_init(&cond,NULL);//條件變量pthread_mutex_init(&mtx,NULL);//互斥鎖sem_init(&sem,0,1);//條件變量if(pthread_create(&tid1,NULL,producer,NULL)!=0){perror("pthread_create");return -1;}for(int i=0;i<MAX;i++){if(pthread_create(&tid2[i],NULL,consumer,NULL)!=0){perror("pthread_create");return -1; }}pthread_join(tid1,NULL);for(int i=0;i<MAX;i++){pthread_join(tid2[i],NULL);}pthread_mutex_destroy(&mtx);pthread_cond_destroy(&cond);return 0;
}
運行測試結(jié)果:
作業(yè)3:
互斥鎖,無名信號量,條件變量再練習(xí)一遍
1.互斥鎖代碼如何實現(xiàn)
代碼:
#include <myhead.h>
pthread_mutex_t mtx;//定義互斥鎖
void *fun(void *n)
{pthread_mutex_lock(&mtx);//上鎖printf("%d\n",*(int *)n);pthread_mutex_unlock(&mtx);//解鎖pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2;int num1=1,num2=2;pthread_mutex_init(&mtx,NULL);//初始化互斥鎖if(pthread_create(&tid1,NULL,fun,&num1))//子線程1{perror("pthread_create");return -1;}if(pthread_create(&tid2,NULL,fun,&num2))//子線程2{perror("pthread_create");return -1;}pthread_join(tid1,NULL);//阻塞回收線程pthread_join(tid2,NULL);pthread_mutex_destroy(&mtx);//毀鎖return 0;
}
運行測試結(jié)果:
2.無名信號量
代碼:
#include <myhead.h>
#define MAX 5
sem_t sem;//定義無名信號量
int n=0;
void *producer()
{for(int i=0;i<MAX;i++){n++;printf("生產(chǎn)了特斯拉%d\n",n);}sem_post(&sem);//釋放無名變量pthread_exit(NULL);
}
void *consumer()
{sem_wait(&sem);//申請無名變量printf("消費了一臺特斯拉%d\n",n);n--;sem_post(&sem);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid[MAX];sem_init(&sem,0,0);//初始化無名信號量if(pthread_create(&tid1,NULL,producer,&n)!=0){perror("pthread_create");return -1;}for(int i=0;i<MAX;i++){if(pthread_create(&tid[i],NULL,consumer,&n)!=0){perror("pthread_create");return -1;}}pthread_join(tid1,NULL);for(int i=0;i<MAX;i++){pthread_join(tid[i],NULL);}sem_destroy(&sem);//銷毀無名變量return 0;
}
運行測試結(jié)果:
3.條件變量
見作業(yè)2