I'm having problem with this piece of code, valgrind detects memory leaks in std::basic_string and I don't know what am I doing wrong. The leak is only when std::string is used in child process. Could you please tell me, where the problem is? I've never used fork() in C++ before so I don't have much exprience.
#include <iostream>
#include <string>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
pid_t childPid = fork();
if (childPid == -1)
{
perror("fork");
return EXIT_FAILURE;
}
else if (childPid == 0)
{
std::cout << "Child PID: " << getpid() << std::endl;
std::string str("something"); //valgrind detects mem leak here
_Exit(EXIT_SUCCESS);
}
else
{
//std::string str("something"); //but not here
}
waitpid(-1, 0, 0);
return EXIT_SUCCESS;
}
_Exit will not run any destructors or atexit functions, it simply ends immediately.
Obviously this punches a giant hole through RAII, so Don't Do That.
One way of implementing Don't Do That while maintaining the same exit strategy might be:
template <typename Func, typename... Args>
int wrap_cpp_code(Func&& func, Args&&.. args)
{
try
{
return std::forward<Func>(func)(std::forward<Args>(args)...);
}
catch (...)
{
return EXIT_FAILURE;
}
}
Which won't cough up an return value until all destructors under its scope are run, giving:
int child_main(int argc, char *argv[])
{
std::cout << "Child PID: " << getpid() << std::endl;
std::string str("something");
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
pid_t childPid = fork();
if (childPid == -1)
{
perror("fork");
return EXIT_FAILURE;
}
else if (childPid == 0)
{
int ret = wrap_cpp_code(child_main, argc, argv);
_Exit(ret);
}
else
{
/*
int ret = wrap_cpp_code(xyz, argc, argv);
*/
}
waitpid(-1, 0, 0);
return EXIT_SUCCESS;
}
But this still fails to account for atexit functions or global destructors. So still Avoid Doing That.
Related
I am stuck with a problem for my assignment. I am trying to execute 3 concurrent processes (in C++) out of which 2 of them are Python programs and one of them is C++ program.
My C++ program (sample.cpp):
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <signal.h>
using namespace std;
int main()
{
while (true)
{
cout << "lol" << endl;
sleep(2);
}
return 0;
}
My Python program 1 (sample.py):
import sys
while True:
line = sys.stdin.readline().strip()
print "Python says: " + str(line)
My Python program 2 (sample2.py):
import sys
while True:
line = sys.stdin.readline().strip()
print "Python 2 says: " + str(line)
Here is my driver C++ program which forks processes:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <signal.h>
using namespace std;
int main()
{
vector<pid_t> kids;
int fd[2];
if (pipe(fd) < 0)
{
cout << "Error";
return 1;
}
int fd2[2];
if (pipe(fd2) < 0)
{
cout << "Error";
return 1;
}
pid_t pid;
pid = fork();
if (pid == 0)
{
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
while (true)
{
execvp("./sample", NULL);
}
}
else
{
kids.push_back(pid);
pid = fork();
if (pid == 0)
{
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
dup2(fd2[1], STDOUT_FILENO);
close(fd2[1]);
close(fd2[0]);
char * python = "/usr/bin/python";
char * pythonProgram = "./sample.py";
char * pythonArgs[] = {python, pythonProgram, NULL, NULL};
execvp(python, pythonArgs);
}
else
{
kids.push_back(pid);
pid = fork();
if (pid == 0)
{
dup2(fd2[0], STDIN_FILENO);
close(fd2[0]);
close(fd2[1]);
char * python = "/usr/bin/python";
char * pythonProgram = "./sample2.py";
char * pythonArgs[] = {python, pythonProgram, NULL, NULL};
execvp(python, pythonArgs);
}
else
{
kids.push_back(pid);
}
}
}
close(fd[0]);
close(fd[1]);
close(fd2[0]);
close(fd2[1]);
for (pid_t k : kids)
{
int status;
//kill (k, SIGTERM);
waitpid(k, &status, 0);
}
}
When I run this program, I am expected to see "Python 2 says: Python says: lol". However, I see nothing (complete blank)... it just hangs. What am I doing wrong? I tried looking up a lot of things but no luck.
The while loop around the start of ./sample is pointless unless you expect execvp to fail. A successful call to exec* will never return. The actual call to execvp is wrong too:
execvp("./sample", NULL);
the second argument should be a char *const[].
You should add error handling for execvp:s (like a line with std::exit(1)). Otherwise if execvp fails, you'll have child processes running in the main flow of the program.
The python programs needs to be run unbuffered or else it will take a long time for the messages to appear. You should also check if the readline succeeded.
sample.py
import sys
while True:
line = sys.stdin.readline().strip()
if not line: break
print "Python says: " + str(line)
sample2.py
import sys
while True:
line = sys.stdin.readline().strip()
if not line: break
print "Python 2 says: " + str(line)
driver.cpp
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main()
{
vector<pid_t> kids;
int fd[2];
if (pipe(fd)==-1)
{
clog << "Error\n";
return 1;
}
int fd2[2];
if (pipe(fd2)==-1)
{
clog << "Error\n";
return 1;
}
pid_t pid;
pid = fork();
if (pid == 0)
{
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
char* const args[] = { NULL };
execvp("./sample", args);
std::clog << "sample failed\n";
std::exit(1);
}
else
{
kids.push_back(pid);
pid = fork();
if (pid == 0)
{
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
dup2(fd2[1], STDOUT_FILENO);
close(fd2[1]);
close(fd2[0]);
char const* python = "/usr/bin/python";
char const* pythonProgram = "./sample.py";
char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
execvp(python, const_cast<char* const*>(pythonArgs));
std::clog << "sample.py failed\n";
std::exit(1);
}
else
{
kids.push_back(pid);
pid = fork();
if (pid == 0)
{
dup2(fd2[0], STDIN_FILENO);
close(fd2[0]);
close(fd2[1]);
char const* python = "/usr/bin/python";
char const* pythonProgram = "./sample2.py";
char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
execvp(python, const_cast<char* const*>(pythonArgs));
std::clog << "sample2.py failed\n";
std::exit(1);
}
else
{
kids.push_back(pid);
}
}
}
close(fd[0]);
close(fd[1]);
close(fd2[0]);
close(fd2[1]);
for (pid_t k : kids)
{
int status;
//kill (k, SIGTERM);
waitpid(k, &status, 0);
}
}
I am trying to communicate with forked child process using message queue from boost interprocess library. When child process calls receive it causes exception with message
boost::interprocess_exception::library_error
I am using GCC 6.3 on Debian 9 x64.
#include <iostream>
#include <unistd.h>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <memory>
int main(int argc, char* argv[])
{
using namespace boost::interprocess;
const char* name = "foo-552b8ae9-6037-4b77-aa0d-d4dc9dad790b";
const int max_num_msg = 100;
const int max_msg_size = 32;
bool is_child = false;
message_queue::remove(name);
auto mq = std::make_unique<message_queue>(create_only, name, max_num_msg, max_msg_size);
auto child_pid = fork();
if (child_pid == -1)
{
std::cout << "fork failed" << std::endl;
return -1;
}
else if (child_pid == 0)
{
is_child = true;
}
if (is_child)
{
// does child needs to reopen it?
mq.reset( new message_queue(open_only, name) );
}
int send_num = 0;
while(true)
{
unsigned int priority = 0;
if (is_child)
{
message_queue::size_type bytes = 0;
try
{
int num;
// Always throws. What is wrong ???????
mq->receive(&num, sizeof(num), bytes, priority);
std::cout << num << std::endl;
}
catch(const std::exception& e)
{
std::cout << "Receive caused execption " << e.what() << std::endl;
}
sleep(1);
}
else
{
mq->send(&send_num, sizeof(send_num), priority);
send_num++;
sleep(5);
}
}
return 0;
}
Also, in child process is it required to reopen the message queue created by the parent process? I tried it both ways and neither worked. I am getting the same exception on receive.
The problem is that your receive buffer is smaller than max_msg_size. Assuming 4-byte integers, this should work:
int num[8];
mq.receive(num, sizeof(num), bytes, priority);
std::cout << *num << std::endl;
Also, I see no reason to play fast and loose with the actual queue instance. Just create it per process:
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <iostream>
#include <memory>
#include <unistd.h>
int main() {
namespace bip = boost::interprocess;
const char *name = "foo-552b8ae9-6037-4b77-aa0d-d4dc9dad790b";
{
const int max_num_msg = 100;
const int max_msg_size = 32;
bip::message_queue::remove(name);
bip::message_queue mq(bip::create_only, name, max_num_msg, max_msg_size);
}
auto child_pid = fork();
if (child_pid == -1) {
std::cout << "fork failed" << std::endl;
return -1;
}
bip::message_queue mq(bip::open_only, name);
if (bool const is_child = (child_pid == 0)) {
while (true) {
unsigned int priority = 0;
bip::message_queue::size_type bytes = 0;
try {
int num[8];
mq.receive(num, sizeof(num), bytes, priority);
std::cout << *num << std::endl;
} catch (const bip::interprocess_exception &e) {
std::cout << "Receive caused execption " << boost::diagnostic_information(e, true) << std::endl;
}
sleep(1);
}
} else {
// parent
int send_num = 0;
while (true) {
unsigned int priority = 0;
mq.send(&send_num, sizeof(send_num), priority);
send_num++;
sleep(5);
}
}
}
I have learned recently about processes and threads, and I am building a project where I need to run a program inside a child process.
The problem is that after the program in the child process exits I don't know how to restart and run the program again.
Here's a test which demonstrates what I am trying to do.
main.cpp file:
#include <iostream>
#include <unistd.h>
#include <sys/ptrace.h>
#include "handler.h"
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "usage: program name" << std::endl;
return 1;
}
pid_t pid = fork();
if (pid == 0)
{
if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) < 0)
std::cerr << "ptrace error" << std::endl;
else
execl(argv[1], argv[1], nullptr);
}
else if (pid >= 1)
{
Handler handler(std::string(argv[1]), pid);
handler.run();
}
}
handler.h file:
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <sys/ptrace.h>
#include <sys/wait.h>
class Handler
{
private:
pid_t m_pid;
std::string m_name;
public:
Handler(const std::string& name, pid_t pid) : m_name(name), m_pid(pid)
{
}
void run()
{
std::string line;
while (true)
{
std::cout << "(command) ";
std::getline(std::cin, line);
if (line == "run")
{
int options = 0, status = 0;
ptrace(PTRACE_CONT, m_pid, nullptr, nullptr);
int pid = waitpid(m_pid, &status, options);
// trying to restart again the program
if (pid < 0)
{
m_pid = fork();
execl(m_name.c_str(), m_name.c_str(), nullptr);
}
}
}
}
};
I'm looking to invoke g++ and get the output. Here's my code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <boost/optional.hpp>
#include <vector>
#include <string>
namespace Util
{
template<typename T>
using optional = boost::optional<T>;
}
namespace Wide
{
namespace Driver
{
struct ProcessResult
{
std::string std_out;
int exitcode;
};
ProcessResult StartAndWaitForProcess(std::string name, std::vector<std::string> args, Util::optional<unsigned> timeout);
}
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <fcntl.h>
Wide::Driver::ProcessResult Wide::Driver::StartAndWaitForProcess(std::string name, std::vector<std::string> args, Util::optional<unsigned> timeout) {
int filedes[2];
pipe(filedes);
pid_t pid = fork();
if (pid == 0) {
while ((dup2(filedes[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
auto fd = open("/dev/null", O_RDWR);
while ((dup2(fd, STDIN_FILENO) == -1) && (errno == EINTR)) {}
//freopen("/dev/null", "rw", stdin);
//freopen("/dev/null", "rw", stderr);
//close(filedes[1]);
close(filedes[0]);
std::vector<const char*> cargs;
cargs.push_back(name.c_str());
for (auto&& arg : args)
cargs.push_back(arg.c_str());
cargs.push_back(nullptr);
execv(name.c_str(), const_cast<char* const*>(&cargs[0]));
}
std::string std_out;
close(filedes[1]);
char buffer[4096];
while (1) {
ssize_t count = read(filedes[0], buffer, sizeof(buffer));
if (count == -1) {
if (errno == EINTR) {
continue;
} else {
perror("read");
exit(1);
}
} else if (count == 0) {
break;
} else {
std_out += std::string(buffer, buffer + count);
}
}
close(filedes[0]);
int status;
ProcessResult result;
result.std_out = std_out;
waitpid(pid, &status, 0);
if (!WIFEXITED(status))
result.exitcode = 1;
else {
result.exitcode = WEXITSTATUS(status);
if (result.exitcode != 0) {
std::cout << name << " failed with code " << result.exitcode << "\n";
}
}
return result;
}
int main()
{
auto r = Wide::Driver::StartAndWaitForProcess("g++", { "-std=c++14", "main.cpp" }, 150);
std::cout << r.std_out << "!!!!\n!!!!\n" << r.exitcode << "\n";
}
The output:
read: Bad file descriptor
g++ failed with code 1
!!!!
!!!!
1
Just invoke g++ main.cpp -std=c++14 && ./a.out.
I've used strace but it doesn't really give any more interesting details- the process runs, then fork/exec, then the above error. I can invoke other processes with the above code so I don't know what's so different about g++. I can invoke GCC with popen without problems so I don't know what's so different here.
The error here is really not very helpful. How can I invoke g++ and get the output?
The problem here is that you call execv which requires a full path to the executable as its first argument.
What you need is execvp which uses the contents of the PATH environment variable to find the executable, and thus only requires a name like g++.
For my school project I must make a C++ program. The program has to use already installed applications on the Linux OS. To get familiar with managing other processes from within a C++ application I want to make a program that sets a wlan interface to monitor mode. The code that I have written so far is pretty long and doesn't seems efficient. Are there any ways to make my code more compact and efficient? At the end of the program I want to execute iwconfig to check if the wlan really is in monitor mode. What is the best way to do that?
#include <unistd.h>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[])
{
string wlan = "wlan1";
pid_t ifconfigDown;
ifconfigDown = fork();
if(ifconfigDown == 0)//ifconfig
{
execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "down",(char*)0 );
}
else //parent
{
usleep(500000);
pid_t iwconfigMode;
iwconfigMode = fork();
if(iwconfigMode == 0)//ifconfig
{
execl("/sbin/iwconfig","iwconfig",wlan.c_str(),"mode","monitor",(char*)0 );
}
else//parent
{
usleep(500000);
pid_t ifconfigUp;
ifconfigUp = fork();
if(ifconfigUp == 0)//ifconfig
{
execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "up", (char*)0 );
}
else//parent
{
usleep(500000);
pid_t iwconfig;
iwconfig = fork();
if(iwconfig == 0)//iwconfig
{
execl("/sbin/iwconfig", "iwconfig", (char*)0 );
//check if wlan1 is in monitor mode
}
}
}
waitpid(-1, NULL, 0);
return 0;
}
}
string rPopenEnd (string cmd)
{
FILE *fp = popen(cmd.c_str(), "r");
if (fp == NULL)
{
return "ERROR";
}
else
{
uint16_t line_size = 20;
char line[line_size];
string result;
while (fgets(line, line_size, fp))
result += line;
wait(NULL);
return result;
}
}
int main(int argc, char *argv[])
{
rPopenEnd("iwconfig");
}