本章讨论了块设备 I/O和缓冲区管理;解释了块设备I/O的原理和I/O缓冲的优点;论述了Unix 的缓冲区管理算法,并指出了其不足之处;还利用信号量设计了新的缓冲区管理算法,以提高 I/O缓冲区的缓存效率和性能;表明了简单的PV算法易于实现,缓存效果好,不存在死锁和饥饿问题;还提出了一个比较 Unix 缓冲区管理算法和 PV算法性能的编程方案。编程项目还可以帮助读者更好地理解文件系统中的I/O操作。
块设备I/O缓冲区
I/O缓冲的基本原理非常简单。文件系统使用一系列I/O缓冲区作为块设备的缓存内存。当进程试图读取(dev,blk)标识的磁盘块时。它首先在缓冲区缓存中搜索分配给磁盘块的缓冲区。如果该缓冲区存在并且包含有效数据、那么它只需从缓冲区中读取数据、而无须再次从磁盘中读取数据块。如果该缓冲区不存在,它会为磁盘块分配一个缓冲区,将数据从磁盘读人缓冲区,然后从缓冲区读取数据。当某个块被读入时、该缓冲区将被保存在缓冲区缓存中,以供任意进程对同一个块的下一次读/写请求使用。同样,当进程写入磁盘块时,它首先会获取一个分配给该块的缓冲区。然后,它将数据写入缓冲区,将缓冲区标记为脏,以延迟写入,并将其释放到缓冲区缓存中。由于脏缓冲区包含有效的数据,因此可以使用它来满足对同一块的后续读/写请求,而不会引起实际磁盘I/O。脏缓冲区只有在被重新分配到不同的块时才会写人磁盘。
Unix I/O缓冲区管理算法
I/O缓冲区:内核中的一系列NBUF 缓冲区用作缓冲区缓存。每个缓冲区用一个结构体表示。
typdef struct buf[ struct buf*next__free;// freelist pointer struct buf *next__dev;// dev_list pointer int dev.,blk; // assigmed disk block;int opcode; // READ|wRITE int dirty; // buffer data modified int async; // ASYNC write flag int valid; //buffer data valid int buay; // buffer is in use int wanted; // some process needs this buffer struct semaphore lock=1; / // buffer locking semaphore; value=1 struct semaphore iodone=0;// for process to wait for I/0 completion;// block data area char buf[BLKSIZE];) } BUFFER; BUFFER buf[NBUF],*freelist;// NBUF buffers and free buffer list
设备表:每个块设备用一个设备表结构表示。
struct devtab{ u16 dev; // major device number // device buffer list BUFFER *dev_list;BUFFER*io_queue // device I/0 queue ) devtab[NDEV];
Unix算法的优点:1.数据的一致性;2.缓存效果;3.临界区;
Unix算法的缺点:1.效率低下;2.缓存效果不可预知;3.可能会出现饥饿;4.该算法使用只适用于单处理系统的休眠/唤醒操作。
BUFFER *getb1k(dev,blk): while(1){ (1). P(free); //get a free buffer first if (bp in dev_1ist){ (2). if (bp not BUSY){ remove bp from freelist;P(bp); // lock bp but does not wait (3).return bp; // bp in cache but BUSY V(free); // give up the free buffer (4).P(bp); // wait in bp queue return bp;v // bp not in cache,try to create a bp=(dev,blk) (5).bp = frist buffer taken out of freelist;P(bp); // lock bp,no wait (6).if(bp dirty){ awzite(bp); // write bp out ASYNC,no wait continue; // continue from (1) (7).reassign bp to(dev,blk);1/ mark bp data invalid,not dir return bp;- // end of while(1); brelse(BUFFER *bp), { (8).iF (bp queue has waiter)( V(bp); return; ] (9).if(bp dirty && free queue has waiter){ awrite(bp);zeturn;}(10).enter bp into(tail of) freelist;V(bp);V(free); }
include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #define N 100 #define true 1 #define producerNum 10 #define consumerNum 5 #define sleepTime 1000 typedef int semaphore; typedef int item; item buffer[N] = {0}; int in = 0; int out = 0; int proCount = 0; semaphore mutex = 1, empty = N, full = 0, proCmutex = 1; void * producer(void * a){ while(true){ while(proCmutex <= 0); proCmutex--; proCount++; printf("produce a product: ID %d, buffer location:%d\n",proCount,in); proCmutex++; while(empty <= 0){ printf("buffer is full\n"); } empty--; while(mutex <= 0); mutex--; buffer[in] = proCount; in = (in + 1) % N; mutex++; full++; sleep(sleepTime); } } void * consumer(void *b){ while(true){ while(full <= 0){ printf("buffer is empty\n"); } full--; while(mutex <= 0); mutex--; int nextc = buffer[out]; buffer[out] = 0;//消费完将缓冲区设置为0 out = (out + 1) % N; mutex++; empty++; printf("produce a product: ID %d, buffer location:%d\n", nextc,out); sleep(sleepTime); } } int main() { pthread_t threadPool[producerNum+consumerNum]; int i; for(i = 0; i < producerNum; i++){ pthread_t temp; if(pthread_create(&temp, NULL, producer, NULL) == -1){ printf("ERROR, fail to create producer%d\n", i); exit(1); } threadPool[i] = temp; }//创建生产者进程放入线程池 for(i = 0; i < consumerNum; i++){ pthread_t temp; if(pthread_create(&temp, NULL, consumer, NULL) == -1){ printf("ERROR, fail to create consumer%d\n", i); exit(1); } threadPool[i+producerNum] = temp; }//创建消费者进程放入线程池 void * result; for(i = 0; i < producerNum+consumerNum; i++){ if(pthread_join(threadPool[i], &result) == -1){ printf("fail to recollect\n"); exit(1); } }//运行线程池 return 0; }
#include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <errno.h> #define total 20 sem_t remain, apple, pear, mutex; static unsigned int vremain = 20, vapple = 0, vpear = 0; void *father(void *); void *mather(void *); void *son(void *); void *daughter(void *); void print_sem(); int main() { pthread_t fa, ma, so, da; sem_init(&remain, 0, total);//总数初始化为20 sem_init(&apple, 0, 0);//盆子中苹果数, 开始为0 sem_init(&pear, 0, 0);//盆子中梨子数, 开始为0 sem_init(&mutex, 0, 1);//互斥锁, 初始为1 pthread_create(&fa, NULL, &father, NULL); pthread_create(&ma, NULL, &mather, NULL); pthread_create(&so, NULL, &son, NULL); pthread_create(&da, NULL, &daughter, NULL); for(;;); } void *father(void *arg) { while(1) { sem_wait(&remain); sem_wait(&mutex); printf("before A: remain=%u, apple_num=%u\n", vremain--, vapple++); printf("after A: remain=%u, apple_num%u\n", vremain, vapple); sem_post(&mutex); sem_post(&apple); sleep(1); } } void *mather(void *arg) { while(1) { sem_wait(&remain); sem_wait(&mutex); printf("before B: remain=%u, pear_num=%u\n", vremain--, vpear++); printf("after B: remain=%u, pear_num%u\n", vremain, vpear); sem_post(&mutex); sem_post(&pear); sleep(2); } } void *son(void *arg) { while(1) { sem_wait(&pear); sem_wait(&mutex); printf("before C: remain=%u, pear_num=%u\n", vremain++, vpear--); printf("after C: remain=%u, pear_num=%u\n", vremain, vpear); sem_post(&mutex); sem_post(&remain); sleep(3); } } void *daughter(void *arg) { while(1) { sem_wait(&apple); sem_wait(&mutex); printf("before D remain=%u, apple_num=%u\n", vremain++, vapple--); printf("after D: remain=%u, apple_num=%u\n", vremain, vapple); sem_post(&mutex); sem_post(&remain); sleep(3); } } void print_sem() { int val1, val2, val3; sem_getvalue(&remain, &val1); sem_getvalue(&apple, &val2); sem_getvalue(&pear, &val3); printf("Semaphore: remain:%d, apple:%d, pear:%d\n", val1, val2, val3); }
问:setbuf()函数和setvbuf()函数的区别是什么?
答:setbuf()和setvbuf()函数的实际意义在于:用户打开一个文件后,可以建立自己的文件缓冲区,而不必使用fopen()函数打开文件时设定的默认缓冲区。这样就可以让用户自己来控制缓冲区,包括改变缓冲区大小、定时刷新缓冲区、改变缓冲区类型、删除流中默认的缓冲区、为不带缓冲区的流开辟缓冲区等。
https://www.jb51.net/article/71720.htm