例题1程序A生成1个文件,其大小为1000字节,其内容为小写字母abcd...z的循环。
试编写该程序。
文件名t1.c#include <stdio.h>#include <stdlib.h>int main(){char x;int i;int fd=open("aa",O_CREAT|O_TRUNC|O_WRONLY,0666);if(fd<0){printf("open file error!\r\n");exit(0);};for(i=0;i<1000;i++){x='a'+(i%26);write(fd,&x,1);}close(fd);}例题2读出一个文件a.txt的倒数第2个字节和倒数第1个字节,显示在屏幕上。
并且显示出当前时间。
文件名t2.c#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){char x[2];int fd=open("a.txt",O_RDONLY);if(fd<0){printf("open file error!\r\n");exitlseek(fd,-3,SEEK_END);read(fd,x,2);printf("倒数第二和第一字节为%c%c\r\n",x[0],x[1]);close(fd);time_t t;time(&t);printf("当前时间:%s",asctime(localtime(&t)));}例题3产生一个进程树父进程有3个子进程,这三个子进程分别有2个子进程。
每个进程退出前打印自己的进程id号文件名t3.c#include <stdio.h>#include <stdlib.h>int main(){int ret,i;for(i=0;i<3;i++){ret=fork();if(ret==0)break;}if(ret==0)for(i=0;i<2;i++){ret=fork();if(ret==0)break;}sleep(10);printf("thread %d is exiting now \r\n",getpid());}测试方法:在另一窗口#pstree-a例题4编写两程序实现消息队列通信程序名t4snd.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/errno.h>struct msgbuf{long mtype;char ctext[100];};int main(){struct msgbuf buf;int msid;msid=msgget(0x1000,0666|IPC_CREAT); if(msid<0){printf("open failed\r\n");exit (0);};while(1){buf.mtype=getpid();scanf("%s",buf.ctext);while((msgsnd(msid,&buf,strlen(buf.ctext),0))<0){if(errno==EINTR)continue;return ;}if(strcmp(buf.ctext,"exit")==0)break;}return 0;}文件名t4rev.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/errno.h>struct msgbuf{long mtype;char ctext[100];};int main(){struct msgbuf buf;int msid,ret;msid=msgget(0x1000,0666|IPC_CREAT);if(msid<0){printf("open failed\r\n");exit(0);};while(1){memset(&buf,0,sizeof(buf));while((ret=msgrcv(msid,&buf,sizeof(buf.ctext),0,0))<0){if(errno==EINTR)continu e;return ;}printf("%d%s\r\n",buf.mtype,buf.ctext);if(strcmp(buf.ctext,"exit")==0)break;}msgctl(msid,IPC_RMID,NULL); return 0;}测试方法首先运行t4snd,输入三行字符串,最后一行必须是小写字母的exit #./t4sndHelloWorldexit则t4snd自动退出然后运行t4rev#./t4rev例题5网络TCP的服务端文件名server.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 82#define BUFSIZE 512char buf[BUFSIZE+1];int main(){//第1步创建套接字int sockfd=socket(AF_INET,SOCK_STREAM,0);int opt=SO_REUSEADDR;setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));//端口重用//第2步设置地址结构体struct sockaddr_in saddr,caddr;saddr.sin_port=htons(PORT);0.0.0.0",&saddr.sin_addr);//第3步绑定bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));//第4步监听listen(sockfd,128);while(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:(0);}buf[n]=0;printf(" %s from %s:例题6SDL的简单动画在编写程序前,要确定2件事情:1 SDL环境安装了2 b.bmp文件和源文件和编译后的可执行文件位于同一路径下文件名mv.c#include <SDL/SDL.h>#include <stdlib.h>#include <stdio.h>#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)<0){printf("无法初始化SDL\r\n");exit(-1);};s=SDL_SetVideoMode(X,Y,16,SDL_SWSURFACE);if(s==NULL){printf("无法设置%d*%d的视频模式\r\n",X,Y);exit(-1);}; image=SDL_LoadBMP("b.bmp");if(image==NULL){printf("无法加载图像%s\r\n");exit(0);};dest.x=0;dest.y=0;dest.w=image->w;dest.h=image->h;while(1){SDL_FillRect(s,&dest,0);dest.x=dest.x+2;//变化的x坐标dest.y=dest.y+3;//变化的y坐标if(dest.x>X||dest.y>Y)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;}注意编译命令#gccmv.c-omv-lSDL#./mv如果这时候报错并且不是代码问题和b.bmp的问题,则可能是xwindow的问题,应该按照下面的方式解决问题:#exit$xhost +$./mv注意你这时候不是以root身份运行程序了!测试方法:然后编译,利用给你的client.c生产客户端。
在第一窗口处启动server,在另外两个窗口处分别启动client.在两个client窗口处输入不同的字符串,检查sever是否收到了这些字符串。