我有一些代码使用 fork、execlp 和 wait 来创建两个进程。目标是能够重复打印提示并让用户输入命令,该命令最多包含 4 个参数/选项。
int main()
{
string command, argument;
istringstream iss(argument);
do
{
//prompt user for command to run
cout << "Enter command: ";
cin >> command >> argument;
int pid, rs, status;
//fork will make 2 processes
pid = fork();
if(pid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if(pid == 0) {
//Child process: exec to command with argument
//C_str to get the character string of a string
rs = execlp(command.c_str(), command.c_str(), argument.c_str(), (char*) NULL);
. …Run Code Online (Sandbox Code Playgroud) 我想使用自定义分配器从空闲列表中为std::basic_ostringstream. 这是我要使用的自定义分配器:
template <class Tp>
struct NAlloc {
typedef Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
Tp* allocate(std::size_t n) {
n *= sizeof(Tp);
memoryPool *memPool = memoryPool::GetInstance(10);//get memory pool instance
std::cout << "allocating " << n << " bytes\n";
return static_cast<Tp*>(memPool->allocate(n)); //get memory from pool
}
void deallocate(Tp* p, std::size_t n) {
std::cout << …Run Code Online (Sandbox Code Playgroud)