Linux程序设计考试例题_第1页
Linux程序设计考试例题_第2页
Linux程序设计考试例题_第3页
Linux程序设计考试例题_第4页
Linux程序设计考试例题_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、例题1 程序A生成1个文件,其大小为1000字节,其内容为小写字母abcd.z的循环。试编写该程序。文件名 t1.c#include #include #include int main() char x; int i; int fd=open(aa,O_CREAT|O_TRUNC|O_WRONLY,0666); if(fd0)printf(open file error!rn);exit(0); for(i=0;i1000;i+) x=a+(i%26); write(fd,&x,1); close(fd);例题2 读出一个文件a.txt的倒数第2个字节和倒数第1个字节,显示在屏幕上。并且显示

2、出当前时间。文件名 t2.c#include #include #include #include int main() char x2; int fd=open(a.txt,O_RDONLY); if(fd0)printf(open file error!rn);exit(0); lseek(fd,-3,SEEK_END); read(fd,x,2); printf(倒数第二和第一字节为%c %crn,x0,x1); close(fd); time_t t; time(&t); printf(当前时间:%s,asctime(localtime(&t);例题3 产生一个进程树 父进程有3个子进

3、程,这三个子进程分别有2个子进程。每个进程退出前打印自己的进程id号文件名t3.c#include #include #include int main() int ret,i; for(i=0;i3;i+) ret=fork(); if(ret=0)break; if(ret=0) for(i=0;i2;i+) ret=fork(); if(ret=0)break; sleep(10); printf(thread %d is exiting now rn,getpid(); 测试方法:在另一窗口#su#pstree -a例题4 编写两程序 实现消息队列通信程序名t4snd.c#includ

4、e #include #include #include #include #include #include #include struct msgbuf long mtype; char ctext100;int main() struct msgbuf buf; int msid; msid=msgget(0x1000,0666|IPC_CREAT); if(msid0)printf(open failedrn);exit(0); while(1) buf.mtype=getpid(); scanf(%s,buf.ctext); while(msgsnd(msid,&buf,strlen

5、(buf.ctext),0)0) if(errno=EINTR)continue; return ; if(strcmp(buf.ctext,exit)=0)break; return 0; 文件名t4rev.c#include #include #include #include #include #include #include #include struct msgbuf long mtype; char ctext100;int main() struct msgbuf buf; int msid,ret; msid=msgget(0x1000,0666|IPC_CREAT); if

6、(msid0)printf(open failedrn);exit(0); while(1) memset(&buf,0,sizeof(buf); while(ret=msgrcv(msid,&buf,sizeof(buf.ctext),0,0)0) if(errno=EINTR)continue; return ; printf(%d %srn,buf.mtype,buf.ctext); if(strcmp(buf.ctext,exit)=0)break; msgctl(msid,IPC_RMID,NULL); return 0; 测试方法首先运行t4snd, 输入三行字符串,最后一行必须是

7、小写字母的exit#./t4sndHelloWorldexit则t4snd自动退出然后运行t4rev#./t4rev例题5 网络TCP的服务端文件名 server.c#include #include #include #include #include #include #include #define PORT 82#define BUFSIZE 512char bufBUFSIZE+1;int main() /第 1 步 创建套接字 int sockfd=socket(AF_INET,SOCK_STREAM,0); int opt=SO_REUSEADDR; setsockopt(soc

8、kfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt);/端口重用 /第 2 步 设置地址结构体 struct sockaddr_in saddr,caddr; saddr.sin_family=AF_INET;/使用 internet 协议 saddr.sin_port=htons(PORT); inet_aton(0.0.0.0,&saddr.sin_addr); /第 3 步 绑定 bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr); /第 4 步 监听 listen(sockfd,128); while

9、(1) int len=sizeof(caddr); int new_fd=accept(sockfd,(struct sockaddr*)&caddr,&len); /第 5 步 接收 int ret=fork(); if(ret!=0)continue; while(1) int n=read(new_fd,buf,sizeof(buf); if(n=0) printf(%s:%d closern,inet_ntoa(caddr.sin_addr),htons(caddr.sin_port); exit(0); bufn=0; printf( %s from %s:%drn,buf,ine

10、t_ntoa(caddr.sin_addr),htons(caddr.sin_port); 例题6 SDL的简单动画在编写程序前,要确定2件事情:1 SDL环境安装了2 b.bmp文件和源文件和编译后的可执行文件位于同一路径下文件名 mv.c#include #include #include #define X 800#define Y 600int main() SDL_Surface *s; SDL_Surface *image; SDL_Rect dest,dest1; int x,y; if(SDL_Init(SDL_INIT_VIDEO)w; dest.h=image-h; whi

11、le(1) SDL_FillRect(s,&dest,0); dest.x=dest.x+2;/变化的x坐标 dest.y=dest.y+3;/变化的y坐标 if(dest.xX|dest.yY)dest.x=dest.y=0; SDL_BlitSurface(image,NULL,s,&dest);/*对象目标快速转换*/ SDL_UpdateRect(s,0,0,0,0); SDL_Delay(10); SDL_Event e; if(SDL_PollEvent(&e) switch(e.type) case SDL_QUIT: exit(0); break; return 0; 注意 编译命令#gcc mv.c -o mv -lSDL#./mv如果这时候报错 并且不是代码问题和b.bmp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论