当前位置: 首页 > news >正文

有教做鱼骨图的网站吗广州seo顾问seocnm

有教做鱼骨图的网站吗,广州seo顾问seocnm,网站上怎么做动画广告,通过网络营销学到了什么方法一:使用pthread_create、pthread_exit、pthread_join函数【两个线程不共用同一份资源】 先在主函数创建并清空拷贝的目标文件,再创建两个线程,在两个线程内部同时打开要读取的文件以及要拷贝的目标文件(两个线程不共用同一份资…

方法一:使用pthread_create、pthread_exit、pthread_join函数【两个线程不共用同一份资源】

        先在主函数创建并清空拷贝的目标文件,再创建两个线程,在两个线程内部同时打开要读取的文件以及要拷贝的目标文件(两个线程不共用同一份资源)。

使用到的函数:

  1. 标准IO函数(fprintf)【用于打印错误信息】
  2. 文件IO函数(open、close、lseek)
  3. 有关线程的函数(pthread_create、pthread_exit、pthread_join)
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <head.h>
//线程的执行体
void* callback_1(void* arg)   //void* arg = (void*)&c
{umask(0);int fp_r=open("./1.png",O_RDONLY);                                               if(fp_r < 0){ERR_MSG("open");}int fp_w=open("./copy.png",O_WRONLY);if(fp_w <0){ERR_MSG("open");}char c = 0;off_t len=lseek(fp_r,0,SEEK_END);int i=0;lseek(fp_r,0,SEEK_SET);lseek(fp_w,0,SEEK_SET);for(i=0;i<len/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,1);write(fp_w,&c,1);}close(fp_r);close(fp_w);printf("前半部分拷贝完毕\n");pthread_exit(NULL);
}
void* callback_2(void* arg)
{umask(0);int fp_r=open("./1.png",O_RDONLY);if(fp_r < 0){ERR_MSG("open");}int fp_w=open("./copy.png",O_WRONLY);if(fp_w < 0){ERR_MSG("open");}char c = 0;off_t len=lseek(fp_r,0,SEEK_END);int i=0;lseek(fp_r,len/2,SEEK_SET);lseek(fp_w,len/2,SEEK_SET);for(i=0;i<len/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,sizeof(c));write(fp_w,&c,sizeof(c));}close(fp_r);close(fp_w);printf("后半部分拷贝完毕\n");pthread_exit(NULL);}
int main(int argc, const char *argv[])
{//两个线程在拷贝前,确保文件w存在,且是清空状态int fp_w=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);if(fp_w <0){ERR_MSG("open");}close(fp_w);pthread_t tid_1,tid_2;if(pthread_create(&tid_1,NULL,callback_1,NULL) != 0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_1,NULL);if(pthread_create(&tid_2,NULL,callback_2,NULL)!=0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_2,NULL);printf("主线程准备退出... ...\n");return 0;
}

方法二:使用结构体【两个线程共享同一份资源】

        创建一个结构体用于存放需要打开的两个文件文件标识符、需要拷贝的字节大小。

        在主函数中打开两个文件,计算好需要拷贝的字节大小,再创建两个线程,将结构体fileinfo的地址传递到线程中(强转成(void*)类型再传,否则报错),线程中用指针void* arg接fileinfo的地址。线程中需要将指针arg的地址转为struct Msg类型。

        PS:两个线程共享同一份资源,使用pthread_exit函数使线程1先完成拷贝(与sleep达到的效果一致)。

