西南交大操作系統實驗總報告_第1頁
西南交大操作系統實驗總報告_第2頁
西南交大操作系統實驗總報告_第3頁
西南交大操作系統實驗總報告_第4頁
西南交大操作系統實驗總報告_第5頁
已閱讀5頁,還剩16頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、操作系統實驗報告指導教師:胡曉鵬注:文檔可能無法思考全面,請瀏覽后下載,供參考。實驗報告一實驗名稱:Linux操作系統下的C語言編程實驗目的:1.認識Linux系統,熟悉Linux操作系統的基本操作;2. 了解vi命令的基本用法,能夠使用vi命令對文件 進行基礎的操作與編輯;3. 能夠在Linux環境下編寫C語言程序,生成.out文 件,并成功執行;4. 體會Linux環境下編程與Windows環境下編程的異同,加深對Linux操作系統的理解。實驗內容:熟悉Linux環境,編寫簡單C語言程序。實驗結果C you.c) - gedlt/Open fiA SaveJJJi4、Undov ifc H

2、Uyou.c x#1 nclude Int mal.n()ubuntu: dellolubuntu:IsDesktopDownloadshelloMusicPublicVideosyou.cDocumentsexamplesdgsktophellocPicturesTemplatesyoudell01ubuntu:psPID TTYTINECMD4899 pts/300:00:00bash4920 pts/300:00:00psdelleiubuntu:|實驗結論:在Linux操作系統下的編程環境不同于Windows,編譯器進行命令編譯。操作環境簡潔,大多以鍵盤操作。實驗報告二實驗名稱:基于進

3、程與線程的并發實驗目的:1. 了解進程與線程工作原理,掌握并發機制,學會編寫簡單的并發程序。2. 充分理解并掌握基于進程與線程的并發。實驗內容:在Linux環境進行基于進程與線程編寫實驗程序。 試驗核心代碼:int mainO pthread_t tidN;pid_t pid;pid=fork0;if(pid0) printf (,zfail to forkn);exit(l);else if (pid=0):、printf (the child process:n?z);elsesleep(lO);printf (the parent process: n/z);void *res;int

4、err3;err 10=pthread_create(&tid10, NULL,fnl, NULL);err 11=pthread_create(NULL,fn2,NULL);err Z2=pthread_create(&tid2, NULL,fn3, NULL);int i;for(i=0;iN;i+)if (err i= pthread_join(tidEi, &res) !=0) printf (cannot join the thread %dn,z, i);exit(l);return 0;void *fnl(void *arg)printf (,?the first thread

5、is donen);return (void *) 1;void *fn2(void *arg)printf (the second thread is donen,z);/ sleep (1);return (void *) 1;void *fn3(void *arg)printf (,?the third thread is donen);/ sleep (2);return (void *)1;實驗報告三實驗題目:進程間的通信實驗目的:學習在lirmx環境進程間的信息傳遞和接收,理解進程間的信號通信, 并能編寫簡單代碼實現進程間的通信。實驗代碼:nclude #include #incl

6、ude nclude nclude static int alarmfired=O;void ouch(int sig) alarm_fired=l:int main() pid_t pid;pid=fork();if(pid=-l) printf (,?fork failedn);exit(l);else if(pid=O) kill(getpid(), SIGALRM); exit (0):signal(SIGALRM, ouch);alarm(5);pause ();if(alarm_fired=l)printf(receive a signal %dn, SIGALRM);exit(O

7、);實驗結論:進程能進行信息通信,如果進程間有必要,能夠實現很好的進程異步處理。printf(Memory attached at 玄Xn,(int)shared_memory);/*讓結構體指針指向這塊共享內存權shared_stuff=(struct shared_use_st *)shared_memory:/*循環的向共享內存中寫數據,直到寫入的為“end”為止*/ while(running)while (shared_stuff-written_by_you=l)sleep(l);等到讀進程讀完之后再寫printf(waiting for client n);printf(?,en

8、er some text:);fgets(buffer, BUFSIZ, stdin);strncpy(shared_stuff-some_text, buffer, TEXT_SZ);share d_s tuff-wr i 11 en_by_you=1; if (strncmp(buffer, end, 3)=0)running=0;/結束循環/*II*/*創建共享內存*/shmid=shmget(key_t)1234, sizeof(struct shared_use_st), 0666 IPC_CREAT);if(shmid=-l)fprintf (stderr, shmget fail

9、edn/z);exit(EXIUAILURE);/*映射共享內存*/shared_memory=shmat(shmid, (void *)0,0);if (shared_memory= (void *)T)fprintf (stderr, ?,shmat failedn?,);exit(EXIUAILURE);printf (Memory attached at %Xnz/, (int) shared_memory);/*讓結構體指針指向這塊共享內存*/shared_stuff=(struct shared_use_st *)shared_memory;/*控制讀寫順序*/share d_s

