操作系統(tǒng)進(jìn)程同步實(shí)驗(yàn)報(bào)告_第1頁
操作系統(tǒng)進(jìn)程同步實(shí)驗(yàn)報(bào)告_第2頁
操作系統(tǒng)進(jìn)程同步實(shí)驗(yàn)報(bào)告_第3頁
操作系統(tǒng)進(jìn)程同步實(shí)驗(yàn)報(bào)告_第4頁
操作系統(tǒng)進(jìn)程同步實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 實(shí)驗(yàn)三:進(jìn)程同步實(shí)驗(yàn)一、實(shí)驗(yàn)任務(wù):(1)掌握操作系統(tǒng)的進(jìn)程同步原理;(2)熟悉linux的進(jìn)程同步原語;(3)設(shè)計(jì)程序,實(shí)現(xiàn)經(jīng)典進(jìn)程同步問題。二、實(shí)驗(yàn)原理:(1)P、V操作PV操作由P操作原語和V操作原語組成(原語是不可中斷的過程),對(duì)信號(hào)量進(jìn)行操作,具體定義如下:P(S):將信號(hào)量S的值減1,即S=S-1;如果S³0,則該進(jìn)程繼續(xù)執(zhí)行;否則該進(jìn)程置為等待狀態(tài),排入等待隊(duì)列。V(S):將信號(hào)量S的值加1,即S=S+1;如果S>0,則該進(jìn)程繼續(xù)執(zhí)行;否則釋放隊(duì)列中第一個(gè)等待信號(hào)量的進(jìn)程。(2)信號(hào)量信號(hào)量(semaphore)的數(shù)據(jù)結(jié)構(gòu)為一個(gè)值和一個(gè)指針,指針指向等待該信號(hào)量的

2、下一個(gè)進(jìn)程。信號(hào)量的值與相應(yīng)資源的使用情況有關(guān)。當(dāng)它的值大于0時(shí),表示當(dāng)前可用資源的數(shù)量;當(dāng)它的值小于0時(shí),其絕對(duì)值表示等待使用該資源的進(jìn)程個(gè)數(shù)。注意,信號(hào)量的值僅能由PV操作來改變。一般來說,信號(hào)量S³0時(shí),S表示可用資源的數(shù)量。執(zhí)行一次P操作意味著請(qǐng)求分配一個(gè)單位資源,因此S的值減1;當(dāng)S<0時(shí),表示已經(jīng)沒有可用資源,請(qǐng)求者必須等待別的進(jìn)程釋放該類資源,它才能運(yùn)行下去。而執(zhí)行一個(gè)V操作意味著釋放一個(gè)單位資源,因此S的值加1;若S£0,表示有某些進(jìn)程正在等待該資源,因此要喚醒一個(gè)等待狀態(tài)的進(jìn)程,使之運(yùn)行下去。(3)linux的進(jìn)程同步原語wait();阻塞父進(jìn)程,子

3、進(jìn)程執(zhí)行;#include <sys/types.h>#include <sys/ipc.h>key_t ftok (char*pathname, char proj);它返回與路徑pathname相對(duì)應(yīng)的一個(gè)鍵值。int semget(key_t key, int nsems, int semflg) 參數(shù)key是一個(gè)鍵值,由ftok獲得,唯一標(biāo)識(shí)一個(gè)信號(hào)燈集,用法與msgget()中的key相同;參數(shù)nsems指定打開或者新創(chuàng)建的信號(hào)燈集中將包含信號(hào)燈的數(shù)目;semflg參數(shù)是一些標(biāo)志位。參數(shù)key和semflg的取值,以及何時(shí)打開已有信號(hào)燈集或者創(chuàng)建一個(gè)新的信號(hào)燈

