c++ - 读取/sys/block/$DEVICE/stat 会导致竞争条件吗?

标签 c++ linux race-condition

每一秒钟,我的程序都会读取/sys/block/$DEVICE/stat。

减去以前保存的值,然后保存当前值。所以我知道硬盘事件。 但有时我的值太大(在上一个和当前之间)(5GB/秒)。

  1. 它可以是竞争条件吗(如果内核正在写入文件 当应用程序同时读取它时)?
  2. 是否存在避免这种情况的标准解决方案?

    const int HDD_READ_POS     = 2;
    const int HDD_WRITE_POS    = 6;
    const int UNIX_SECTOR_SIZE = 512;
    
    std::tuple<uint64_t, uint64_t> hddStatus(const std::string &name)
    {
        std::ifstream in("/sys/block/"+name+"/stat");
    
        auto readVal_ = static_cast<uint64_t>(0);
        auto writeVal_= static_cast<uint64_t>(0);
    
        if ( ! in.is_open() ) {
            return std::tuple<uint64_t, uint64_t> (readVal_, writeVal_);
        }
    
        std::string line;
        std::regex rgx ( "\\d+" );
        std::regex_token_iterator<std::string::iterator> end;
    
        while (std::getline(in, line) ){
    
            std::regex_token_iterator<std::string::iterator> iter( line.begin(), line.end(), rgx, 0 );
            int pos_ = 0 ;
    
            while ( iter != end ) {
    
                if ( pos_ == HDD_READ_POS){
                    readVal_ = std::stoul( *iter ) ;
                }
    
                if ( pos_ == HDD_WRITE_POS){
                    writeVal_ = std::stoul( *iter ) ;
                }
    
                ++iter;
                ++pos_;
            }
        }
    
        return std::tuple<uint64_t, uint64_t> (readVal_, writeVal_);
    
    }
    

最佳答案

完整列表

#include <iostream>
#include <fstream>
#include <regex>
#include <tuple>
#include <chrono>
#include <thread>
#include <cstdint>

const int HDD_READ_POS     = 2;
const int HDD_WRITE_POS    = 6;
const int UNIX_SECTOR_SIZE = 512;
uint64_t prevRead  = static_cast<uint64_t>(0);
uint64_t prevWrite = static_cast<uint64_t>(0);

std::tuple<uint64_t, uint64_t> hddStatus(const std::string &name="sda"){
  std::ifstream in("/sys/block/"+name+"/stat");

auto readVal_ = static_cast<uint64_t>(0);
auto writeVal_= static_cast<uint64_t>(0);

if ( ! in.is_open() ) {
    return std::tuple<uint64_t, uint64_t> (readVal_, writeVal_);
}

std::string line;
std::regex rgx ( "\\d+" );
std::regex_token_iterator<std::string::iterator> end;

while (std::getline(in, line) ){

    std::regex_token_iterator<std::string::iterator> iter( line.begin(), line.end(), rgx, 0 );
    int pos_ = 0 ;

    while ( iter != end ) {

        if ( pos_ == HDD_READ_POS){
            readVal_ = std::stoul( *iter ) ;
        }

        if ( pos_ == HDD_WRITE_POS){
            writeVal_ = std::stoul( *iter ) ;
        }

        ++iter;
        ++pos_;
    }
}

return std::tuple<uint64_t, uint64_t> (readVal_, writeVal_);

}


void init()
{

    auto values = hddStatus();
    prevRead  = std::get<0>( values ) * UNIX_SECTOR_SIZE;
    prevWrite = std::get<1>( values ) * UNIX_SECTOR_SIZE;
}

int main(int argc, char const *argv[])
{
init();


while(true){

std::ofstream stat("statistics.txt", std::fstream::out | std::fstream::app);
if ( stat.is_open() ){    

        auto values = hddStatus();
        auto read  = std::get<0>( values ) * UNIX_SECTOR_SIZE;
        auto write = std::get<1>( values ) * UNIX_SECTOR_SIZE;

         // stat<<"Current Read: "<< read<<" Write: "<<write<<'\n';
        if (read > prevRead){
            stat<<"Diff Read: "<< read - prevRead <<'\n';
            std::cout<<"Diff Read: "<< read - prevRead <<'\n';
        }

        if ( write > prevWrite){
            stat<<"Diff Write: "<<write - prevWrite <<'\n';
            std::cout<<"Diff Write: "<<write - prevWrite <<'\n';
        }

        prevRead  = read;
        prevWrite = write;

        std::this_thread::sleep_for(std::chrono::milliseconds(1));

    }
}

return 0;

}

关于c++ - 读取/sys/block/$DEVICE/stat 会导致竞争条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45243040/

相关文章:

c++ - 从 Gradle 迁移到 CMake for C++ 项目

linux - 如何将 find 的输出用于 if 条件?

javascript - javascript 中的 promise 会干扰其余代码

c++ - C++中类的静态数据成员和静态方法是静态对象吗?

c++ - 使用 C++ 的频谱图

c++ - 实现通用树

mysql - Debian 安装 MySQL 特定版本不可用

linux - bash——复制和更改文件名

javascript - Ajax 调用的 Angular 1 服务的竞争条件

mysql - 事务是否停止了 MySQL 中的所有竞争条件问题?