

下載本文檔
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(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)容為小寫(xiě)字母 abcd.z 的循環(huán)。試 編寫(xiě)該程序。文件名 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 讀出一個(gè)文件 a.txt 的倒數(shù)第 2 個(gè)字節(jié)和倒數(shù)第 1 個(gè)字節(jié),顯
2、示在屏幕上。并且顯示 出當(dāng)前時(shí)間。文件名 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( 當(dāng)前時(shí)間 :%s,asctime(localtime(&t);例題 3 產(chǎn)生一個(gè)進(jìn)程樹(shù) 父進(jìn)
3、程有 3 個(gè)子進(jìn)程,這三個(gè)子進(jìn)程分別有 2 個(gè)子進(jìn)程。 退出前打印自己的進(jìn)程 id 號(hào)文件名 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();測(cè)試方法:在另一窗口#su#pstree -a例題 4 編寫(xiě)兩程序 實(shí)現(xiàn)消息隊(duì)列通信程序名 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);每個(gè)進(jìn)程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;測(cè)試方法首先運(yùn)行 t4snd, 輸入三行字符串,最后一行必須是小寫(xiě)字母的 exit #
7、./t4sndHelloWorldexit則 t4snd 自動(dòng)退出 然后運(yùn)行 t4rev #./t4rev例題 5 網(wǎng)絡(luò) TCP 的服務(wù)端 文件名 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 步 設(shè)置地址結(jié)構(gòu)體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)聽(tīng) 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 的簡(jiǎn)單動(dòng)畫(huà) 在編寫(xiě)程序前,要確定 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 坐標(biāo)dest.y=dest.y+3;/ 變化的 y 坐標(biāo) if(dest.xX|dest.yY)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;注意 編譯命令#gcc mv.c -o mv -lSDL#./mv如果這時(shí)候報(bào)錯(cuò) 并且不是代碼問(wèn)題和 b.bmp 的問(wèn)題 , 則可能是 xwindow
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 排水涵管施工方案
- 珠江啤酒公司財(cái)務(wù)管理模式的改進(jìn)方案5400字
- 粉刷警示柱施工方案
- 照明專(zhuān)項(xiàng)施工方案
- 廣東鍋爐管道防腐施工方案
- 削竹式隧道明洞施工方案
- 灰土基層施工方案
- 鋁合金欄桿施工方案
- 拆除道牙和生態(tài)磚施工方案
- 室外壁掛式充電樁施工方案
- 四年級(jí)數(shù)學(xué)(小數(shù)加減運(yùn)算)計(jì)算題專(zhuān)項(xiàng)練習(xí)與答案
- 天津市建筑安全員-C證考試題庫(kù)
- 2025年皖北衛(wèi)生職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)參考答案
- 小學(xué)生春耕教學(xué)課件
- 2024年南信語(yǔ)文數(shù)學(xué)試卷(含答案)
- JGJ46-2024 建筑與市政工程施工現(xiàn)場(chǎng)臨時(shí)用電安全技術(shù)標(biāo)準(zhǔn)
- 2016-2023年江蘇電子信息職業(yè)學(xué)院高職單招(英語(yǔ)/數(shù)學(xué)/語(yǔ)文)筆試歷年考點(diǎn)試題甄選合集含答案解析
- 8.6《林黛玉進(jìn)賈府》課本劇劇本
- 計(jì)算機(jī)信息檢索第三章
- ISO22716:2007標(biāo)準(zhǔn)(中英文對(duì)照SN T2359-2009)47
- 融媒體檔案信息化管理探究
評(píng)論
0/150
提交評(píng)論