10、t uff-wr i 11 en_by_you=0;/*循環的從共享內存中讀數據,直到讀到“end”為止*/while(running)if(shared_stuff-wr i 11 en_by_you)printf (You wrote:%s,z, shared_stuff-some_text);sleep(l); 讀進程睡一秒,同時會導致寫進程睡一秒,這樣做到讀了之后再 寫shared_stuff-written_by_you=0;if (strncmp (shared_stuff-some_text, end, 3)=0)running二0; /結束循環實驗結果: delloi(gubu

11、ntu: -delloiubuntu:*$ /shn2 nonery attached at CC99B0O8 ener some text:t am a girl watting for client waiting for client ener sone text:and you watting for client. ener sone text:end dellBlubuntu:-$ |實驗結論:內存共享中應該含有安全防護機制,內存在共享的同時應該做到內存 的保護,共享內存段是更應該做到安全高效共享。應當加強訓練共享內存段申請 原則,多多學習管道運輸的實現。實驗報告五實驗題目:虛擬內

12、存實驗目的:模擬替換算法技術,編程實現LRU和CLOCK算法,掌握替換算法技術, 完全實現替換策略。實驗主要函數:void lru_p(int n, int m, int proc):void clock_p(int n, int m, int proc):實驗代碼:#include include /準備用時鐘函數調用庫函數include using namespace std;void lru_p(int n, int m, int proc)cout,?z/endl;cout這是 least recently used: z/endl;struct pageint time;int pr

13、o;struct page *p= (struct page*malloc(n*sizeof(page);int i = 0;int flag二0;/記錄是否滿頁int exit;/初始化時間for (int j=O;jn;j+)pj time二0;while (im)/初始化進程不占用頁exit=0;for(j = 0;j flag;j+)pj time+;if(二二procti) /處理相同如何情況下的相同進程exit=l;pj time=l;if (exit0& (f lagn)/沒有相同進程且不缺頁 p.flag pro=proc.i;p.flag time=l;flag+

14、;elseif(exit-O) /處理缺頁沒有相同進程但缺頁int key=0;int maxtime=p0time;for(j=l;jn;j+)if (maxtimepj time) maxtime=pj time; key=j;pkey pro=proc ij;pkey time=l;輸岀for (j=0;jflag;j+)printf C %d ,pj pro);coutendl;i+;void clock_p(int n,int m, int proc)cout,z,zendl;cout這是 clock: /zendl;struct pageint flag;int pro;struc

15、t page *p=(struct page*)malloc(m*sizeof(page);/初始化隊列for (int k=0;kn;k+)pkl. flag=0;int i=0;int next;int j二0;/標記初始位苣int mart=0;while(im) next=l;while(next)for(k=0;kmart;k+)if(pk pro二二proci) next二0;if (next)if(!pjl. flag)pjl. pro=proci;pj. flag二1;next二0;if(martn)mart+;elsepj. flag二0;next二1;if(martn)ma

16、rt+;j+;/輸岀i+;for(k二0;kmart;k+)printf C %d pro);coutendl;void mainO int pages;cout 輸入頁面數:;cinpages;cout輸入你的進程個數:;int m;cinm;cout輸入你的進程ID:; int *proc=new intm;for (int i=0;inext)q=q-next;void inputdata(PCB* r);inputdata(r);r-next=q-next;q-next=r;向鏈表PCB ij點內容中輸入數據void inputdata(PCB* r)printf (進程名:”);sc

17、anf (n%c, &r-name);printf (?,到達時間:);scanf & r-reach_time); printf (/z運行時間:);scanf (駕d, & r-run_time);以“到達時間”的從大到小的順序對鏈表進行排序void seq(PCB* head)PCB* h=head;while(h-next-next)PCB* p=h-next, *q=h-next;while(q-next)q=q-next;if(p-reach_time q-reach_time)P二q;PCB* r=h;while (r-next!=p)r=r-next;r-next=p-next

18、;p-next=h-next;h-next=p;h=h-next;求最大的到達時間int mrtime(PCB* head)PCB* p=head;while(p-next)p=p-next;int maxreacht ime=p-reach_time;return maxreachtime;將各個進程運行時間的初始值賦給p-rt;在求帶權周轉時間的時候要用到void runtort(PCB* head)PCB* p=head;dop=p-next;p-rt=p-run_t ime;while(p-next);void output(PCB* head, int nums)PCB* t=hea

19、d;int zzs_time=0;float dqzzs_time二0.00;wh訂e(t-next!=NULL)t=t-next;printf (/z進程名:%cn,?, t-name);printf (?,t 周轉時間:%dn,z, t-zz_time);printf (?,t 帶權周轉時間:%fn?,, t-dqzz_time);z z s_t i me=z z s_t ime+t-z z_t i me;dqzzs_time=dqzzs_time+t-dqzz_time;printf (Xn 平均周轉時間:%fn,z, (float) zzs_time/nums);printf C平均帶權周轉時間:%fn?z, (float) dqzzs_time/nums);主函數void mainOint nums;printff請輸入要運行的進程個數:);scanf (爼d,&nums); /nums代表要運行的進程的個數;PCB* head;headlinkinit();

溫馨提示

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

評論

0/150

提交評論