【问题标题】:System in C++ Wall Clock TimeC++ 挂钟时间系统
【发布时间】:2017-11-27 15:41:01
【问题描述】:

我正在尝试从已启动计时器的 C++ 程序运行 bash nvme flush 命令。这是为了让 nvme 驱动程序在获取第二个时间戳并执行时间计算之前完成对驱动器的所有写入。我的问题是 C++ system() 命令中指定的 bash 指令是否会在 C++ 程序的下一行之前执行。

fd = open (fname.c_str (), O_WRONLY | O_CREAT, 0660);

// Verify the file has been opened.
if (fd == -1)
{
    cout << get_datetime_string() << "Write open of " << fname 
    << " failed.  Errno: " << errno << endl;
}
else
{
    // Total bytes written.
    uint64_t written = 0;

    // Notify the start of the test.
    cout << get_datetime_string() << "Write test started" << endl;

    // Elapsed time.
    struct timeval tv = { 0 };
    get_elapsed_time (&tv);
    struct timeval write_tv = tv;

    // Run until it is time for the test to stop.
    while (written < READ_LIMIT && zero_writes < 10)
    {
        ssize_t writesize = write (fd, &buf[0], blocksize);
        if (writesize == -1)
        {
            cout << get_datetime_string << "Write failure.  Errno: " << errno << endl;
            zero_writes = 10;
        }
        else if (0 == writesize)
        {
            cout << get_datetime_string() << "Zero bytes written" << endl;
            zero_writes++;
        }
        else
        {
            written += writesize;
        }
    }

//
// ISSUE THE NVME FLUSH COMMAND HERE
// Something like system("nvme flush...");
//


// Get the elapsed time.
get_elapsed_time (&write_tv);

【问题讨论】:

    标签: c++ bash system


    【解决方案1】:

    是的,system 等待命令退出。

    否则它无法像在某些系统上那样为您提供程序的返回值。在 POSIX 系统上,您可以使用 WEXITSTATUS 来检索它,因此该命令必须已经完成。

    http://en.cppreference.com/w/cpp/utility/program/system

    如果您的机器安装了man 并安装了相应的文档,您可以使用man 3 system 了解系统在您机器上的行为:

    例如here它表示“system()通过调用/bin/sh -c命令执行command中指定的命令,在命令完成后返回。” p>

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2015-10-11
      • 1970-01-01
      • 2019-08-10
      • 2016-04-20
      相关资源
      最近更新 更多