4、集與msgget()中的對(duì)應(yīng)部分相同。該調(diào)用返回與健值key相對(duì)應(yīng)的信號(hào)燈集描述字。調(diào)用返回:成功返回信號(hào)燈集描述字,否則返回-1。int semop(int semid, struct sembuf *sops, unsigned nsops); semid是信號(hào)燈集ID,sops指向數(shù)組的每一個(gè)sembuf結(jié)構(gòu)都刻畫一個(gè)在特定信號(hào)燈上的操作。nsops為sops指向數(shù)組的大小。int semctl(int semid,int semnum,int cmd,union semun arg) 該系統(tǒng)調(diào)用實(shí)現(xiàn)對(duì)信號(hào)燈的各種控制操作,參數(shù)semid指定信號(hào)燈集,參數(shù)cmd指定具體的操作類型;參數(shù)s

5、emnum指定對(duì)哪個(gè)信號(hào)燈操作,只對(duì)幾個(gè)特殊的cmd操作有意義;arg用于設(shè)置或返回信號(hào)燈信息。三、實(shí)驗(yàn)源程序:#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#include<errno.h>#include<stdlib.h>#include<stdio.h>#include<ftl.h>#include<unistd.h>#include <string.h>#include <sys/stat.h>

6、#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#define PERM S_IRUSR|S_IWUSR#define SEMKEY (key_t)0x200typedef union _senum int val; struct semid_ds *buf; ushort *array;semun;int semid;static int count=0;FILE *fp,*fp1,*fp2;struct sembuf prmutex=0,-1,0,pwmutex=1,-1,0,p

7、s=2,-1,0;struct sembuf vrmutex=0,1,0,vwmutex=1,1,0,vs=2,1,0;int initsem() semun x; x.val=1; if(semid=semget(SEMKEY,3,0600|IPC_CREAT|IPC_EXCL)=-1) if(errno=EEXIST) semid=semget(SEMKEY,3,0); if(semctl(semid,0,SETVAL,x)=-1) perror("semctl failedn"); return(-1); if(semctl(semid,1,SETVAL,x)=-1)

8、 perror("semctl failedn"); return(-1); if(semctl(semid,2,SETVAL,x)=-1) perror("semctl failedn"); return(-1); return(semid);main() int i,j,k; static int a30; int shmid; int *pint,*pint2,addr,addr2; for(i=0;i<30;i+) ai=i; if(shmid=shmget(IPC_PRIVATE,4,PERM)=-1) fprintf(stderr,&q

9、uot;Create Share Memory Error:%sna",strerror(errno);exit(1); addr=shmat(shmid,0,0) ; pint=(int*)addr; *pint=0; semid=initsem(); if(fork()=0) /writer semop(semid,&pwmutex,1); printf("call writern"); fp1=fopen("a.txt","w"); for(k=0;k<20;k+) fprintf(fp1,"%

10、dn ",5*k); printf("write %dn ",5*k); fclose(fp1); printf("write finish!n"); semop(semid,&vwmutex,1); exit(0); else if(fork()=0) /reader 1 semop(semid,&prmutex,1); addr2=shmat(shmid,0,0); pint2=(int*)addr2; if(*pint2=0) semop(semid,&pwmutex,1); *pint2=*pint2+1; pr

11、intf("reader 1 enter- count=%dn",*pint2); semop(semid,&vrmutex,1); fp=fopen("a.txt","r"); while(!feof(fp) fscanf(fp,"%d ",&i); printf("reader 1 %dn ",i); semop(semid,&prmutex,1); *pint2=*pint2-1; printf("reader 1 exit- count=%dn"

12、;,*pint2); /count=count-1; /printf("count=%dn",count); if(*pint2=0) semop(semid,&vwmutex,1); semop(semid,&vrmutex,1); exit(0); else if(fork()=0) /reader semop(semid,&prmutex,1); addr2=shmat(shmid,0,0); pint2=(int*)addr2; if(*pint2=0) semop(semid,&pwmutex,1); *pint2=*pint2+1

13、; printf("Read 2 enter+ count=%dn",*pint2); / printf("Read 2 count=%dn",count); /count=2; /printf("count=%dn",count); semop(semid,&vrmutex,1); fp=fopen("a.txt","r"); while(!feof(fp) fscanf(fp,"%d ",&i); printf("reader 2 %dn ",i); semop(semid,&prmutex,1); /count=count-1

溫馨提示

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

評(píng)論

0/150

提交評(píng)論