




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Linux操作系統課程設計一、利用Linux有關系統調用函數編寫一個簡單的文件管理工具,要求實現以下功能(可在任意目錄下操作)。功能說明(提示)1.創建新文件open(),close()2.寫文件open(),write()3.讀文件read()4.復制文件read(),write()5.查看文件權限需使用execv()函數執行”ls -l”命令實現6.修改文件權限chmod()7.創建目錄mkdir()8.查看當前路徑與目錄類同59.刪除目錄rmdir()10.切換目錄chdir()11.建立文件鏈接link()0.退出exit()二、通過訪問/proc文件系統來獲取系統的當前信息,包括:(
2、1)進程信息。包括:進程名稱、運行狀態、PID、優先級、內存使用量。可結束任一進程。(2)系統信息。包括:處理器信息(CPU名稱、CPU頻率、CPU類型、緩存大小),操作系統信息(系統類型、系統版本、GCC編譯版本)。(3)內存資源。包括:內存和緩沖區(內核使用情況(已用、剩余、中共)、交換區使用情況(已用、剩余、中共),CPU使用率(各個核的使用率)。(4)模塊信息。包括:模塊名稱、內存使用、使用次數。可卸載任一模塊。一、利用Linux有關系統調用函數編寫一個簡單的文件管理工具程序代碼:#include <iostream>#include <>#include &l
3、t;fstream>#include <>using namespace std;int showmenu() /顯示菜單,在主函數中循環調用。返回用戶選擇的選項。 int option; cout<<"1.創建新文件n" cout<<"2.寫文件n" cout<<"3.讀文件n" cout<<"4.復制文件n" cout<<"5.查看文件權限n" cout<<"6.修改文件權限n" c
4、out<<"7.創建目錄n" cout<<"8.查看當前路徑與目錄n" cout<<"9.刪除目錄n10.切換目錄n" cout<<"11.建立文件鏈接n0.退出n" cin>>option; return option;void createfile() /以用戶輸入的文件名創建新文件 string filename; cout<<"input the filenamen" cin>>filename; ofs
5、tream of; ( (); if (!of) cerr<<"open fail"<<endl; ();void insert() /寫入用戶指定的內容到指定文件 string filename, msg; cout<<"input the filenamen" cin>>filename; cout<<"input something you want to insertn" cin>>msg; ofstream out; ( (); if (!out) ce
6、rr<<"open fail"<<endl; out<<(); ();void readfile() /讀取文件內容并顯示 string filename; cout<<"input the filenamen" cin>>filename; ifstream in; ( (); if (!in) cerr<<"open fail"<<endl; char buffer1024; while (!() (buffer,100); cout<<
7、;"=>"<<buffer<<endl; ();void copyfile() /復制文件 string ifilename; cout<<"input the filename of old filen" cin>>ifilename; /*將文件內容讀取到buffer中*/ ifstream in; ( (); if (!in) cerr<<"open fail"<<endl; char buffer1024; while (!() (buffer,10
8、0); (); /*將buffer中的內容寫入新文件*/ string ofilename, msg; cout<<"input the filename of new file n" cin>>ofilename; ofstream out; ( (); if (!out) cerr<<"open fail"<<endl; out<<buffer; ();void executecommand(const char * command, char * const* argv) /在子進程中執行
9、路徑為/command的程序,參數在argv中 int pid = fork(); if (pid = 0) if (execv(command, argv) = -1) cout<<"=>errorn" else sleep(1); /等待子進程執行完畢#include <iostream>#include ""using namespace std;int main() while (true) /keeping showing the menu int option; option = showmenu(); swit
10、ch(option) case 1: /創建新文件 createfile(); break; case 2: /寫入 insert(); break; case 3: /讀取 readfile(); break; case 4: /復制 copyfile(); break; case 5: /查看權限 char * argv = "ls","-l",NULL; char * path = "/bin/ls" executecommand(path, argv); break; case 6: /修改權限 string filename
11、; string mod; cout<<"input the filenamen" cin>>filename; cout<<"input the mode, r=4,w=2,x=1。 example:777 is rwxrwxrwxn" cin>>mod; char f20,m10; char * argv = "chmod", strcpy(m, (), strcpy(f, (), NULL; char * path = "/bin/chmod" executec
12、ommand(path, argv); break; case 7: /創建目錄 string foldername; cout<<"input the foldernamen" cin>>foldername; char f20; char * argv = "mkdir", strcpy(f, (), NULL; char * path = "/bin/mkdir" executecommand(path, argv); break; case 8: /查看當前路徑 char * argv = "
13、pwd", NULL; char * path = "/bin/pwd" executecommand(path, argv); break; case 9: /切換目錄 string foldername; cout<<"input the foldernamen" cin>>foldername; char f20; char * argv = "rm", strcpy(f, (), "-r", NULL; char * path = "/bin/rm" e
14、xecutecommand(path, argv); break; case 10: /切換目錄 string dir; cout<<"input the path you want to be n" cin>>dir; char p30; if (chdir(strcpy(p, () = -1) cout<<"fail to change dir"<<endl; break; case 11: /建立文件連接 string oldpath,newpath; cout<<"input
15、old path n" cin>>oldpath; cout<<"input new pathn" cin>>newpath; char np30,op34; if (link(strcpy(op, (), strcpy(np, () = -1) cout<<"fail to change dir"<<endl;break; case 0: return 0; default: cout << "請選擇0到11" << endl; break
16、; cout<<"n" 程序分為和兩部分,主要功能保存在filehandler,h中,由showmenu()函數顯示菜單,createfile()函數創建新文件,insert()函數寫文件,readfile()函數讀文件,copyfile()函數復制文件,executecommand()函數執行命令。filehandler.pp文件中,主函數為一個死循環,調用showmenu()函數顯示菜單、獲取用戶選擇的選項,之后通過switch匹配相應的函數。實驗截圖:之后運行程序,顯示菜單,接著我們一個一個功能測試選擇功能1并輸入文件名后,可以看到文件夾中確實創建了新文件
17、選擇功能2之后、輸入要寫入的文件的文件名,再輸入要寫入的內容選擇功能3之后,輸入要讀取的文件的文件名,在提示符” =”之后的是文件內容選擇功能4,依次輸入舊文件名,新文件名選擇功能5,調用命令ls查看權限選擇功能6,輸入文件名xiexie,并輸入777之后再選擇功能5,可以看到test2的權限變成了rwxrwxrwx選擇功能7,輸入新目錄的名字選擇功能8,查看當前目錄選擇功能9,輸入剛剛創建的目錄的目錄名,確實刪除了選擇功能10,切換到/home目錄通過功能10切換會之前的shiyan6目錄之后,選擇功能11,創建filehandler.h的文件連接二、通過訪問/proc文件系統來獲取系統的當
18、前信息程序代碼:/F#include<>#include<sys/>#include<>#include<>#include<sys/>#include<>#include<>#include<>#include <>void menu(void);void PIF(void);void SIF(void);void MIF(void);void BIF(void);int main() int choose;menu();scanf("%d",&choose
19、);while(choose!=0) switch(choose) case 1:PIF();break;case 2:SIF();break;case 3:MIF();break;case 4:BIF();break;default:printf("*沒有該選項,請重新輸入*n");menu();scanf("%d",&choose);return 0;void menu(void) printf("* n");printf("*親愛的用戶請輸出你需要的操作* n");printf("*1.查看
20、進程信息* n");printf("*2.查看系統信息* n");printf("*3.查看內存資源* n");printf("*4.查看模塊信息* n");printf("*0.退出該系統* n");printf("* n");printf("*請輸入1-4*n");void PIF(void) char *pa = "/usr/bin/X11/top"char *arg4 = "top", NULL;if(fork()=0
21、) printf("*現在正在進入進程信息界面* n");execv(pa,arg); printf("ps:在該界面你可以輸入k進行殺死進程,輸入k之后再輸入進程編號PID號n"); printf("ps:如果你想要退出該界面,可以輸入q進行退出 n");elsewait(0);void SIF(void) printf("- n");printf("* 系統信息如下 * n");printf("- n");if(fork()=0) execlp("/bin/ca
22、t","cat","/proc/version",NULL);elsewait(0);printf("- n");printf("* 處理器信息如下* * n");printf("- n");if(fork()=0) execlp("/bin/cat","cat","/proc/cpuinfo",NULL);elsewait(0);void MIF(void) printf("- n");printf("* 有關內存的信息如下* n");printf(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 聘任專家協議書
- 教師赴企業培訓協議書
- 美簽互換協議書
- 租賃期權協議書
- 紫光展銳協議書
- 生產線外包合同協議書
- 貓咪售后協議書
- 職場體驗協議書
- 校區合伙人合同協議書
- 藥品進貨協議書
- 安徽省合肥一中2025屆高三5月回歸教材讀本
- 2024年江蘇省無錫市中考歷史真題(解析版)
- 雙方房屋使用協議書
- 投資理財保本協議書
- 2025年廣東省深圳市中考道德與法治 總復習責任意識檢測卷(含答案)
- 地西半球的國家 復習課課件-2024-2025學年七年級地理下學期(人教版2024)
- 2025法語DELFA1級考試試卷
- 2025年黃山旅游發展股份有限公司春季招聘75人筆試參考題庫附帶答案詳解
- 2025年中考時事政治題及答案
- 2025-2030全球及中國發電機租賃行業市場現狀供需分析及市場深度研究發展前景及規劃可行性分析研究報告
- 第10課 相親相愛一家人 課件-2024-2025學年道德與法治一年級下冊統編版
評論
0/150
提交評論