




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選文庫 計科143 實驗5 Linux文件操作之帶緩存和非緩沖文件的讀寫 專業班級: 實驗日期:2017420實驗成績: 學生姓名:王祥真學號:6103114095 實驗類型:驗證?綜合 設計 創新 通過編寫文件讀寫及上鎖的程序,進一步熟悉Linux中文件I/O相關的應用開 發,并且熟練掌握open()、read()、write()、fcntl()等函數的使用。 二. 實驗內容 1.用write, read, open等系統調用編寫分別實現如下功能的程序(要求進行 必要的出錯檢查): (1)創建一個文件testfile.txt,文件內容從鍵盤輸入; 將testfile.txt的內容顯示在屏幕
2、上,并將testfile.txt 的內容復制到一個 新的文件file2.txt中。 實驗代碼: #i ncludev un istd.h #in clude #i ncludevsys/stat.h #in clude #i ncludevstdio.h #i ncludevstdlib.h #i ncludevstdio.h #in clude int main() int fd, fdsrc,fddes ,n bytes; int n ewret , n1,n2; int flags=O_CREAT | O_TRUNC | O_WRONLY; char src50,des30,buf30;
3、 char str160; char str260; strcat(str1,cat ); printf(請輸入要創建的源文件名字); scan f(%s,src); fd=creat(src,S_IRUSR|S_IWUSR); /創建源文件 n 仁read(STDIN_FILENO,buf,8O); if(n 10) p error(read STDIN_FILENO); exit(1); 以讀寫方式打開 fdsrc=open( src,flags,0644); / if(fdsrc0) exit(1); write(fdsrc,buf, n1);/寫入 printf(輸出剛創建源文件的內容
4、n); strcat(str1,src);/cat src; n ewret=system(str1);/執行命令 close(fdsrc);/ 關閉指針 printf(請輸入目標文件名:); scan f(%s,des); fd=creat(des,S_IRUSR|S_IWUSR); fdsrc=o pen (src,O_RDONLY); if(fdsrc0) exit(1); fddes=open( des,flags,0744); if(fddes0) int z=write(fddes,buf, nbytes); if(z #include vunistd.h #include vst
5、dlib.h #include vstring.h #include vfcntl.h #include mylock.h #define MAXLEN #define ALPH ABET #define ALPH ABET p rintf(Read lock set by %dn, get pid(); break; p rintf(Write lock set by break; p rintf(Release lock by break; break; */ p roducer.c 10 /* /* START a /* #define COUNT OF ALPHABET 26 #def
6、ine DIGIT #define DIGIT START O %dn, get pid(); %dn, get pi d(); return 1; 緩沖區大小最大值*/ 表示使用英文字符*/ 頭一個字符,可以用A*/ /*字母字符的個數*/ /*表示使用數字字符*/ /*頭一個數字字符*/ #define COUNT OF DIGIT 10 /*數字字符的個數*/ #define SIGN TYPE ALPHABET /*本實例用英文字符*/ const char *fifo_file = ./myfifo; /* ! FIFO文件名*/ char buffMAXLEN; /* 緩沖區 */
7、 /*函數product。產生一個字符并寫入仿真FIFO文件中*/ int roduct(void) int fd; unsigned int sign_t ype, sign_start, sign_count, size; static unsigned int counter 0; /* 打開! FIFO文件*/ if (fd = open(fifo_file, O_CREAT|O_RDWR|O_APPEND, 0644) 0) p rintf(O pen fifo file errorn); exit(1); sign_ty pe =SIGN_T YPE; switch(sign_t
8、ype) case ALPH ABET: /*英文字符*/ sign_start = ALPH ABET_START; sign_count = COUNT_OF_AL PH ABET; break; case DIGIT: /*數字字符*/ sign_start = DIGIT_START; sign_count = COUNT_OF_DIGIT; break; default: return -1; /*end of switch*/ spnntf(buff, %c, (sign_start + counter); counter = (counter + 1) % sign_count;
9、 lock_set(fd, F_WRLCK); /* 上寫鎖 */ if (size = write(fd, buff, strlen(buff) 1) /* 第一個參數表示生產周期*/ sscanf(argv1, %d, if (argc 2) /* 第二個參數表示需要生產的資源數*/ sscanf(argv2, %d, while (time_life-) if (product() 0) break; slee p( time_ste p); mylock.h exit(EXIT_SUCCESS); 消費者程序的源代碼:customer.c #define MAX FILE SIZE 1
10、00 * 1024 *1024 /*100M*/ const char *fifo_file= ./myfifo; const char *tmp_file= ./tmp; /* /* 仿真FIFO 臨時文件名 文件名*/ */ /* 資源消費函數customing */ int customing(const char *myfifo, int need) int fd; char buff; int counter = 0; if (fd = op en(myfifo, O_RDONLY) p rintf(Function customing errorn); return -1; 0)
11、p rintf(Enjoy:); lseek(fd, SEEK_SET, 0); while (counter need) while (read(fd, /*-.就是在屏幕上/0的顯示*/ counter+; fp uts(n, stdout); close(fd); return 0; /* myfilecopy()函數:實現從sour_file文件的offset偏移處開始將count個字節數據復制到dest_file文件*/ int myfilecopy(const char *sour_file,const char *dest_file, int offset, int count,
12、 int copy_mode) int in_file, out_file; int counter = 0; char buff_unit; (in_file = op en(sour_file, O_RDONLY|O_NONBLOCK) 0) if p rintf(Function myfilec opy error in source filen); return -1; if (out_file = op en(dest_file, O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK,0644) 0) p rintf(Function myfilec opy error
13、 in destination file:); return -1; /* customer.c */ #include #include #include #include #include #include lseek(in_file, offset, SEEK_SET); while (read(in_file, counter+; - - close(in_file); close(out_file); return 0; 1) /*對資源進行消費,need表示該消費的資源數目*/ customing(fifo_file, need); if (fd = op en(fifo_file
14、, O_RDWR)1) /*第一個參數指定需要消費的資源數目,默認值為 if 10 */ sscanf(argv1, %d, if (customer_capacity 0) custom(customer_ca pacity); exit(EXIT_SUCCESS); 分別編譯生產者程序producer.c 和消費者程序customer.c 確保編譯沒有錯誤后,先在控制臺終端1上運行生產者程序:./producer 1 20 再在控制臺終端2上運行消費者程序:./customer 5 觀察兩終端的輸入輸出情況 實驗結果: !-/實崟5$ get producer.c -o producer
15、wxzgw咒工-Ltual-piachLne:f/實釜5$ ./producer i ze Write lock set by 597S Release lock by 697S Write Lock set by 597S Release Lock by 697& write lock set by 0975 Release Lock b# 697E Write lock set by 6975 Release lock bv 6975 iiWrlte lock set by 697S 循環出現,所以只截圖了其中一小部分。 I Release lock by 6975 I wxzwxz-vtrtual-nachlner/BSsi ./customer 5 I !En joy :abcde J Write lock set by 7023 -Lock failed:type 二 2 T iwxzgwxz-virtual-Piachtne:-/實驗5$
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年管理人員安全培訓考試試題附參考答案【基礎題】
- 25年公司、項目部、各個班組三級安全培訓考試試題含解析答案可打印
- 2025新入職工職前安全培訓考試試題(考點精練)
- 2025定期存款理財協議 合同書
- 2025存量房買賣合同書范本
- 2025年商業公寓租賃合同范本
- 2025年全屋板式家具項目合作計劃書
- 2025標準房產抵押借款合同
- 2025年樓宇監控系統合作協議書
- 2025酒店用品采購合同
- 華大新高考聯盟2025屆高三4月教學質量測評化學+答案
- 2025年中國防曬護理洗發露市場調查研究報告
- 2025年陜西省普通高中學業水平合格考試模擬卷(五)歷史試題(含答案)
- 2025年有關“我為群眾辦實事”主題日活動工作方案
- 油氣管道輸送試題及答案
- 鐵路雨季三防培訓課件
- 2025-2030中國非鄰苯二甲酸酯類增塑劑行業市場發展趨勢與前景展望戰略研究報告
- 靜療護理典型案例
- 大班音樂欣賞粵曲《荔枝頌》微課件
- 《肌內注射說課》ppt課件
- 沈萍微生物學第七章
評論
0/150
提交評論