简体   繁体   中英

System in C++ Wall Clock Time

I am trying to run a bash nvme flush command from a C++ program with a timer having been started. This is to allow an nvme driver to complete all writing to drive before taking the second timestamp and performing a time calculation. My question is whether the bash instructions specified in a C++ system() command will execute before the next line of the C++ program.

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

Yes, system waits for the command to exit.

Otherwise it could not give you the program's return value as it does on some systems. On POSIX systems you can use WEXITSTATUS to retrieve this, so the command must have finished.

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

If your machine has man and the appropriate documentation installed, you can use man 3 system to find out how system behaves on your machine:

For example here it says that "system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed . "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM