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);
}
}
}
Related
I would like to measure the maximum memory usage of abc.exe on random tests generated by gen.exe. How could I do that?
My code that runs abc.exe on tests from gen.exe looks like this:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i = 0;
while (true)
{
string si = to_string(i);
cout << i << "\n";
if (system(("echo " + si + "| ./gen.exe > test.in").c_str())) // gen.exe is test generator
{
cout << "gen error\n";
break;
}
if (system(("./abc.exe < test.in > a.out"))) // abc.exe is the program I want to test
{
cout << "abc error\n";
break;
}
i++;
}
}
I know that i can use time -v ./abc.exe but then the used memory is printed in the terminal but I'd like to be able to save it to a variable.
You can use getrusage( RUSAGE_CHILDREN, ... ) to obtain the maximum resident memory. Note that this call will return the maximum memory used by the biggest child at that point in time.
In the example below I used boost::process because it gives better control but it's up to you to use std::system or not, works the same way.
#include <string>
#include <cstdint>
#include <string.h>
#include <iostream>
#include <boost/process/child.hpp>
#include <sys/resource.h>
namespace bp = boost::process;
int parent( const std::string& exename )
{
// Loop from 0 to 10 megabytes
for ( int j=0; j<10; ++j )
{
// Command name is the name of this executable plus one argument with size
std::string gencmd = exename + " " + std::to_string(j);
// Start process
bp::child child( gencmd );
// Wait for it to allocate memory
sleep(1);
// Query the memory usage at this point in time
struct rusage ru;
getrusage( RUSAGE_CHILDREN, &ru );
std::cerr << "Loop:" << j << " mem:"<< ru.ru_maxrss/1024. << " MB" << std::endl;
// Wait for process to quit
child.wait();
if ( child.exit_code()!=0 )
{
std::cerr << "Error executing child:" << child.exit_code() << std::endl;
return 1;
}
}
return 0;
}
int child( int size ) {
// Allocated "size" megabites explicitly
size_t memsize = size*1024*1024;
uint8_t* ptr = (uint8_t*)malloc( memsize );
memset( ptr, size, memsize );
// Wait for the parent to sample our memory usage
sleep( 2 );
// Free memory
free( ptr );
return 0;
}
int main( int argc, char* argv[] )
{
// Without arguments, it is the parent.
// Pass the name of the binary
if ( argc==1 ) return parent( argv[0] );
return child( std::atoi( argv[1] ) );
}
It prints
$ ./env_test
Loop:0 mem:0 MB
Loop:1 mem:3.5625 MB
Loop:2 mem:4.01953 MB
Loop:3 mem:5.05469 MB
Loop:4 mem:6.04688 MB
Loop:5 mem:7.05078 MB
Loop:6 mem:7.78516 MB
Loop:7 mem:8.97266 MB
Loop:8 mem:9.82031 MB
Loop:9 mem:10.8867 MB
If you cannot use boost libraries, you'd got to work a little more but it is still feasible.
If you just want to know the maximum size ever of your children processes then the following works with std::system:
#include <cstdio>
#include <string>
#include <iostream>
#include <sstream>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
int main(int argc, char* argv[]) {
if (argc > 1) {
size_t size = ::atol(argv[1]);
size_t memsize = size * 1024 * 1024;
void* ptr = ::malloc(memsize);
memset(ptr, 0, memsize);
::sleep(2);
::free(ptr);
return 0;
}
for (int j = 0; j < 10; ++j) {
std::ostringstream cmd;
cmd << argv[0] << " " << j;
int res = std::system(cmd.str().c_str());
if (res < 0) {
fprintf(stderr, "ERROR system: %s\n", strerror(errno));
break;
}
struct rusage ru;
res = getrusage(RUSAGE_CHILDREN, &ru);
size_t maxmem = ru.ru_maxrss;
fprintf(stderr, "Loop:%d MaxMem:%ld\n", j, maxmem);
}
return 0;
}
It prints
Loop:0 MaxMem:3552
Loop:1 MaxMem:4192
Loop:2 MaxMem:5148
Loop:3 MaxMem:6228
Loop:4 MaxMem:7364
Loop:5 MaxMem:8456
Loop:6 MaxMem:9120
Loop:7 MaxMem:10188
Loop:8 MaxMem:11324
Loop:9 MaxMem:12256
However if you want to keep track of the memory usage during the child process execution you cannot use std::system(). First, you need to call fork() to spawn a new process and then execv() to execute a bash command.
#include <string>
#include <cstdint>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/resource.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <vector>
int parent(const std::string& exename) {
// Loop from 0 to 10 megabytes
for (int j = 0; j < 10; ++j) {
// Command name is the name of this executable plus one argument with size
std::string gencmd = exename + " " + std::to_string(j);
// Start process
pid_t pid = fork();
if (pid == 0) { // child
const char* args[] = {"/bin/bash", "-c", gencmd.c_str(), (char*)0};
int res = execv("/bin/bash", (char**)args);
// Should never return
std::cerr << "execv error: " << strerror(errno) << std::endl;
return 1;
}
// parent
long maxmem = 0;
while (true) {
int status;
pid_t rid = ::waitpid(pid, &status, WNOHANG);
if (rid < 0) {
if (errno != ECHILD) {
std::cerr << "waitpid:" << strerror(errno) << std::endl;
return 2;
}
break;
}
if (rid == pid) {
if (WIFEXITED(pid)) {
break;
}
}
// Wait for it to allocate memory
usleep(10000);
// Query the memory usage at this point in time
struct rusage ru;
int res = getrusage(RUSAGE_CHILDREN, &ru);
if (res != 0) {
if (errno != ECHILD) {
std::cerr << "getrusage:" << errno << strerror(errno) << std::endl;
}
break;
}
if (maxmem < ru.ru_maxrss) {
maxmem = ru.ru_maxrss;
}
}
std::cerr << "Loop:" << j << " mem:" << maxmem / 1024. << " MB" << std::endl;
}
return 0;
}
int child(int size) {
// Allocated "size" megabites explicitly
size_t memsize = size * 1024 * 1024;
uint8_t* ptr = (uint8_t*)malloc(memsize);
memset(ptr, size, memsize);
// Wait for the parent to sample our memory usage
sleep(2);
// Free memory
free(ptr);
return 0;
}
int main(int argc, char* argv[]) {
// Without arguments, it is the parent.
// Pass the name of the binary
if (argc == 1) return parent(argv[0]);
return child(std::atoi(argv[1]));
}
The result on my machine is:
$ ./fork_test
Loop:0 mem:3.22656 MB
Loop:1 mem:3.69922 MB
Loop:2 mem:4.80859 MB
Loop:3 mem:5.92578 MB
Loop:4 mem:6.87109 MB
Loop:5 mem:8.05469 MB
Loop:6 mem:8.77344 MB
Loop:7 mem:9.71875 MB
Loop:8 mem:10.7422 MB
Loop:9 mem:11.6797 MB
There is a video about this post.
I have a process which is using boost message queue. When it is being blocked in either send or receive due to queue size limit has been reached, if I send a signal, it seemed the function call remained blocking. I expected the call to cancel or raise an exception but it didn't behave that way. How can I interrupt the send or receive function call ?
#include <boost/interprocess/ipc/message_queue.hpp>
#include <signal.h>
#include <iostream>
#include <vector>
using namespace boost::interprocess;
static sig_atomic_t do_exit = 0;
void sig_handler(int sig)
{
printf("signal %d", sig);
do_exit = 1;
}
int main ()
{
signal(SIGINT, sig_handler);
try{
//Erase previous message queue
message_queue::remove("message_queue");
//Create a message_queue.
message_queue mq
(create_only //only create
,"message_queue" //name
,5 //max message number
,sizeof(int) //max message size
);
//Send 100 numbers
for(int i = 0; i < 100 && !do_exit; ++i){
mq.send(&i, sizeof(i), 0);
printf("%i\n", i);
}
printf("finished\n");
}
catch(interprocess_exception &ex){
std::cout << ex.what() << std::endl;
return 1;
}
catch(...) {
std:: cout << "Exception" << std::endl;
}
return 0;
}
The way is to use the timed interfaces:
for (int i = 0; i < 100 && !do_exit; ++i) {
while (!do_exit) {
if (mq.timed_send(&i, sizeof(i), 0, now() + 10ms)) {
printf("%i\n", i);
break;
}
}
sleep_for(50ms);
}
E.g.:
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <signal.h>
#include <vector>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
namespace bip = boost::interprocess;
static sig_atomic_t do_exit = 0;
void sig_handler(int sig)
{
printf("signal %d\n", sig);
do_exit = 1;
}
int main()
{
auto now = std::chrono::steady_clock::now;
using std::this_thread::sleep_for;
signal(SIGINT, sig_handler);
try {
bip::message_queue::remove("message_queue");
bip::message_queue mq(bip::create_only, // only create
"message_queue", // name
5, // max message number
sizeof(int) // max message size
);
// Send 100 numbers
for (int i = 0; i < 100 && !do_exit; ++i) {
while (!do_exit) {
if (mq.timed_send(&i, sizeof(i), 0, now() + 10ms)) {
printf("%i\n", i);
break;
}
}
sleep_for(50ms);
}
printf("finished\n");
} catch (bip::interprocess_exception const& ex) {
std::cout << ex.what() << std::endl;
return 1;
} catch (...) {
std::cout << "Exception" << std::endl;
return 2;
}
}
Demo
I am trying to create shared memory, but whenever I access it from a child process its value is different than what it should be. I think that I am using shmget() correctly. I have tried a lot of stuff that I have found online, but I can't find anyone with the same problem I am having. No matter what I enter num as, whenever I try to get l->returnLicense it outputs 0. I'm really at a loss as to what to try next.
#include "license.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string>
#include <sys/wait.h>
#include <iostream>
#include <unistd.h>
using namespace std;
int validateArguments (int num) {
if (num == -69) {
//no arg
return 10;
}
if (num < 1 || num > 20) {
//warning use 20 as num
return 10;
}
return num;
}
int initSharedMemory (License *l) {
key_t key = ftok("/tmp", 'J');
cout << "key: " << key << endl;
int shmid = shmget(key, sizeof(l), 0666|IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
return -1;
}
l = (License*)shmat(shmid, (void*)0, 0);
if (l == (void*) -1) {
perror("Shared memory attach");
return -1;
}
return shmid;
}
void detachSharedMemory (License *l) {
shmdt(l);
}
void destroySharedMemory (int shmid) {
shmctl(shmid, IPC_RMID, NULL);
}
void spawn (int shmid) {
pid_t c_pid = fork();
if (c_pid == -1) {
perror("fork");
} else if (c_pid > 0) {
cout << "parent" << shmid << endl;
c_pid = wait(NULL);
} else {
cout << "child" << endl;
License *l;
key_t key = ftok("/tmp", 'J');
cout << "key: " << key << endl;
int shmid = shmget(key, sizeof(l), 0666);
cout << shmid;
l = (License*) shmat(shmid,0,0);
if(l == (void*) -1) {
perror("memory attach");
exit(0);
}
int num = l->returnLicense();
cout << num << "num\n";
shmdt(l);
char* args[] = {"./testChild", NULL};
execvp(args[0],args);
exit(0);
}
}
int main (int argv, char *argc[]) {
int num;
if (argv == 2) {
num = atoi(argc[1]);
} else {
num = -69;
}
num = validateArguments (num);
License *l;
int shmid = initSharedMemory (l);
License *tmp = l;
tmp->initLicense(num);
spawn(shmid);
cout << l->returnLicense() << endl;
detachSharedMemory(l);
destroySharedMemory(shmid);
return 0;
}
I'm not including the entirety of my code, but I think this is enough to illustrate my problem. I copied code from the testChild that I exec from this process so that you can see the problem I'm facing all in one file.
License *l; // uninitialized
int shmid = initSharedMemory (l); // pass l by value, UB!
License *tmp = l; // copy uninitialized pointer, UB!
tmp->initLicense(num); // call member function through uninitialized pointer, BOOM!
spawn(shmid);
cout << l->returnLicense() << endl; // call member function through uninitialized pointer, BOOM!
Probably you meant for your initSharedMemory() function to have a reference-typed parameter, so that it would affect the License * l; variable in main().
I have following piece of code :
// temp.h
#include <iostream>
#include <pthread.h>
static pthread_spinlock_t mylock;
__attribute__((constructor))
void init_lock() {
if(pthread_spin_init(&my_lock,0) != 0) {
// hard exit !!
exit(1);
}
}
__attribute__((destructor))
void uninit_lock() {
if(pthread_spin_destroy(&my_lock) != 0) {
// hard exit !!
exit(1);
}
}
#define EXPAND_VAR(ARG1, ARG2) \
do {\
assert(pthread_spin_lock(&my_lock) == 0);
/// do something
assert(pthread_spin_unlock(&my_lock) == 0);\
}while(0)
// main.cpp
#include "temp.h"
#define NUM_THREADS 5
void *logs(void *i){
int j;
for(j = 0 ; j < 10000 ; j++){
EXPAND_VAR(j, j+1);
}
std::cout << "returning from thread func" << std::endl;
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
int i;
pthread_t threads[NUM_THREADS];
for( i = 0; i < NUM_THREADS; i++) {
std::cout << " creating threads" << std::endl;
rc = pthread_create(&threads[i],NULL,logs,(void *)i);
}
std::cout << "returning from main" << std::endl;
pthread_exit(NULL);
return 0;
}
When executing the above code 'pthread_spin_lock' always gets blocked. I tried 'pthread_spin_trylock' and it passes with return value 0.
Anything i am missing that 'pthread_spin_lock' not working ?
I am using gcc version 4.1.2 :(
I'm studying ZeroMQ with myself.
I tested PUB as a server(bind), SUB as a client(connect) and worked fine. Opposite (PUB as a client(connect), SUB as a server(bind)) also works fine.
When I connect a another SUB socket as client something goes wrong without any exception or errors.
here's my example code.
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
#include <thread>
class ZMQSock
{
public:
ZMQSock(const char* addr)
{
if (addr != NULL)
{
mctx = new zmq::context_t(1);
mszAddr = new char[strlen(addr) + 1];
snprintf(mszAddr, strlen(addr) + 1, "%s", addr);
}
}
virtual ~ZMQSock()
{
if (msock != nullptr)
delete msock;
if (mctx != nullptr)
delete mctx;
if (mszAddr != nullptr)
delete [] mszAddr;
}
int zbind()
{
if (msock != nullptr)
msock->bind(mszAddr);
else return -1;
return 0;
}
int zconnect()
{
if (msock != nullptr)
msock->connect(mszAddr);
else return -1;
return 0;
}
void start()
{
if (mbthread != false)
return ;
mbthread = true;
mhthread = std::thread(std::bind(&ZMQSock::run, this));
}
virtual void stop()
{
if (mbthread == false)
return ;
mbthread = false;
if (mhthread.joinable())
mhthread.join();
}
virtual void run() = 0;
protected:
char* mszAddr{nullptr};
zmq::context_t* mctx{nullptr};
zmq::socket_t* msock{nullptr};
bool mbthread{false};
std::thread mhthread;
};
class ZPublisher : public ZMQSock
{
public:
ZPublisher(const char* addr) : ZMQSock(addr)
{
if (msock == nullptr)
{
msock = new zmq::socket_t(*mctx, ZMQ_PUB);
}
}
virtual ~ZPublisher()
{
}
bool zsend(const char* data, const unsigned int length, bool sendmore=false)
{
zmq::message_t msg(length);
memcpy(msg.data(), data, length);
if (sendmore)
return msock->send(msg, ZMQ_SNDMORE);
return msock->send(msg);
}
void run()
{
if (mszAddr == nullptr)
return ;
if (strlen(mszAddr) < 6)
return ;
const char* fdelim = "1";
const char* first = "it sends to first. two can not recv this sentence!\0";
const char* sdelim = "2";
const char* second = "it sends to second. one can not recv this sentence!\0";
while (mbthread)
{
zsend(fdelim, 1, true);
zsend(first, strlen(first));
zsend(sdelim, 1, true);
zsend(second, strlen(second));
usleep(1000 * 1000);
}
}
};
class ZSubscriber : public ZMQSock
{
public:
ZSubscriber(const char* addr) : ZMQSock(addr)
{
if (msock == nullptr)
{
msock = new zmq::socket_t(*mctx, ZMQ_SUB);
}
}
virtual ~ZSubscriber()
{
}
void setScriberDelim(const char* delim, const int length)
{
msock->setsockopt(ZMQ_SUBSCRIBE, delim, length);
mdelim = std::string(delim, length);
}
std::string zrecv()
{
zmq::message_t msg;
msock->recv(&msg);
return std::string(static_cast<char*>(msg.data()), msg.size());
}
void run()
{
if (mszAddr == nullptr)
return ;
if (strlen(mszAddr) < 6)
return ;
while (mbthread)
{
std::cout << "MY DELIM IS [" << mdelim << "] - MSG : ";
std::cout << zrecv() << std::endl;
usleep(1000 * 1000);
}
}
private:
std::string mdelim;
};
int main ()
{
ZPublisher pub("tcp://localhost:5252");
ZSubscriber sub1("tcp://localhost:5252");
ZSubscriber sub2("tcp://*:5252");
pub.zconnect();
sub1.zconnect();
sub2.zbind();
sub1.setScriberDelim("1", 1);
sub2.setScriberDelim("2", 1);
pub.start();
std::cout << "PUB Server has been started.." << std::endl;
usleep(1000 * 1000);
sub1.start();
std::cout << "SUB1 Start." << std::endl;
sub2.start();
std::cout << "SUB2 Start." << std::endl;
int i = 0;
std::cout << "< Press any key to exit program. >" << std::endl;
std::cin >> i;
std::cout << "SUB1 STOP START" << std::endl;
sub1.stop();
std::cout << "SUB2 STOP START" << std::endl;
sub2.stop();
std::cout << "PUB STOP START" << std::endl;
pub.stop();
std::cout << "ALL DONE" << std::endl;
return 0;
}
What causes this? or Am I using PUB/SUB illegally?
You are connecting a SUB socket to a SUB socket, that is an invalid connection. In your case the PUB should bind and the SUBs should connect.