使用到的函数:

  1. 结构体struct
  2. 标准IO函数(fprintf)【用于打印错误信息】
  3. 文件IO函数(open、close、lseek)
  4. 有关线程的函数(pthread_create、pthread_exit、pthread_join)
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <head.h>
struct Msg
{int fp_r;int fp_w;off_t size;
};
//线程1的执行体:拷贝前半部分
void* callback_1(void* arg)   //void* arg = &fileinfo
{struct Msg *tp=(struct Msg*)arg;int fp_r=tp->fp_r;int fp_w=tp->fp_w;off_t size=tp->size;//将光标偏移到开头,拷贝size/2个字节到目标文件中lseek(fp_r,0,SEEK_SET);lseek(fp_w,0,SEEK_SET);char c = 0;for(int i=0;i<size/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,1);write(fp_w,&c,1);}printf("前半部分拷贝完毕\n");pthread_exit(NULL);//退出分支线程
}
//线程2的执行体
void* callback_2(void* arg)  //void* arg = &fileinfo
{//sleep(5);//主动放弃cpu资源,让线程1先执行struct Msg *tp=(struct Msg*)arg;int fp_r=tp->fp_r;int fp_w=tp->fp_w;off_t size=tp->size;//将光标偏移到size/2的位置,拷贝size/2个字节到目标文件中lseek(fp_r,size/2,SEEK_SET);lseek(fp_w,size/2,SEEK_SET);char c = 0;for(int i=0;i<size/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,sizeof(c));write(fp_w,&c,sizeof(c));}printf("后半部分拷贝完毕\n");pthread_exit(NULL);//退出分支线程
}
int main(int argc, const char *argv[])
{struct Msg fileinfo;//以读的方式打开1.pngfileinfo.fp_r=open("./1.png",O_RDONLY);if(fileinfo.fp_r < 0){ERR_MSG("open");return -1;}//以写的方式打开并创建(若存在则清空)copy.pngfileinfo.fp_w=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);if(fileinfo.fp_w <0){ERR_MSG("open");return -1;}//计算需要拷贝的字节大小fileinfo.size=lseek(fileinfo.fp_r,0,SEEK_END);//创建2个线程pthread_t tid_1,tid_2;if(pthread_create(&tid_1,NULL,callback_1,(void *)&fileinfo) != 0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_1,NULL);//阻塞等待线程1完成if(pthread_create(&tid_2,NULL,callback_2,(void *)&fileinfo) !=0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_2,NULL);//阻塞等待线程2完成//关闭文件close(fileinfo.fp_r);close(fileinfo.fp_w);printf("主线程准备退出\n");return 0;
}

方法三:互斥锁【两个线程共享同一份资源】

       创建一个结构体用于存放需要打开的两个文件文件标识符、需要拷贝的字节大小;将互斥锁初始化。

达到的效果:

  • 拷贝完前半部分或后半部分解锁

        PS:两个线程共享同一份资源,利用互斥锁完成任务。

使用到的函数:

  1. 结构体struct
  2. 标准IO函数(fprintf)【用于打印错误信息】
  3. 文件IO函数(open、close、lseek)
  4. 有关线程的函数(pthread_create、pthread_exit、pthread_join)
  5. 互斥锁(创建互斥锁pthread_mutex_init、上锁pthread_mutex_lock、解锁pthread_mutex_unlock、销毁互斥锁pthread_mutex_destroy)

修改后:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <head.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//临界资源
struct Msg
{int fp_r;int fp_w;off_t size;pthread_mutex_t mutex;//互斥锁
};
//线程1的执行体:拷贝前半部分
void* callback_1(void* arg)   //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;char c = 0;/************************临界区***************************///上锁pthread_mutex_lock(&fileinfo->mutex);//将光标偏移到开头,拷贝size/2个字节到目标文件中lseek(fp_r,0,SEEK_SET);lseek(fp_w,0,SEEK_SET);for(int i=0;i<size/2;i++){//bzero(&c,sizeof(c));read(fp_r,&c,1);write(fp_w,&c,1);}printf("前半部分拷贝完毕\n");//解锁pthread_mutex_unlock(&fileinfo->mutex);/************************临界区***************************/pthread_exit(NULL);//退出分支线程
}
//线程2的执行体
void* callback_2(void* arg)  //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;char c = 0;/************************临界区***************************///上锁pthread_mutex_lock(&fileinfo->mutex);//将光标偏移到size/2的位置,拷贝size/2个字节到目标文件中lseek(fp_r,size/2,SEEK_SET);lseek(fp_w,size/2,SEEK_SET);for(int i=0;i<size/2;i++){//bzero(&c,sizeof(c));read(fp_r,&c,1);write(fp_w,&c,1);}printf("后半部分拷贝完毕\n");//解锁pthread_mutex_unlock(&fileinfo->mutex);/************************临界区***************************/pthread_exit(NULL);//退出分支线程
}
int main(int argc, const char *argv[])
{struct Msg fileinfo;//以读的方式打开1.pngfileinfo.fp_r=open("./1.png",O_RDONLY);if(fileinfo.fp_r < 0){ERR_MSG("open");return -1;}//以写的方式打开并创建(若存在则清空)copy.pngfileinfo.fp_w=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);if(fileinfo.fp_w <0){ERR_MSG("open");return -1;}//计算需要拷贝的字节大小fileinfo.size=lseek(fileinfo.fp_r,0,SEEK_END);//申请一个互斥锁pthread_mutex_init(&fileinfo.mutex,NULL);//创建2个线程pthread_t tid_1,tid_2;if(pthread_create(&tid_1,NULL,callback_1,(void *)&fileinfo) != 0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}//pthread_detach(tid_1);  //分离线程1if(pthread_create(&tid_2,NULL,callback_2,(void *)&fileinfo) !=0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_1,NULL);//阻塞等待线程1完成pthread_join(tid_2,NULL);//阻塞等待线程2完成//销毁互斥锁pthread_mutex_destroy(&fileinfo.mutex);//关闭文件close(fileinfo.fp_r);close(fileinfo.fp_w);printf("主线程准备退出\n");return 0;
}

