

下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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#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 網絡 TCP 的服務端 文件名 server.c#include #include #include #include #include #include #include #define PORT 82 #defineBUFSIZE 512 char bufBUFSIZE+1;int main()/ 第 1 步 創建套接字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協議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(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 環境安裝了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;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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 七夕節活動方案(合集18篇)
- 年產300萬只汽車前大燈智項目實施方案
- 年產20萬噸氟化系列產品生產項目建議書(僅供參考)
- 廣告牌匾規范化整治規劃設計方案(范文)
- 公共廁所改造工程規劃設計方案(范文)
- 社會服務方案設計
- 伊春職業學院《申論》2023-2024學年第二學期期末試卷
- 畢節醫學高等專科學校《視唱III》2023-2024學年第二學期期末試卷
- 廈門興才職業技術學院《教師語言技能B1》2023-2024學年第二學期期末試卷
- 郴州思科職業學院《大數據運維實驗》2023-2024學年第二學期期末試卷
- 人教版(2024)七年級下冊英語期中質量檢測試卷(含答案)
- 2025年中國研發服務業行業市場調研分析及投資戰略咨詢報告
- 2025屆福建省龍巖市高三下學期3月一模物理試題 含解析
- 《TCPSS1002-2023直流散熱風扇環境適應性測試技術規范》
- 高滲高血糖綜合癥護理
- 2025年大學轉專業面試題及答案
- ktv入股協議合同范例
- 吸氧并發癥預防及處理
- GB 20943-2025交流-直流和交流-交流電源能效限定值及能效等級
- 2025年甘肅省公路發展集團限公司招聘70人歷年高頻重點模擬試卷提升(共500題附帶答案詳解)
- 一模總結沖刺中考主題班會
評論
0/150
提交評論