中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

網(wǎng)站建設(shè)需要公司谷歌搜索引擎免費

網(wǎng)站建設(shè)需要公司,谷歌搜索引擎免費,松原做公司網(wǎng)站,wordpress 特色圖片尺寸作業(yè)1&#xff1a; 多線程實現(xiàn)文件拷貝&#xff0c;線程1拷貝一半&#xff0c;線程2拷貝另一半&#xff0c;主線程回收子線程資源。 代碼&#xff1a; #include <myhead.h> sem_t sem1; void *copy1()//子線程1函數(shù) 拷貝前一半內(nèi)容 {int fd1open("./1.txt",O…

作業(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

Xmind知識點:

http://www.risenshineclean.com/news/8859.html

相關(guān)文章:

  • 移動互聯(lián)網(wǎng)應(yīng)用程序指的是什么搜索引擎營銷優(yōu)化診斷訓(xùn)練
  • 棗陽網(wǎng)站建設(shè)_棗陽山水數(shù)碼網(wǎng)站制作定制
  • 自我簡介網(wǎng)頁模板htmlseo如何建立優(yōu)化網(wǎng)站
  • wordpress文章尾部做搜索引擎優(yōu)化的企業(yè)
  • wordpress幻燈片教程視頻seo人員是什么意思
  • 自助個人網(wǎng)站網(wǎng)絡(luò)推廣外包業(yè)務(wù)銷售
  • 揭陽網(wǎng)站制作專業(yè)適合seo的網(wǎng)站
  • 營銷型網(wǎng)站建設(shè)費用怎么這么大網(wǎng)絡(luò)營銷渠道
  • 賣模具做哪個網(wǎng)站好地推的60種方法
  • 華為云 wordpress微信搜一搜seo
  • 網(wǎng)站建設(shè)與發(fā)布百度推廣怎么優(yōu)化排名
  • 網(wǎng)站運營軟件站長工具seo綜合查詢可以訪問
  • 設(shè)計師網(wǎng)上接單的網(wǎng)站三只松鼠有趣的軟文
  • 淘客推廣有用嗎店鋪seo是什么意思
  • 沈陽市有做網(wǎng)站的公司百度推廣北京總部電話
  • 建設(shè)網(wǎng)站免費模板百度問答庫
  • 運城鹽湖區(qū)姚孟信通網(wǎng)站開發(fā)中心專業(yè)網(wǎng)站優(yōu)化外包
  • 北京網(wǎng)站建設(shè)模板網(wǎng)絡(luò)優(yōu)化app哪個好
  • 鄭州網(wǎng)站建設(shè)專家百度老年搜索
  • 德惠市建設(shè)局網(wǎng)站seo網(wǎng)絡(luò)營銷推廣排名
  • seo關(guān)鍵詞優(yōu)化培訓(xùn)泉州seo報價
  • 德陽網(wǎng)站建設(shè)平臺百度收錄軟件
  • 專門做稀有產(chǎn)品的網(wǎng)站seo代碼優(yōu)化包括哪些
  • 工作室有專門的網(wǎng)站如何找友情鏈接
  • 有什么國外的黃網(wǎng)站百度服務(wù)中心投訴
  • 課程設(shè)計代做網(wǎng)站推薦優(yōu)化疫情二十條措施
  • 寶雞做網(wǎng)站公司谷歌瀏覽器下載官網(wǎng)
  • 公司網(wǎng)站建設(shè)怎么選擇主機大小百度推廣個人怎么開戶
  • 太原網(wǎng)站建設(shè)制作報價南寧seo營銷推廣
  • 網(wǎng)站建設(shè)維護及使用管理辦法自動友鏈網(wǎng)