Linux程序設(shè)計(jì)考試?yán)}_第1頁(yè)
Linux程序設(shè)計(jì)考試?yán)}_第2頁(yè)
Linux程序設(shè)計(jì)考試?yán)}_第3頁(yè)
Linux程序設(shè)計(jì)考試?yán)}_第4頁(yè)
Linux程序設(shè)計(jì)考試?yán)}_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、例題1程序a生成1個(gè)文件,其大小為1000字節(jié),其內(nèi)容為小寫字母abcd.z的循環(huán)。試編寫該程序。文件名 t1.c#include <stdio.h>#include <stdlib.h>#include <fcntl.h> int main() char x;int i;int fd=open("aa",o_creat|o_trunc|o_wronly,0666);if(fd<0)printf("open !rn");exit(0);for(i=0;i<1000;i+) x='a'+(i%

2、26); write(fd,&x,1); close(fd);例題 2 讀出一個(gè)文件a.txt 的倒數(shù)第 2 個(gè)字節(jié)和倒數(shù)第1 個(gè)字節(jié),顯示在屏幕上。并且顯示出當(dāng)前時(shí)間。文件名 t2.c#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <time.h> int main() char x2;int fd=open("a.txt",o_rdonly);if(fd<0)printf("open !rn");exit(0)

3、;lseek(fd,-3,seek_end);read(fd,x,2);printf(" 倒數(shù)第二和第一字節(jié)為 %c %crn",x0,x1);close(fd);time_t t;time(&t);printf(" 當(dāng)前時(shí)間 :%s",asctime(localtime(&t);每個(gè)進(jìn)程例題 3 產(chǎn)生一個(gè)進(jìn)程樹(shù)父進(jìn)程有 3 個(gè)子進(jìn)程,這三個(gè)子進(jìn)程分別有2 個(gè)子進(jìn)程。退出前打印自己的進(jìn)程id 號(hào)文件名 t3.c#include <stdio.h>#include <stdlib.h>#include <fcn

4、tl.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 rn",getpid();測(cè)試方法:在另一窗口#su#pstree -a例題 4 編寫兩程序 實(shí)現(xiàn)消息隊(duì)列通信程序名 t4snd.c#include <stdio.h>#include <stdlib.h>#includ

5、e <fcntl.h>#include <string.h>#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/errno.h>struct msgbuflong mtype;char ctext100;int main()struct msgbuf buf;int msid;msid=msgget(0x1000,0666|ipc_creat);if(msid<0)printf("open failedrn&

6、quot;);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 <fcntl.h>#i

7、nclude <string.h>#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/errno.h>struct msgbuflong mtype;char ctext100;int main()struct msgbuf buf;int msid,ret;msid=msgget(0x1000,0666|ipc_creat);if(msid<0)printf("open failedrn");exit(0);

8、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;測(cè)試方法首先運(yùn)行 t4snd, 輸入三行字符串,最后一行必須是小寫字母的 ex

9、it#./t4sndhelloworldexit則 t4snd 自動(dòng)退出然后運(yùn)行 t4rev#./t4rev例題5網(wǎng)絡(luò)tcp的服務(wù)端文件名 server.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define port 82#define bufsize 512char

10、 bufbufsize+1;int main()/ 第 1 步 創(chuàng)建套接字int sockfd=socket(af_inet,sock_stream,0);int opt=so_reuseaddr;setsockopt(sockfd,sol_socket,so_reuseaddr,&opt,sizeof(opt)端口重用/ 第 2 步 設(shè)置地址結(jié)構(gòu)體struct sockaddr_in saddr,caddr;saddr.sin_family=af_inet;使用 internet 協(xié)議saddr.sin_port=htons(port);inet_aton("0.0.0.0

11、",&saddr.sin_addr);/ 第 3 步 綁定bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr);/ 第 4 步 監(jiān)聽(tīng)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(

12、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_addr),htons(caddr.sin_port); 例題6 sdl的簡(jiǎn)單動(dòng)畫(huà)在編寫程序前,要確定2 件事情:1 sdl環(huán)境安裝了2 b.bmp 文件和源文件和編譯后的可執(zhí)行文件位于同一路徑下文件名 mv.c#include <sdl/sdl.h>

13、#include <stdlib.h>#include <stdio.h>#define x 800#define y 600 int main() sdl_surface *s;sdl_surface *image;sdl_rect dest,dest1;int x,y;if(sdl_init(sdl_init_video)<0)printf(" 無(wú)法初始化 sdlrn");exit( -1);s=sdl_setvideomode(x,y,16,sdl_swsurface);if(s=null)printf(" 無(wú)法設(shè)置 %d*%d

14、 的視頻模式rn",x,y);exit( -1);image=sdl_loadbmp("b.bmp");if(image=null)printf(" 無(wú)法加載圖像%srn");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 坐標(biāo)dest.y=dest.y+3;/ 變化的y 坐標(biāo)if(dest.x>x|dest.y>y)dest.x=dest.y=0;sdl_blitsurface(image,null,s,&dest);/* 對(duì)象目標(biāo)快速轉(zhuǎn)換*/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;注意 編譯命令#g

溫馨提示

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

評(píng)論

0/150

提交評(píng)論