When should I use mmap for file access?

mmap is great if you have multiple processes accessing data in a read only fashion from the same file, which is common in the kind of server systems I write. mmap allows all those processes to share the same physical memory pages, saving a lot of memory. mmap also allows the operating system to optimize … Read more

Return value range of the main function

The standard doesn’t say. 0, EXIT_SUCCESS and EXIT_FAILURE have (sort of) specified meanings. Anything else depends on the implementation. At the present time, most Unix-based systems only support 8-bit return values. Windows supports (at least) a 32-bit return value. I haven’t checked whether 64-bit Windows supports a 64-bit return value, but I rather doubt it, … Read more

What is the meaning of “POSIX”?

POSIX is a family of standards, specified by the IEEE, to clarify and make uniform the application programming interfaces (and ancillary issues, such as commandline shell utilities) provided by Unix-y operating systems. When you write your programs to rely on POSIX standards, you can be pretty sure to be able to port them easily among … Read more

What is the difference between sigaction and signal?

Use sigaction() unless you’ve got very compelling reasons not to do so. The signal() interface has antiquity (and hence availability) in its favour, and it is defined in the C standard. Nevertheless, it has a number of undesirable characteristics that sigaction() avoids – unless you use the flags explicitly added to sigaction() to allow it … Read more

How do I execute a command and get the output of the command within C++ using POSIX?

#include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, “r”), pclose); if (!pipe) { throw std::runtime_error(“popen() failed!”); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; } Pre-C++11 version: #include <iostream> #include <stdexcept> #include … Read more