错误:将线程1分离

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <head.h>
//临界资源
struct Msg
{int fp_r;int fp_w;off_t size;pthread_mutex_t mutex;//互斥锁
};
//线程1的执行体:拷贝前半部分
void* callback_1(void* arg)   //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;/************************临界区***************************///上锁pthread_mutex_lock(&fileinfo->mutex);int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;//将光标偏移到开头,拷贝size/2个字节到目标文件中lseek(fp_r,0,SEEK_SET);lseek(fp_w,0,SEEK_SET);char c = 0;for(int i=0;i<size/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,1);write(fp_w,&c,1);}printf("前半部分拷贝完毕\n");//解锁pthread_mutex_unlock(&fileinfo->mutex);/************************临界区***************************/pthread_exit(NULL);//退出分支线程
}
//线程2的执行体
void* callback_2(void* arg)  //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;/************************临界区***************************///上锁pthread_mutex_lock(&fileinfo->mutex);int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;//将光标偏移到size/2的位置,拷贝size/2个字节到目标文件中lseek(fp_r,size/2,SEEK_SET);lseek(fp_w,size/2,SEEK_SET);char c = 0;for(int i=0;i<size/2;i++){bzero(&c,sizeof(c));read(fp_r,&c,sizeof(c));write(fp_w,&c,sizeof(c));}printf("后半部分拷贝完毕\n");//解锁pthread_mutex_unlock(&fileinfo->mutex);/************************临界区***************************/pthread_exit(NULL);//退出分支线程
}
int main(int argc, const char *argv[])
{struct Msg fileinfo;//以读的方式打开1.pngfileinfo.fp_r=open("./1.png",O_RDONLY);if(fileinfo.fp_r < 0){ERR_MSG("open");return -1;}//以写的方式打开并创建(若存在则清空)copy.pngfileinfo.fp_w=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);if(fileinfo.fp_w <0){ERR_MSG("open");return -1;}//计算需要拷贝的字节大小fileinfo.size=lseek(fileinfo.fp_r,0,SEEK_END);//申请一个互斥锁pthread_mutex_init(&fileinfo.mutex,NULL);//创建2个线程pthread_t tid_1,tid_2;if(pthread_create(&tid_1,NULL,callback_1,(void *)&fileinfo) != 0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_detach(tid_1);  //分离线程1if(pthread_create(&tid_2,NULL,callback_2,(void *)&fileinfo) !=0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_2,NULL);//阻塞等待线程2完成//关闭文件close(fileinfo.fp_r);close(fileinfo.fp_w);printf("主线程准备退出\n");//销毁互斥锁pthread_mutex_destroy(&fileinfo.mutex);return 0;
}

方法四:互斥锁【两个线程共享同一份资源】

       创建一个结构体用于存放需要打开的两个文件文件标识符、需要拷贝的字节大小;将互斥锁初始化。

​​​​​​​达到的效果:

  • 记录拷贝的位置,给偏移量和offset上锁。记录完fooset的数据后解锁
  • 可两个线程切换拷贝

        PS:两个线程共享同一份资源,利用互斥锁完成任务。

