From 14938f9392524ad1b198bf36e63d42e0e61dab85 Mon Sep 17 00:00:00 2001 From: rtm Date: Sat, 12 Aug 2006 17:17:35 +0000 Subject: [PATCH] buffer cache, fifo replacement --- Notes | 3 +++ bio.c | 23 +++++++++++++++++++---- buf.h | 1 + usertests.c | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Notes b/Notes index 3efafb3..e1d763d 100644 --- a/Notes +++ b/Notes @@ -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! underflow and wrecks *previous* cpu's struct + +better buffer cache replacement +read/write of open file that's been unlinked diff --git a/bio.c b/bio.c index 7184a28..2db9694 100644 --- a/bio.c +++ b/bio.c @@ -20,20 +20,31 @@ struct buf * getblk(uint dev, uint sector) { struct buf *b; + static struct buf *scan = buf; + int i; acquire(&buf_table_lock); while(1){ 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; if(b < buf+NBUF){ - sleep(buf, &buf_table_lock); + if(b->flags & B_BUSY){ + sleep(buf, &buf_table_lock); + } else { + b->flags |= B_BUSY; + release(&buf_table_lock); + return b; + } } else { - for(b = buf; b < buf+NBUF; b++){ + 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->flags = B_BUSY; b->dev = dev; b->sector = sector; release(&buf_table_lock); @@ -53,11 +64,14 @@ bread(uint dev, uint sector) extern struct spinlock ide_lock; b = getblk(dev, sector); + if(b->flags & B_VALID) + return b; acquire(&ide_lock); c = ide_start_rw(dev & 0xff, sector, b->data, 1, 1); sleep (c, &ide_lock); ide_finish(c); + b->flags |= B_VALID; release(&ide_lock); 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); sleep (c, &ide_lock); ide_finish(c); + b->flags |= B_VALID; release(&ide_lock); } diff --git a/buf.h b/buf.h index e3f6b14..7ab352a 100644 --- a/buf.h +++ b/buf.h @@ -5,3 +5,4 @@ struct buf { uchar data[512]; }; #define B_BUSY 0x1 +#define B_VALID 0x2 diff --git a/usertests.c b/usertests.c index a3bb284..9bd144c 100644 --- a/usertests.c +++ b/usertests.c @@ -339,7 +339,7 @@ main(int argc, char *argv[]) { puts("usertests starting\n"); - unlinkread(); + //unlinkread(); createdelete(); twofiles(); sharedfd();