《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. hdrp gpu instance MPB不生效问题

    Thanks for posting these tips. I was devastated when my project dropped to 3 FPS because material pr ...

  2. Python学习笔记组织文件之用zipfile模块压缩文件

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  3. fastapi loguru

    使用loguru记录日志 安装 pip install loguru 基本使用 那么这个库怎么来用呢?我们先用一个实例感受下: In [1]: from loguru import logger .. ...

  4. Vue后台管理项目中解决需要配置多个端口号问题

    背景 登录接口:http://39.98.123.211:8170/ 商品接口:http://39.98.123.211:8510/ 可见前面是地址是一致的,但是端口号不一样. 这就会导致,登录进得去 ...

  5. 【ZYNQ学习】如何使用ZYNQ

    本篇博客建立一套ZYNQ系统开发的一般方法和流程,并对ZYNQ的硬件和软件的设计流程进行概述 设计工具: vivado IDE:创建SoC设计中的硬件系统部分,同时和设计套件中的其他工具有交互,包含集 ...

  6. Feign组件

    一.简介 Feign是Netflix开发的声明式,模块化的HTTP客户端 1 导入依赖 <dependency> <groupId>org.springframework.cl ...

  7. -bash: ./mlnxofedinstall: /usr/bin/perl: bad interpreter: No such file or directory

    -bash: ./mlnxofedinstall: /usr/bin/perl: bad interpreter: No such file or directory 解决办法:安装相关的环境即可 输 ...

  8. zookeeper设置开机自启

    开机自启:(1)编辑zookeeper.service文件 vim /usr/lib/systemd/system/zookeeper.service 加入如下内容复制代码[Unit]Descript ...

  9. Sql Server 服务无法正常启动,126异常

    SQL Server 一记 异常信息 方法/步骤 打开[开始菜单]找到软件SQL Server下面的[配置管理器] 可以看到数据库主服务是 '停止' 状态: ![数据库主服务 展开[SQL Serve ...

  10. 模态框:JavaScript+css

    solution one: JavaScript,单个模态框展示: modal_tools.js window.onload = function () { //js默认加载页面方法 // get m ...