已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Linux系统下编程作业班 级: 姓 名: 学 号: 1. 编写一个文件读写的例子,将自己的名字,学号写入一个文本文件中,并读出来。程序代码:#includemain()FILE *fp;char ch;if(fp=fopen(1.txt,wt+)=NULL)printf(Cannot open file strike any key exit!);getch();exit(1);printf(input a string:n);ch=getchar();while (ch!=n)/这里用回车表示文件写入结束,你可以设置其他的字符,但是必须有结束的标示符。fputc(ch,fp);ch=getchar();rewind(fp);ch=fgetc(fp);while(ch!=EOF)putchar(ch);ch=fgetc(fp);printf(n);fclose(fp);程序运行结果2. 编写一个三线程的例子运行时间分别为100ms200ms300ms程序:#include#include#includePthread_mutex_t mutex=PTHREAD_INITIALIZER;Pthread_cond_t cond=PEHREAD_COND_INITIALIZER;Void*thread1(void*);Void*thread2(void*);Void*thread3(void*);Int i=1;Int main()Pthread_t t1;Pthread_t t2;Pthread_t t3;Pthread_creath(&t1,NULL,.thread1,(void*)NULL);Pthread_creath(&t2,NULL,.thread2,(void*)NULL);Pthread_creath(&t3,NULL,.thread3,(void*)NULL);Pthread_join(t1,NULL);Pthread_join(t2,NULL);Pthread_join(t3,NULL);Exit(0);Printf(“线程1被创建”n);Printf(“线程2被创建”n);Printf(“线程3被创建”n);For(i=1;i=4;i+);If(i=1)Printf(“thread1:Im thread 1”n)Sleep(0.1);Printf(“我已经延迟100毫秒了!”);If(i=2)(Printf(thread2:Im thread 2”n)Sleep(0.2)Printf(”我已经延迟200毫秒了!”);If(i=3)Printf(”Im thread 3”);Sleep(0.3);Printf(”线程1已经结束n”);Printf(”线程2已经结束n”);Printf(”线程3已经结束n”);运行结果3. 编写一个进程之间使用消息队列进行通信的例子程序:sen.c#include#include#include#include#include#include#include#include#define BUF_SIZE 256#define PROJ_ID 32#define PATH_NAME /tmp#define SEND_MSG 1#define RECEIVE_MSG 2int main(void)struct mymsgbuflong msgtype;char messageBUF_SIZE;msgbuf;int queueid;int msglen;if(queueid = msgget(1234,IPC_CREAT|0660) = -1)perror(msgget error!n);exit(1);while(1)printf(send:);fgets(msgbuf.message,BUF_SIZE,stdin);/从标准输入读取字符存至messageif(strncmp(end,msgbuf.message,3) = 0)msgctl(queueid,IPC_RMID,NULL);break;msgbuf.messagestrlen(msgbuf.message)-1 = 0;msgbuf.msgtype = SEND_MSG;if(msgsnd(queueid,&msgbuf,strlen(msgbuf.message)+1,0) = -1)perror(Send msgsnd error!n);exit(1);if(msgrcv(queueid,&msgbuf,BUF_SIZE,RECEIVE_MSG,0) = -1)/缓冲区的大小,请求读取消息的类型perror(Send msgrcv errorn);exit(1);printf(receive: %sn,msgbuf.message);exit(0);receive.c#include#include#include#include#include#include#include#include#define BUF_SIZE 256#define PROJ_ID 32#define PATH_NAME /tmp#define SEND_MSG 1#define RECEIVE_MSG 2int main(void)struct mymsgbuflong msgtype;char messageBUF_SIZE;msgbuf;int queueid;int msglen;if(queueid = msgget(1234,IPC_CREAT|0660) = -1)perror(msgget eror!n);exit(1);while(1)if(msgrcv(queueid,&msgbuf,BUF_SIZE,SEND_MSG,0) = -1)perror(Send msgrcv error!n);exit(1);printf(this is what you sent:%sn,msgbuf.message);printf(Message received from 3196);fgets(msgbuf.message,BUF_SIZE,stdin);if(strncmp(end,msgbuf.message,3) = 0)msgctl(queueid,IPC_RMID,NULL);break;msgbuf.messagestrlen(msgbuf.message)-1=0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 别墅楼顶租房合同范本
- 劳动用工合同解除协议
- 农林机械出租合同范本
- 农村垃圾运输合同范本
- 合同文本拒收现结协议
- 卤菜店劳务合同协议书
- 司法鉴定委托书协议书
- 公司常用劳动合同范本
- 合同违约未处罚写协议
- 保险经纪合同补充协议
- 早期生产遏制GP-12工作要求
- GB/T 16463-1996广播节目声音质量主观评价方法和技术指标要求
- GB/T 15972.20-2021光纤试验方法规范第20部分:尺寸参数的测量方法和试验程序光纤几何参数
- GA/T 1068-2015刑事案件命名规则
- 刘德武《如何画正方形》课件
- 政务礼仪-位次礼仪课件
- 药监系统官方培训 王知坚 益生菌相关药物的质量分析与控制 浙江省食品药品检验研究院
- 绝缘电阻和接地电阻的测量实验
- 《食品经营许可证》申请报告书空白模板
- 生产过程质量改善计划
- 绿萝养殖幻灯片
评论
0/150
提交评论