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

8.6日作业

逆置打印

#include "iohead.h"
//负责打印
char str[128]="hello";void *callback1(void *arg1)
{while(1){puts(str);                                                           putchar(10);sleep(3);}
}
//负责逆置
void *callback2(void *arg2)
{sleep(1);while(1){int len=strlen(str);for(int i=0,j=len-1;i<len/2;++i,--j){char temp=str[i];str[i]=str[j];str[j]=temp;}sleep(1);}
}int main(int argc, const char *argv[])
{                                         //创建第一个分支pthread_t thread1;if(pthread_create(&thread1,NULL,callback1,NULL)!=0){printf("pthread_create error");return -1;}//第二个分支pthread_t thread2;if(pthread_create(&thread2,NULL,callback2,NULL)!=0){printf("pthread_create error");return -1;}pthread_join(thread1,NULL);pthread_join(thread2,NULL);while(1);return 0;
}

#include "iohead.h"                                                         
void *callback(void* arg)
{//打开文件FILE *fp=fopen("./file","r");FILE *fp_w=fopen("./myfile","w");//光标偏移末尾,计算文件大小fseek(fp,0,SEEK_END);long len=ftell(fp);fseek(fp,0,SEEK_SET);fseek(fp_w,0,SEEK_SET);char buf;for(int i=0;i<len/2;i++){//从file中读取写入bufif(1>fread(&buf,1,1,fp)){break;}//buf里的内容写入fp_wfwrite(&buf,1,1,fp_w);}//关闭文件if(EOF==fclose(fp)){printf("文件关闭失败");}if(EOF==fclose(fp_w)){printf("文件关闭失败");}return NULL;
}
void *callback1(void* arg)
{//打开文件FILE *fp=fopen("./file","r");FILE *fp_w=fopen("./myfile","w");//光标偏移末尾,计算文件大小fseek(fp,0,SEEK_END);long len=ftell(fp);fseek(fp,len/2,SEEK_SET);fseek(fp_w,len/2,SEEK_SET);char buf;for(int i=len/2;i<len;i++){//从file中读取写入bufif(1>fread(&buf,1,1,fp)){break;}//buf里的内容写入fp_wfwrite(&buf,1,1,fp_w);}//关闭文件if(EOF==fclose(fp)){printf("文件关闭失败");}if(EOF==fclose(fp_w)){printf("文件关闭失败");}return NULL;
}
int main(int argc, const char *argv[])
{pthread_t thread,thread1;//创建一个线程使用threadif(0!=pthread_create(&thread,NULL,callback,NULL)){return -1;}//创建第二个线程使用thread1if(0!=pthread_create(&thread1,NULL,callback,NULL)){return -1;}pthread_detach(thread);pthread_detach(thread1);while(1);return 0;

http://www.dtcms.com/a/318499.html

相关文章:

  • 五、Envoy集群管理
  • Redis Redis 常见数据类型
  • TFTP: Linux 系统安装 TFTP,文件系统启动后TFTP使用
  • Java 启动命令的完整解析
  • 【渲染流水线】[应用阶段]-[裁剪]以UnityURP为例
  • GeoTools 结合 OpenLayers 实现缓冲区分析
  • LINQ 要点
  • 92、【OS】【Nuttx】【构建】cmake 支持构建的目标
  • SOD-YOLO:增强基于YOLO的无人机影像小目标检测
  • Product Hunt 每日热榜 | 2025-08-06
  • GoogLeNet训练
  • FastDeploy2.0:Error reading file: SafeTensorError::MetadataIncompleteBuffer
  • chdir系统调用及示例
  • 【C/C++】形参、实参相关内容整理
  • 零基础-动手学深度学习-8.7. 通过时间反向传播
  • Spring_事务
  • 国产3D大型装配设计新突破①:图纸打开设计双加速 | 中望3D 2026
  • C语言的数组与字符串练习题2
  • 如何快速翻译PPT中的文字(或简繁体转换)
  • 【51单片机2个独立按键2个独立数码管静态显示内容自定】2022-10-22
  • Perforce P4 Plan - DevOps实时规划工具
  • 指挥中心自动化的演变
  • 无人机遥控器波特率技术解析
  • 前端开发_怎么禁止用户复制内容
  • 计算机网络:如何判断B或者C类IP地址是否划分了子网
  • 设备 AI 知识库如何提升管理效率?实测分享
  • 【STM32U385RG 测评】基于VSCode的STM32开发环境搭建
  • 认识河豚毒素!剧毒神经毒素详解!
  • 向量数据库基础夯实:相关概念的详细介绍
  • 淘宝/天猫商品详情API详解(tb.item_get)