buffer cache, fifo replacement

This commit is contained in:
rtm 2006-08-12 17:17:35 +00:00
parent 7ce01cf9be
commit 14938f9392
4 changed files with 24 additions and 5 deletions

3
Notes
View file

@ -354,3 +354,6 @@ HMM maybe the variables at the end of struct cpu are being overwritten
OH! recursive interrupts will use up any amount of cpu[].stack! OH! recursive interrupts will use up any amount of cpu[].stack!
underflow and wrecks *previous* cpu's struct underflow and wrecks *previous* cpu's struct
better buffer cache replacement
read/write of open file that's been unlinked

21
bio.c
View file

@ -20,20 +20,31 @@ struct buf *
getblk(uint dev, uint sector) getblk(uint dev, uint sector)
{ {
struct buf *b; struct buf *b;
static struct buf *scan = buf;
int i;
acquire(&buf_table_lock); acquire(&buf_table_lock);
while(1){ while(1){
for(b = buf; b < buf+NBUF; b++) for(b = buf; b < buf+NBUF; b++)
if((b->flags & B_BUSY) && b->dev == dev && b->sector) if((b->flags & (B_BUSY|B_VALID)) && b->dev == dev && b->sector == sector)
break; break;
if(b < buf+NBUF){ if(b < buf+NBUF){
if(b->flags & B_BUSY){
sleep(buf, &buf_table_lock); sleep(buf, &buf_table_lock);
} else { } else {
for(b = buf; b < buf+NBUF; b++){
if((b->flags & B_BUSY) == 0){
b->flags |= B_BUSY; b->flags |= B_BUSY;
release(&buf_table_lock);
return b;
}
} else {
for(i = 0; i < NBUF; i++){
b = scan++;
if(scan >= buf+NBUF)
scan = buf;
if((b->flags & B_BUSY) == 0){
b->flags = B_BUSY;
b->dev = dev; b->dev = dev;
b->sector = sector; b->sector = sector;
release(&buf_table_lock); release(&buf_table_lock);
@ -53,11 +64,14 @@ bread(uint dev, uint sector)
extern struct spinlock ide_lock; extern struct spinlock ide_lock;
b = getblk(dev, sector); b = getblk(dev, sector);
if(b->flags & B_VALID)
return b;
acquire(&ide_lock); acquire(&ide_lock);
c = ide_start_rw(dev & 0xff, sector, b->data, 1, 1); c = ide_start_rw(dev & 0xff, sector, b->data, 1, 1);
sleep (c, &ide_lock); sleep (c, &ide_lock);
ide_finish(c); ide_finish(c);
b->flags |= B_VALID;
release(&ide_lock); release(&ide_lock);
return b; return b;
@ -73,6 +87,7 @@ bwrite(uint dev, struct buf *b, uint sector)
c = ide_start_rw(dev & 0xff, sector, b->data, 1, 0); c = ide_start_rw(dev & 0xff, sector, b->data, 1, 0);
sleep (c, &ide_lock); sleep (c, &ide_lock);
ide_finish(c); ide_finish(c);
b->flags |= B_VALID;
release(&ide_lock); release(&ide_lock);
} }

1
buf.h
View file

@ -5,3 +5,4 @@ struct buf {
uchar data[512]; uchar data[512];
}; };
#define B_BUSY 0x1 #define B_BUSY 0x1
#define B_VALID 0x2

View file

@ -339,7 +339,7 @@ main(int argc, char *argv[])
{ {
puts("usertests starting\n"); puts("usertests starting\n");
unlinkread(); //unlinkread();
createdelete(); createdelete();
twofiles(); twofiles();
sharedfd(); sharedfd();