計(jì)算機(jī)操作系統(tǒng)教程實(shí)驗(yàn)指導(dǎo)(第2版)_第1頁(yè)
計(jì)算機(jī)操作系統(tǒng)教程實(shí)驗(yàn)指導(dǎo)(第2版)_第2頁(yè)
計(jì)算機(jī)操作系統(tǒng)教程實(shí)驗(yàn)指導(dǎo)(第2版)_第3頁(yè)
計(jì)算機(jī)操作系統(tǒng)教程實(shí)驗(yàn)指導(dǎo)(第2版)_第4頁(yè)
計(jì)算機(jī)操作系統(tǒng)教程實(shí)驗(yàn)指導(dǎo)(第2版)_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1.編程管理文件makefike

filsys:main.oingetput.oiallfre.oballfre.oname.oaccess.olog.oclose.ocreat.o

delete.odir.oopen.ordwt.oformat.oinstall.ohalt.occ-ofilsysmain.o

ingetput.oiallfre.oballfre.oname.oaccess.olog.oclose.ocreat.odelete.odir.o

open.ordwt.oformat.oinstall.ohalt.o

main.o:main.cfilesys.hcc-cmain.c

igetput.o:igetput.cfilesys.hcc-cigetput.c

iallfre.o:iallfre.cfilesys.hcc-ciallfre.c

ballfre.o:ballfre.cfilesys.hcc-cballfre.c

name.o:name.cfilesys.hcc-cname.c

accesse.o:access.cfilesv**s.hcc-caccess.c

log.o:log.cfilesys.hcc-clog.c

close.o:close.cfilesys.hcc-cclose.c

creat.o:creat.cfilesys.hcc-ccreat.c

delete.o:delete.cfilesys.hcc-cdelete.c

dir.o:dir.cfilesys.hcc-cdir.c

open.o:open.cfilesys.hcc-copen.c

rdwt.o:rdwt.cfilesys.hcccrdwt.c

format.o:format.cfilesys.hcc-cformat.c

install.o:install.cfilesys.hcc-cinstall.c

halt.o:halt.ccc-chalt.o

2.頭文件filesye.h

^defineBLOCKSIZE512

#dcfincSYSOPENFILE40

#defineDIRNUM128

#definePWDSIZ14

#definePWDNUM12

#defineNOFILE20

^defineNADDK10

#defineNHINO128

#defineUSERNUM10

#defineDINODESIZ32

#defineFILEBLK512

#dcfineN1CFREE50

#defineNICNOD5()

#defineDINODESTART2*BLOCKSIZ

#defineDATASTART(2+DINODEBLK)*BLOCKSIZ

#defineDIEMPTY00000

#defineDIFILE01000

#defineDIDIR02000

#defmelIDIREAD00001

#defineUDIWRITE0(X)02

#defineUDIEXICUTE00004

#defineGDIREAD00010

#defineGDIWRITE00020

#defineGDIEXICUTE00040

#defineODIREAD00100

#defineODIWRITE()020()

#defineODIEXICUTE00400

#defineREAD1

#defineWRITE2

#defineEXICUTE3

^defineDEFAULTMODE00777

#dctme1UPDATE00002

#defineSUPDATE()0001

#defineFREAD00001

#defineFWRITE00002

#defineFAPPEND00004

^defineDISKFULL65525

2

#defineSEEL..SET0

/*文件系統(tǒng)數(shù)據(jù)結(jié)構(gòu)*、

structinode{

structinode*i_frow;

structinode*i_back;

chari_flag;

unsignedinti_ino;

unsignedinti_count;

unsignedshortdi_number;

unsignedshortdi_moder;

unsignedshortdi_uid;

unsignedshortdi_gid;

unsignedshortdi_size;

unsignedintdi_addr[NADDR|;

);

structdinode{

unsignedshortdi_number;

unsignedshortdi_moder;

unsignedshortdi_uid;

unsignedshortdi_gid;

unsignedlongdi_size;

unsignedintdi_addr[NADDR];

);

structdiicel{

chard_name[DIRSIZ];

unsignedintd_ino;

};

structfilsys{

unsignedshorts_size;

unsignedlongs_fsize;

3

unsignedints_nfree;

unsignedshorts_pfree;

unsignedints_free[NICFREE];

unsignedints_ninode;

unsignedshorts_pinode;

unsignedints_inode[NICINOD];

unsignedints_rinode;

chars_fmod;

);

structpwd{

unsignedshortp_uid;

unsignedshc)rtp_gid;

charpssword[PWDSIZE];

);

strcutdir{

structdirectdirectfDIRNUMI;

intsize;

};

structhinode(

structinode*i_forw;

};

structuser{

unsignedshortu_delault_mode;

unsignedshoilu_uid;

unsignedshortu_gid;

unsignedshortu_ofile[NOFILE];

/*下為全局變量*/

