《Unix/Linux系统编程》第十周学习笔记

块设备I/O和缓冲区管理

解释块设备I/O的原理和I/O缓冲的优点

I/O缓冲区:内核中的一系列NBUF缓冲区用作缓冲区缓存。每个缓冲区用一个结构体表示
typdef struct buf{
struct buf *next_free; //freelist pointer
struct buf *next_dev; //dev_list pointer
int dev,blk; //assigned disk block;
int opcode; //READ|WRITE
int dirty; //buffer data modified
int async; //ASYNC write flag
int valid; //buffer data valid
int busy; //buffer is in use
int wanted; //some process needs this buffer
struct, semaphore lock=l ; //buffer locking semaphore; value=L
struct semaphore iodone=0; //for process to wait for I/O completion;
char buf[BLKSIZE]; //block data area
} BUFFER; BUFFER buf[NBUF], *freelist; // NBUF buffers and free buffer list

介绍Unix的缓冲区管理算法

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

利用信号量设计新的缓冲区管理算法,以提高I/O缓冲区的缓存效率和性能

信号量的主要优点是:
  (1)计数信号量可用来表示可用资源的数量,例如:空闲缓冲区的数量。
  (2)当多个进程等待一个资源时,信号量上的V操作只会释放一个等待进程,该进程不必重试,因为它保证拥有资源。
使用信号量的缓冲区管理算法
  1.保证数据一致性;
  2.良好的缓存效果;
  3.高效率:没有重试循环,没有不必要的进程“唤醒”
  4.无死锁和饥饿。

介绍简单的PV算法及其特点

PV算法

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;
}

《Unix/Linux系统编程》第十周学习笔记的更多相关文章

  1. 《Linux内核分析》第一周学习笔记

    <Linux内核分析>第一周学习笔记 计算机是如何工作的 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/c ...

  2. 《Linux内核分析》第二周学习笔记

    <Linux内核分析>第二周学习笔记 操作系统是如何工作的 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/ ...

  3. linux内核分析第五周学习笔记

    linux内核分析第五周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...

  4. Linux内核分析第三周学习笔记

    linux内核分析第三周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...

  5. Linux内核分析第六周学习笔记——分析Linux内核创建一个新进程的过程

    Linux内核分析第六周学习笔记--分析Linux内核创建一个新进程的过程 zl + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...

  6. Linux 内核分析第八周学习笔记

    Linux 内核分析第八周学习笔记 zl + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...

  7. Linux内核分析第七周学习笔记——Linux内核如何装载和启动一个可执行程序

    Linux内核分析第七周学习笔记--Linux内核如何装载和启动一个可执行程序 zl + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study. ...

  8. linux内核分析第六周学习笔记

    LINUX内核分析第六周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...

  9. 20135320赵瀚青LINUX内核分析第三周学习笔记

    赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 本周是学习的主要是构造 ...

  10. Linux内核分析——第八周学习笔记20135308

    第八周 进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与进程调度的时机分析 (1)进程分类 第一种分类 I/O-bound:等待I/O CPU-bound: ...

随机推荐

  1. chrome浏览器通知与语音播放

    HTML5 Web Notification 语法 https://developer.mozilla.org/zh-CN/docs/Web/API/notification 如果浏览器支持Web N ...

  2. python_列表和元组的转换

    1, 通过list函数将元组的数据获取到,保存到新定义的列表里面.备注:元组的数据不会更改. info_tuple = ("小明", 24, 1.75) info_list = l ...

  3. pytorch学习笔记(6)--神经网络非线性激活

    如果神经元的输出是输入的线性函数,而线性函数之间的嵌套任然会得到线性函数.如果不加非线性函数处理,那么最终得到的仍然是线性函数.所以需要在神经网络中引入非线性激活函数. 常见的非线性激活函数主要包括S ...

  4. Linux基础第十一章:日志文件及如何使用rsyslog搭建小型日志服务器

    一.日志文件 1.日志作用 2.常用日志 3.日志级别 二.Rsyslog日志处理系统 1.使用Rsyslog创建日志有点 2.Rsyslog配置文件解析 3.使用rsyslog将ssh服务的日志单独 ...

  5. Kubernetes--Ingress资源

    Ingress资源 Kubernetes提供了两种内建的云端负载均衡机制(cloud load balancing)用于发布公共应用,一种是工作于传输层的Service资源,它实现的是"TC ...

  6. Spring入门之IoC 的概念和作用(02)

    2.1 程序的耦合和解耦 2.1.1 程序的耦合耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的 ...

  7. JLink OB SWI 取代串口打印的方式

    1:debug的串口被占用 2:从Keil 迁移到的LINUX下开发. 3:手上只有JLinkOB,(4线:CLK,SWIO,GND,RST) 4:设备只引出了4线(SWO 没接出) 环境: JLin ...

  8. yii 自定义form样式适应现成模板

    需求:想使用下边的样式但是使用yii中的表单样式会 <?= $form->field($model, 'attribute_code')->textInput()->label ...

  9. 1903021126 申文骏 Java 第三周作业 编写代码及运行

    项目 内容 课程班级博客链接 19级信计班(本) 作业要求链接 第三周作业要求 博客名称 1903021126 申文骏 Java 第三周作业 编写代码及运行 要求 每道题要有题目,代码(使用插入代码, ...

  10. 04 ajax执行php并传递参数

    做这个事情之前要导入jQuery js的方式 _this.value1 = "abc"; _this.value2 = 1; $.ajax({ url: 'xxxxxx.php', ...