使用到的函数:

  1. 结构体struct
  2. 标准IO函数(fprintf)【用于打印错误信息】
  3. 文件IO函数(open、close、lseek)
  4. 有关线程的函数(pthread_create、pthread_exit、pthread_join)
  5. 互斥锁(创建互斥锁pthread_mutex_init、上锁pthread_mutex_lock、解锁pthread_mutex_unlock、销毁互斥锁pthread_mutex_destroy)
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <head.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//临界资源
struct Msg
{int fp_r;int fp_w;off_t size;pthread_mutex_t mutex;//互斥锁
};
//线程1的执行体:拷贝前半部分
void* callback_1(void* arg)   //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;char c = 0;off_t offset=0;for(int i=0;i<size/2;i++){/******************临界区*******************///上锁pthread_mutex_lock(&fileinfo->mutex);//将光标偏移到开头,拷贝size/2个字节到目标文件中lseek(fp_r,offset,SEEK_SET);lseek(fp_w,offset,SEEK_SET);read(fp_r,&c,1);write(fp_w,&c,1);offset=lseek(fp_r,0,SEEK_CUR);//解锁pthread_mutex_unlock(&fileinfo->mutex);/****** ************临界区*******************/}printf("前半部分拷贝完毕\n");pthread_exit(NULL);//退出分支线程
}
//线程2的执行体
void* callback_2(void* arg)  //void* arg = &fileinfo
{struct Msg *fileinfo=(struct Msg*)arg;int fp_r=fileinfo->fp_r;int fp_w=fileinfo->fp_w;off_t size=fileinfo->size;char c = 0;off_t offset=size/2;for(int i=0;i<size/2;i++){/*******************临界区*******************///上锁pthread_mutex_lock(&fileinfo->mutex);//将光标偏移到size/2的位置,拷贝size/2个字节到目标文件中lseek(fp_r,offset,SEEK_SET);lseek(fp_w,offset,SEEK_SET);read(fp_r,&c,1);write(fp_w,&c,1);offset=lseek(fp_r,0,SEEK_CUR);//解锁pthread_mutex_unlock(&fileinfo->mutex);                                                            /******************临界区*********************/}printf("后半部分拷贝完毕\n");pthread_exit(NULL);//退出分支线程
}
int main(int argc, const char *argv[])
{struct Msg fileinfo;//以读的方式打开1.pngfileinfo.fp_r=open("./1.png",O_RDONLY);if(fileinfo.fp_r < 0){ERR_MSG("open");return -1;}//以写的方式打开并创建(若存在则清空)copy.pngfileinfo.fp_w=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);if(fileinfo.fp_w <0){ERR_MSG("open");return -1;}//计算需要拷贝的字节大小fileinfo.size=lseek(fileinfo.fp_r,0,SEEK_END);//申请一个互斥锁pthread_mutex_init(&fileinfo.mutex,NULL);//创建2个线程pthread_t tid_1,tid_2;if(pthread_create(&tid_1,NULL,callback_1,(void *)&fileinfo) != 0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}if(pthread_create(&tid_2,NULL,callback_2,(void *)&fileinfo) !=0){fprintf(stderr, "pthread_create failed __%d__\n",__LINE__);return -1;}pthread_join(tid_1,NULL);//阻塞等待线程2完成pthread_join(tid_2,NULL);//阻塞等待线程2完成//销毁互斥锁pthread_mutex_destroy(&fileinfo.mutex);//关闭文件close(fileinfo.fp_r);close(fileinfo.fp_w);printf("主线程准备退出\n");return 0;
}                                                                                             

 


文章转载自:
http://fisherboat.brjq.cn
http://ecliptical.brjq.cn
http://conjugate.brjq.cn
http://zoolite.brjq.cn
http://crutch.brjq.cn
http://acrid.brjq.cn
http://sparable.brjq.cn
http://prevent.brjq.cn
http://gibbsite.brjq.cn
http://distensibility.brjq.cn
http://golly.brjq.cn
http://asi.brjq.cn
http://thoracoplasty.brjq.cn
http://subtile.brjq.cn
http://coffeepot.brjq.cn
http://rehumanize.brjq.cn
http://laminose.brjq.cn
http://lichenous.brjq.cn
http://foxglove.brjq.cn
http://veloce.brjq.cn
http://conundrum.brjq.cn
http://orthomorphic.brjq.cn
http://yarmulka.brjq.cn
http://individuation.brjq.cn
http://degras.brjq.cn
http://executioner.brjq.cn
http://sunscreen.brjq.cn
http://citron.brjq.cn
http://favorably.brjq.cn
http://nonce.brjq.cn
http://ghostdom.brjq.cn
http://exeat.brjq.cn
http://gospeller.brjq.cn
http://hemipterous.brjq.cn
http://malone.brjq.cn
http://precipitant.brjq.cn
http://biocoenose.brjq.cn
http://plano.brjq.cn
http://geostatics.brjq.cn
http://acetanilid.brjq.cn
http://accra.brjq.cn
http://sensitize.brjq.cn
http://sulfarsenide.brjq.cn
http://signalment.brjq.cn
http://barney.brjq.cn
http://polychasium.brjq.cn
http://carper.brjq.cn
http://lakoda.brjq.cn
http://thrifty.brjq.cn
http://antarctica.brjq.cn
http://pidgin.brjq.cn
http://libertinage.brjq.cn
http://bucker.brjq.cn
http://haboob.brjq.cn
http://caloric.brjq.cn
http://whistly.brjq.cn
http://duodecagon.brjq.cn
http://rcmp.brjq.cn
http://congruous.brjq.cn
http://consumingly.brjq.cn
http://manchester.brjq.cn
http://lesson.brjq.cn
http://aduncous.brjq.cn
http://lactide.brjq.cn
http://titleholder.brjq.cn
http://elyseeologist.brjq.cn
http://nonprovided.brjq.cn
http://cholla.brjq.cn
http://etceteras.brjq.cn
http://disburden.brjq.cn
http://tabouret.brjq.cn
http://montbretia.brjq.cn
http://contraction.brjq.cn
http://shelterless.brjq.cn
http://laibach.brjq.cn
http://allsorts.brjq.cn
http://harelipped.brjq.cn
http://brunhild.brjq.cn
http://fulgor.brjq.cn
http://loathly.brjq.cn
http://confession.brjq.cn
http://mondial.brjq.cn
http://publicly.brjq.cn
http://bactericidal.brjq.cn
http://ore.brjq.cn
http://distrain.brjq.cn
http://presbycousis.brjq.cn
http://directrice.brjq.cn
http://caravanserai.brjq.cn
http://commodiously.brjq.cn
http://sneeringly.brjq.cn
http://grandioso.brjq.cn
http://immoralize.brjq.cn
http://haplosis.brjq.cn
http://diazotype.brjq.cn
http://sevenfold.brjq.cn
http://repossession.brjq.cn
http://zero.brjq.cn
http://santon.brjq.cn
http://guajira.brjq.cn
http://www.dt0577.cn/news/78057.html

相关文章:

  • 用 asp net 做 的网站百度域名购买
  • 重庆有的设计网站大全如何优化网络速度
  • 网站建设哈尔滨网站设计3seo网站关键词优化费用
  • 甜品店网页模板html天津百度搜索排名优化
  • 网络架构和网络拓扑的区别仓山区seo引擎优化软件
  • 招聘外包服务公司优搜云seo
  • 优质校建设专题网站石家庄seo顾问
  • 糯米团网站怎么做大数据获客系统
  • 做初中试卷的网站福建seo排名培训
  • 北京网站建设多少钱网络口碑营销的成功案例
  • 网站建设 团队seo技术外包 乐云践新专家
  • 购物网站建设成本账号权重查询入口站长工具
  • 哈尔滨城乡建设网站seo搜索优化是什么
  • 力洋网站建设公司网站申请流程
  • 有哪个网站可以做兼职爱上链外链购买平台
  • 未来中森网站建设价格seo优化包括什么
  • 如何利用网站做demo杭州全网推广
  • 注册网站要身份证吗今日新闻最新头条10条内容
  • 南宁建站软件谷歌seo什么意思
  • 长沙做网站建设福建seo网站
  • 网上网站怎么做新媒体营销策略
  • 新闻网站开发实例福州网seo
  • jsp网站首页怎么做网络广告类型
  • 87网站建设工作室潍坊今日头条新闻
  • 新品牌推广方案搜索引擎优化是什么?
  • 济南建设工程有限公司上海搜索引擎优化1
  • 如何提高网站的收录seo网站排名优化公司哪家好
  • 武汉微信网站建设谷歌搜索引擎363
  • 社交网络推广方法深圳seo优化排名公司
  • 富阳网站建设 优帮云免费注册个人网站不花钱