externstructhinodehinode[NHIN01;

externstructdirdir;

4

externstructfilesys_ofile[SYSOPENFILE];

externstructfilsysfilsys;

externstructpwdpwdjPWDNUM];

externFILE*fd;

externstructinode*cur_path_inodc;

externintuser_id;

externstructinode*iget()

externiput();

externunsignedintballoc();

externbfree();

externstructinode*ialloc();

externifree();

externunsignedintnamei();

externunsignedshortiname();

externunsignedintaccess();

extern_dir();

externmkdir();

externchdir();

externunsignedshortopen();

externcreat();

externunsignedintreadf);

externunsignedintwrite();

externintlogin();

cxlcminslallO;

externformat();

externclose();

externhalt();

3.主程序main()(文件名main.c)

#include<stdio.h>

5

#include"filesys.h"

structhinodehinode[NHINO];

structdirdir;

structfilesys_ofile[SYSOPENFILE];

structfilsysfilsys;

structpwdpwd[PWDNUM];

structuseruser[USERNUM];

FILE*fd;

structinode*cur_path_inode;

intuer_id;

inain()

(

unsignedshortab_fd1,ab_fd2,ab_fd3,ab_fd4;

unsignedshortbhy_fd1;

char*buf;

printf("\nDoyouwanttoformatthedisk\n");

if(getchar()=='y')

printf(n\nFormatwilleraseallcontextonthedisk\nM);

if(getchar()=-y')

format();

instalK);

_dir();

long(2118,"abcdn);

user_id=0;

6

mkdir("a2118");

chdir("a2118");

wj_fd1=creat(2118;'ab_fiIeO.cH,01777);

buf=(char*)malloc(BLOCKSIZ*6+5);

writc(ab_fd1,buf,BLOCKSIZ*6+5);

close(user_id,ab_fcl1);

free(buf);

mkdir('^subdir'^);

chdir("subdir");

wj_fd2=creat(2118,"file1.c",01777);

hu^(char*)malloc(RLOCKSIZ*4+20);

write(ab_fd2,buf,BLOCKSIZ*4+20);

close(user_id,ab_fd2);

free(buf);

chdir("..n);

ab_fd3=creat(2118J_file2.c”,01777);

buf-(char*)mal!oc(BLOCKSIZ*10+255);

write(ab_fd3,buf,BLOCKSIZ*3+255);

close(ab_fd3);

free(buf);

delete("ab_fileO.cM);

ab_fd4=crcat(2118Jab_fnc3.c”,01777);

buf=(char*)malloc(BLOCKSIZ*8+3()0);

write(ab_fd4,buf,BLOCKSIZ*8+300);

close(ab_fd4);

free(buf);

7

ab_fd3=open(2188,"ab_file2.c",FAPPEND);

buf=(char*)malloc(BLOCKSIZ*3+100);

write(ab_fd3,buf,BLOCKSIZ*3+100);

close(ab_fd3);

free(buf);

_dir();

chdir("..n);

logout();

halt();

)

4.初始化磁盤格式程序format。(文件名format.c)

#include<stdio.h>

#include"filesys.h"

format()

(

structinode*inode;

structdirectdir_buf[BLOCKSIZ/(DIRSIZ+2)];

structpwdpasswd(BLOCKSIZ/(PWDSIZ+4)];

/*{

{2116,03,\kkkT};

{2U7,03;'bbbb"(;

{2118,04/,abcd");

{2119,04;'ccccH);

{2220,05;'eeee"};

};

*/

8

structfilsys;

unsignedintblock_buf[BLOCKSIZ/sizeof(int)];

char*buf;

inti,j;

/*crcatthefilesystemfile*/

fd=fopen("filesysteni","r+w+b");

buf=(char*)malloc(DINODENLK+FILEBLK+2)*BLOCKSIZ*sizeof(char));

if(buf==NULL)

(

printf("\nfilesystemfilecreatfailed!!!\n");

exit(O);

)

fseek(fd,(),SEEK_SET);

fwrite(buf,l,(DINODENLK+FILEBLK+2)*BLOCKSIZ*sizeof(char),fd);

/*O.initializethepasswd*/

passwd[0J.p_uid-2116;passwd[O].p_gid_O3;

strcpy(passwd[()].password,"dddd");

passwd[l].p_uid=2117;passwd[1].p_gid=O3;

strcpy(passwd[1].password,"bbbbn);

passwd[2].p_uid=2118;passwd[2],p_gid=04;

strcpy(passwd[2].password,"abed");

passwdl3].p_uid-2119;passwd[3].p_gid-04;

strcpy(passwd[3J.password,"cccc");

passwd[4].p_uid=2220;passwd[4].p_gid=()5;

strcpy(passwd[4].password,"eeee");

/*1.Creatthemaindirectoryanditssubdiretcandthefilepassword*/

9

inode=iget(0);/*0emptydinodeid*/

inode一>di_moder=DIEMPTY;

iput(inode);

inode=igct(l);/*1maindirid*/

inode—>di_number=1;

inode—>di_moder=DEFAUUFMODE|DIDIR;

inode—>di_size=3*(DIRSIZ+2);

inode一>di_addi[0]=0;/*block0#isusedbythemaindirectory*/

strcpy(dir_buf[O].d_name,H..");

dir_buf[0].d_ino=1;

strcpy(dir_buf[1].d_name,

dir_buf[l].d_ino=l;

strcpy(dir_buff2].d_name,"etc");

dir_buf[2].d_ino=2;

fseek(fd,DATASTART,SEEK_SET);

fwrite(dir_buf,1,3*(DIRSIZ+2),fd);

iput(jnodc);

inode=iget(2);/*2etcdirid*/

inode—>di_number=l;

inode—>di_moder=DEFAULTMODE|DIDIR;

inode一>di_size=3*(D1RSIZ+2);

inode—>di_addr[0]=l;/*block1#isusedbythemaindirectory*/

strcpy(dir_buf[OJ.d_naiTie,H..u);

dir_buf[()].d_ino=l;

strcpy(dir_buff1].d_name,H.u);

dir_buffl].d_ino=2;

strcpy(dir_buf(2].d_name,"password");

dir_buf[2].d_ino=3;

io

fseek(fd,DATASTART,4-BL0CKSIZ*1,SEEK_SET);

fwrite(dir_buf,1,3*(DIRSIZ+2),fd);

iput(jnode);

inode=igct(3);/*2passwordid*/

inode—>di_number=1;

inode—>di_moder=DEFAULTMODE|DIDIR;

inode一>di_size=BLOCKSIZ;

inode一>di_addr[0]=2;/*block2#isusedbythemainfile*/

for(i=5,i<PWDNUM,i++)

{

passwd[iJ.p_uid=O;

passwd[i].p_gid=();

strcpy(passwd[i].password,"");

)

fseek(fd,DATASTART,+BLOCKSIZ*2,SEEK_SET);

fwrite(passwd,1,BLOCKSIZ,fd);

iput(jnodc);

/*2.initializethesuperblock*/

fisys.s_isize=DINODEBLK;

fisys.s_fsize=FILEBLK;

fisys.5_niiiodc-DINODEBLK*BLOCKSIZ/DINODEBLK-4;

fisys.s_nfrce=FILEBLK-3;

for(i=0;i<=NICINOD;i++)

{/*beginwith4,0,1,2,3,isusedbymain,etc,password*/

fisys.s_inode[i]=4+i;

ii

fisys.s_pinode=0;

fisys.s_rinode=NICINOD+4;

block_buf[NICFREE-1]=FILEBLK+1;/*FILEBLK+1isaflagofend*/

for(i=0;i<=NICFREE-l;i++)

block_bufINICFREE-2-i]=FILEBLK-i;

fseek(fd,DATASTART+BLOCKSIZ*(FILEBLK-NICFREE-1),SEEK_SET);

fwrite(block_buf,1,BLOCKSIZ,fd);

for(i=FILEBLK-NICFREE-1;i>2;i-二NICFREE)

{

for(j=O;j<NICFREEJ++)

(

block_buffj]=i-j;

}

fseek(fd,DATASTART+BLOCKSIZ*(i-1),SEEK_SET);

fwrite(block_buf,1,BLOCKSIZ,fd);

I

j=l;

for(i=i;i>2;i-)

(

filsys.s_free[NICFREE+i-j]=i;

)

filsy5.s_pfrcc-NICFREE-j;

fiisys.s_pinodc=0;

fseek(fd,BLOCKSIZ),SEEK_SET);

fwrite(&filsys,1,sizeof(structfilsys),fd);

12

5.進(jìn)入文件系統(tǒng)程序install()(文件名instalLc)

#include<stdio.h>

#include<string.h>

#include"filesys.h"

install()

{

inti,j;

/*O.openthefilecolumn*/

fd=fopcn("filesystemM,"w+r+b");

if(fd=NULL)

(

printf("\nfilesyscannotbeloaded\n);

exit(O);

)

/*l.readthefilsysfromthesuperblock*/

fseek(fd,BLOCKSOZ,SEEK_SET);

fwrite(&filsys,1?sizeof(structfilsys),fd);

/*2.initializetheinodebashchain*/

for(i=0;i<NHINO;i++)

(

hinode[i].i_foiw-NULL;

)

/*3.initializethesys_ofile*/

for(i=0;i<SYSOPENFILE;i++)

sys_ofile[i].f_counl=0;

13

sys_ofiIefi].f_inode=NULL;

/*4.initializetheuser*/

for(i=0;i<USERNUM;i++)

I

user[i].u_uid=O;

user[i].u_gid=O;

for(j=0;j<NOFILE;j+4-)

(

user[i].u_ofile|j]=SYSOPENFILE+1;

)

I

/*5.Readthemaindirectorytoinitializethedir*/

cut_path_inode=iger(1);

dir.size=cur_path_inode->di_size/(DlRSIZ+2);

for(i-0;i<DIRNUM;i++)

(

strcpy(dir.direct[i].d_name,"");

dir.direct[i].d_ino=0;

)

for(i=0;i<dir.size/(BL0CKSIZ/(DIRSIZ+2));i++)

(

fseek(fd,DATASTART4-BLOCKSIZ*cut_path_inodc->di_addr[iJ,SEEK_SET);

fread(&dir.direct[iBLOCKSIZ/(DIRSIZ+2))*i],l,BLOCKSIZ,fd);

)

fseek(fd,DATASTART+BLOCKSIZ*cut_path_inode->di_addr[i],SEEK_SET);

fread(&dir.direct[(BLOCKSIZ/(DIRSIZ+2))*i],1,

cut_path_inode->di_size%BLOCKS[Z,fd);

14

)

6.退出程序halt()(文件名halt.c)

#include<stdio.h>

#include"filesys.h"

halt()

{

structinode*inode;

inti.j;

/*1.writebackthecurrentdir*/

chdir("..n);

iput(cur_path_inode);

/*2.Freetheu_ofileandsys_ofileandinode*/

for(i=0;i<USERNUM;i++)

{

if(user[i].u_uid!=O)

(

for(j=0;j<NOFILE;j++)

(

if(user[i].u_ofile!=SYSOPENFILE+l)

{

clusc(U5cr[i].u_ofilc|JJ);

userliJ.u_ofile=SYSOPENFILE+1;

)

)

}

)

15

/*3.Writebackthefilesystothedisk*/

fseek(fd,BLOCKSIZ,SEEK_SET);

fwrite(&filsys,1,sizeof(structfilsys),fd);

/*4.closethefilesystemcolum*/

fclose(fd);

/*5.SayGoodebyetoalltheuser*/

printf("\nGoodBye.Seeyounexttime.Pleaseturnofftheswitch\nH);

exit(0);

7.獲取釋放i節(jié)點(diǎn)內(nèi)容程序iget()iput()(文件名igetputc)

#include<stdio.h>

#include"filesys.h"

structinode*igel(dinodeid)/*igel()*/

unsignedintdinodeid;

(

intexisted=0.inodeid;

longaddr;

structinode*lemp,*newinode;

iiioduid-dinodcid%NHINO;

if(hinodc[inodeid].i_forw==NULL)cxistcd=0;

else

(

temp=hinode[inodeid].i_f<)rw;

while(temp)

16

if(temp->i_ino==inodeid)

/*existed*/

{

existcd=l;

tcmp->i_count++;

returntemp;

)

/*notexisted*/

else

temp=temp->i_forw;

};

1

/*notexisted*/

/*1.calculatetheaddrofthedinodeinthefilesyscolumn*/

addr=DINODESTART+dinodeid*DINODESIZ;

/*2.mallocthenewinode*/

newinode=(structinode*)malloc(sizeof(structinode));

/*3.readthedinodctotheinode*/

fseek(fd,addr,SEEK_SET);

fread(&(newinode->di_number),DINODESIZ,l,fd);

/*4.putitintohinode[inodeid]queue*/

newinode->i_forw=hinode[inodeid].i_forw;

newinode->i_back=newinode;

iicwiuodc->i_forw->i_back-ncwiiiudc;

hinodc[inodcidj.i_forw=ncwinode;

/*5.initializetheinode*/

newinode->i_count=1;

newinode->i_flag=0;/*flagfornotupdate*/

newinode->i_ino=dinodeid;

17

returnnewinode;

iput(pinodc)/*iput()*/

structinode*pinode;

{

longaddr;

unsignedintblock_num;

inti;

if(pinode->i_connt>I)

(

pinode->i_count—;

return;

)

else

(

if(pinode->di_number!=0)

{

/*writebacktheinode*/

addr=DINODESTART+pinode->i_ino*DINODESIZ;

fseek(fd,addr,SSEK_SET);

fwiilc(&pinudc->di_iiuiiibci,DINODESIZ,l,fd);

)

else

(

/*rmtheinode&theblockofthefileinthedisk*/

block_num=pinode->di_size/BLOCKSIZ;

for(i=0;i<block_num;i++)

18

balloc(pinode->di_addr[i]);

ifree(pinode->i_ino);

/*freetheinodeinthememory*/

if(pinode->i_forw==NULL)

pinode->i_back->i_forw=NULL;

else

(

pinode->i_forw->i_back=pinode->i_back;

pinode->i_back->i_forw=pinode->i_forw;

);

free(pinode);

1;

)

8.1節(jié)點(diǎn)分配和釋放函數(shù)ialloc()和ifree()(文件名iallfree.c)

#include<stdio.h>

#include"filesys.h"

staticstructdinodcblock_buflBLOCKSIZ/DINODESIZJ;

structinode*ialloc()

(

structinode*temp_inode;

unsignedinicur_di;

19

inti,count,block_end_flag;

if(filsys.s_pinode=NICINOD)/*s_inodeempty*/

(

i=0;

count=();

block_end_flag=l;

filsys.s_pinode=NICINOD-1;

cur_di=filsys.s_rinode;

while((count<NICINOD)::(count<=filsys.s_ninode))

(

if(block_end_flag)

(

fseek(fd,DINODESTART+cur_di*DINODESIZ);

fread(block_buf.l.BLOCK,fd);

block_end_flag=0;

i-0;

)

while(block_buf[i].di_mode-DIEMPTY)

(

cur_di++;

i++;

)

if(i==NICINOD)

block_end_flag=1;

else

(

filsys.s_inode[filsys.s_pinode-]=cur_di;

count++;

20

}

filsys.s_rinode=cur_di;

)

temp_inode=igct(filsys.s_inode[filsys.s_pinodc]);

fseek(fd,DINODESTART+filsys.s_inode[filsys.s_pinodeFDINODESIZ,SEEK_SET)

fwrite(&temp_inode->di_numberj,sizeof(structdinode),fd);

filsys.s_pinode++;

filsys.s_ninode—;

filsys.s_fmod=SlJPDATE;

returntemp_inode;

)

ifree(dinodeid)/*ifree*/

unsigneddinodeid;

(

filsys.s_ninode+4-;

if(filsys.s_pinode!=NICINOD)/*notfull*/

(

filsys.s_inode[filsys.s_pinode]=dinodeid;

filsys.s_pinode++;

)

else/*full*/

(

if(dinodeid<filsys.s_rinode)

(

filsys.s_inode[NICINOD]=dinodeid;

filsys.s_rinode=dinodeid;

21

9.磁盤塊分配和釋放函數(shù)balloc()與bfree()(文件名ballfre.c)

#include<stdio.h>

#include"filsys.h"

staticunsignedintblock_buf[BLOCKSIZ];

unsignedintballoc()

(

unsignedintfree_block,free_

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論