synchdisk.cc
来自「操作系统课程设计。在UNIX平台下实现Solary操作系统的一些功能」· CC 代码 · 共 160 行
CC
160 行
///////////////////////////////////////////////////////////////FileName : synchdisk.cc////Creator : Fang Wenbin(0410706)//CreateTime : 2006-12-26////File Desc:// implement synch access disk functions // /////////////////////////////////////////////////////////////#include "copyright.h"#include "synchdisk.h"//----------------------------------------------------------------------// DiskRequestDone// Disk interrupt handler. Need this to be a C routine, because // C++ can't handle pointers to member functions.//----------------------------------------------------------------------static voidDiskRequestDone (int arg){ SynchDisk* disk = (SynchDisk *)arg; disk->RequestDone();}//----------------------------------------------------------------------// SynchDisk::SynchDisk// Initialize the synchronous interface to the physical disk, in turn// initializing the physical disk.//// "name" -- UNIX file name to be used as storage for the disk data// (usually, "DISK")//----------------------------------------------------------------------SynchDisk::SynchDisk(char* name){ semaphore = new Semaphore("synch disk", 0); s_semOfData = new Semaphore("synch file data", 1); s_semOfReaderCount = new Semaphore("synch rc", 1); s_rc = 0; lock = new Lock("synch disk lock"); disk = new Disk(name, DiskRequestDone, (int) this);}//----------------------------------------------------------------------// SynchDisk::~SynchDisk// De-allocate data structures needed for the synchronous disk// abstraction.//----------------------------------------------------------------------SynchDisk::~SynchDisk(){ delete disk; delete lock; delete semaphore; delete s_semOfData; delete s_semOfReaderCount;}//////////////////////////////////////////////////////// Function name : SynchDisk::ReadSector// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:49:54//// Function Desc : // The steps of SynchDisk::ReadSector// reader and writer program // // Fails if://// Note :// Return type : void // Argument : int sectorNumber// Argument : char* data//////////////////////////////////////////////////////
void SynchDisk::ReadSector(int sectorNumber, char* data){ //lock->Acquire(); // only one disk I/O at a time s_semOfReaderCount->P(); s_rc++; if (s_rc == 1) s_semOfData->P(); s_semOfReaderCount->V(); disk->ReadRequest(sectorNumber, data); s_semOfReaderCount->P(); s_rc--; if (s_rc == 0) s_semOfData->V(); s_semOfReaderCount->V(); semaphore->P(); // wait for interrupt //lock->Release();}//////////////////////////////////////////////////////// Function name : SynchDisk::WriteSector// Creator : Fang Wenbin(0410706)// CreateTime : 2007-1-9 21:50:01//// Function Desc : // The steps of SynchDisk::WriteSector// 1.reader and writer program//// Fails if:// // Note :// Return type : void // Argument : int sectorNumber// Argument : char* data//////////////////////////////////////////////////////void SynchDisk::WriteSector(int sectorNumber, char* data){ // lock->Acquire(); // only one disk I/O at a time s_semOfData->P(); disk->WriteRequest(sectorNumber, data); s_semOfData->V(); semaphore->P(); // wait for interrupt // lock->Release();}void SynchDisk::s_ReadDataInSector(int startSector, int offset, int total, char *data){ ASSERT((offset+total)<=128); char buf[128]; char *tmp; ReadSector(startSector, buf); tmp = buf; tmp += offset; memcpy(data, tmp, total);}void SynchDisk::s_WriteDataInSector(int startSector, int offset, int total, char *data){ ASSERT((offset+total)<=128); char buf[128]; char *tmp; ReadSector(startSector, buf); tmp = buf; tmp += offset; memcpy(tmp, data, total); WriteSector(startSector, buf);}//----------------------------------------------------------------------// SynchDisk::RequestDone// Disk interrupt handler. Wake up any thread waiting for the disk// request to finish.//----------------------------------------------------------------------voidSynchDisk::RequestDone(){ semaphore->V();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?