Linux操作系統(tǒng)課程設計_第1頁
Linux操作系統(tǒng)課程設計_第2頁
Linux操作系統(tǒng)課程設計_第3頁
Linux操作系統(tǒng)課程設計_第4頁
Linux操作系統(tǒng)課程設計_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、Linux操作系統(tǒng)課程設計一、利用Linux有關系統(tǒng)調(diào)用函數(shù)編寫一個簡單的文件管理工具,要求實現(xiàn)以下功能(可在任意目錄下操作)。功能說明(提示)1.創(chuàng)建新文件open(),close()2.寫文件open(),write()3.讀文件read()4.復制文件read(),write()5.查看文件權(quán)限需使用execv()函數(shù)執(zhí)行”ls -l”命令實現(xiàn)6.修改文件權(quán)限chmod()7.創(chuàng)建目錄mkdir()8.查看當前路徑與目錄類同59.刪除目錄rmdir()10.切換目錄chdir()11.建立文件鏈接link()0.退出exit()二、通過訪問/proc文件系統(tǒng)來獲取系統(tǒng)的當前信息,包括:(

2、1)進程信息。包括:進程名稱、運行狀態(tài)、PID、優(yōu)先級、內(nèi)存使用量。可結(jié)束任一進程。(2)系統(tǒng)信息。包括:處理器信息(CPU名稱、CPU頻率、CPU類型、緩存大小),操作系統(tǒng)信息(系統(tǒng)類型、系統(tǒng)版本、GCC編譯版本)。(3)內(nèi)存資源。包括:內(nèi)存和緩沖區(qū)(內(nèi)核使用情況(已用、剩余、中共)、交換區(qū)使用情況(已用、剩余、中共),CPU使用率(各個核的使用率)。(4)模塊信息。包括:模塊名稱、內(nèi)存使用、使用次數(shù)。可卸載任一模塊。一、利用Linux有關系統(tǒng)調(diào)用函數(shù)編寫一個簡單的文件管理工具程序代碼:#include <iostream>#include <>#include &l

3、t;fstream>#include <>using namespace std;int showmenu() /顯示菜單,在主函數(shù)中循環(huán)調(diào)用。返回用戶選擇的選項。 int option; cout<<"1.創(chuàng)建新文件n" cout<<"2.寫文件n" cout<<"3.讀文件n" cout<<"4.復制文件n" cout<<"5.查看文件權(quán)限n" cout<<"6.修改文件權(quán)限n" c

4、out<<"7.創(chuàng)建目錄n" cout<<"8.查看當前路徑與目錄n" cout<<"9.刪除目錄n10.切換目錄n" cout<<"11.建立文件鏈接n0.退出n" cin>>option; return option;void createfile() /以用戶輸入的文件名創(chuàng)建新文件 string filename; cout<<"input the filenamen" cin>>filename; ofs

5、tream of; ( (); if (!of) cerr<<"open fail"<<endl; ();void insert() /寫入用戶指定的內(nèi)容到指定文件 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() /讀取文件內(nèi)容并顯示 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; /*將文件內(nèi)容讀取到buffer中*/ ifstream in; ( (); if (!in) cerr<<"open fail"<<endl; char buffer1024; while (!() (buffer,10

8、0); (); /*將buffer中的內(nèi)容寫入新文件*/ 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) /在子進程中執(zhí)行

9、路徑為/command的程序,參數(shù)在argv中 int pid = fork(); if (pid = 0) if (execv(command, argv) = -1) cout<<"=>errorn" else sleep(1); /等待子進程執(zhí)行完畢#include <iostream>#include ""using namespace std;int main() while (true) /keeping showing the menu int option; option = showmenu(); swit

10、ch(option) case 1: /創(chuàng)建新文件 createfile(); break; case 2: /寫入 insert(); break; case 3: /讀取 readfile(); break; case 4: /復制 copyfile(); break; case 5: /查看權(quán)限 char * argv = "ls","-l",NULL; char * path = "/bin/ls" executecommand(path, argv); break; case 6: /修改權(quán)限 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: /創(chuàng)建目錄 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()函數(shù)顯示菜單,createfile()函數(shù)創(chuàng)建新文件,insert()函數(shù)寫文件,readfile()函數(shù)讀文件,copyfile()函數(shù)復制文件,executecommand()函數(shù)執(zhí)行命令。filehandler.pp文件中,主函數(shù)為一個死循環(huán),調(diào)用showmenu()函數(shù)顯示菜單、獲取用戶選擇的選項,之后通過switch匹配相應的函數(shù)。實驗截圖:之后運行程序,顯示菜單,接著我們一個一個功能測試選擇功能1并輸入文件名后,可以看到文件夾中確實創(chuàng)建了新文件

17、選擇功能2之后、輸入要寫入的文件的文件名,再輸入要寫入的內(nèi)容選擇功能3之后,輸入要讀取的文件的文件名,在提示符” =”之后的是文件內(nèi)容選擇功能4,依次輸入舊文件名,新文件名選擇功能5,調(diào)用命令ls查看權(quán)限選擇功能6,輸入文件名xiexie,并輸入777之后再選擇功能5,可以看到test2的權(quán)限變成了rwxrwxrwx選擇功能7,輸入新目錄的名字選擇功能8,查看當前目錄選擇功能9,輸入剛剛創(chuàng)建的目錄的目錄名,確實刪除了選擇功能10,切換到/home目錄通過功能10切換會之前的shiyan6目錄之后,選擇功能11,創(chuàng)建filehandler.h的文件連接二、通過訪問/proc文件系統(tǒng)來獲取系統(tǒng)的當

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.查看系統(tǒng)信息* n");printf("*3.查看內(nèi)存資源* n");printf("*4.查看模塊信息* n");printf("*0.退出該系統(tǒng)* 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("*現(xiàn)在正在進入進程信息界面* n");execv(pa,arg); printf("ps:在該界面你可以輸入k進行殺死進程,輸入k之后再輸入進程編號PID號n"); printf("ps:如果你想要退出該界面,可以輸入q進行退出 n");elsewait(0);void SIF(void) printf("- n");printf("* 系統(tǒng)信息如下 * 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èi)存的信息如下* n");printf(

溫馨提示

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

評論

0/150

提交評論