Linux程序設計考試例題_第1頁
Linux程序設計考試例題_第2頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、例題 1 程序 A 生成 1 個文件,其大小為 1000 字節(jié),其內(nèi)容為小寫字母 abcd.z 的循環(huán)。試 編寫該程序。文件名 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 的倒數(shù)第 2 個字節(jié)和倒數(shù)第 1 個字節(jié),顯

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( 倒數(shù)第二和第一字節(jié)為 %c %crn,x0,x1);close(fd);time_t t;time(&t);printf( 當前時間 :%s,asctime(localtime(&t);例題 3 產(chǎn)生一個進程樹 父進

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 編寫兩程序 實現(xiàn)消息隊列通信程序名 t4snd.c#inc

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

5、n(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 msgbuflong mtype;char ctext100;int main()struct msgbuf buf;int msid,ret; msid=msgget(0 x1000,0666|IPC_CREAT); if(msid

6、0)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, 輸入三行字符串,最后一行必須是小寫字母的 exit #

7、./t4sndHelloWorldexit則 t4snd 自動退出 然后運行 t4rev #./t4rev例題 5 網(wǎng)絡 TCP 的服務端 文件名 server.c#include #include #include #include #include #include #include #define PORT 82 #defineBUFSIZE 512 char bufBUFSIZE+1;int main()/ 第 1 步 創(chuàng)建套接字int sockfd=socket(AF_INET,SOCK_STREAM,0);int opt=SO_REUSEADDR;setsockopt(sockfd

8、,SOL_SOCKET,SO_REUSEADDR,& opt,sizeof(opt)端 口重用 /第 2 步 設置地址結構體structsockaddr_insaddr,caddr;saddr.sin_family=AF_INET;/使用internet協(xié)議saddr.sin_port=htons(PORT);inet_aton(0.0.0.0,&saddr.sin_addr);/ 第 3 步 綁定 bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr);/ 第 4 步 監(jiān)聽 listen(sockfd,128);while(1)int len=s

9、izeof(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,inet_ntoa(caddr.sin_add

10、r),htons(caddr.sin_port);例題 6 SDL 的簡單動畫 在編寫程序前,要確定 2 件事情:1 SDL 環(huán)境安裝了2 b.bmp 文件和源文件和編譯后的可執(zhí)行文件位于同一路徑下文件名 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;while(1)SDL_FillRect(s,

11、&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 的問題 , 則可能是 xwindow

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論