how to solve the linux timer signal handler - c++

why is not possible to have a multiple timer with single handler. I used the program from the below link and it is not working. He has explained the reason about the problem but it is not very clear. Is someone help me why is it not working ??
http://www.graphics-muse.org/wp/?p=868

There is no reason you can't have multiple timers created with timer_create using the same handler, provided your sigval structure contains enough information to distinguish between them as needed in your handler.

The point of the article is that it is possible to have multiple timers trigger the same handler but you need to differentiate them based on some passed data. Oddly the example the author used only went 3/4th of the distance of illustrating this in the code so maybe that is the source of your confusion.
Hopefully this reworking of the article's program is a bit clearer. It uses the sival_ptr to point to a string but it can point to any type. This is how the timers are differentiated.
#define _POSIX_C_SOURCE 199309
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
void timerHandler( int sig, siginfo_t *si, void *uc )
{
// obligator caution not to use printf and other async-unsafe calls
// in a handler in real programs
printf("I am timer %s\n", (char *) si->si_value.sival_ptr);
}
void makeTimer(char *name, int intervalMS)
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
// Set up signal handler.
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
errExit("sigaction");
// Set up timer
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = name;
timer_t timerID;
if (timer_create(CLOCK_REALTIME, &te, &timerID) == -1)
errExit("timer_create");
its.it_value.tv_sec = intervalMS;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
if (timer_settime(timerID, 0, &its, NULL) == -1)
errExit("timer_settime");
return;
}
int main(int argc, char *argv[])
{
char *arr[3] = {"number one", "number two", "number three"};
makeTimer(arr[0], 1);
makeTimer(arr[1], 2);
makeTimer(arr[2], 3);
while (sleep(5));
return(0);
}

Related

Multithreaded timer class

I've created a timer class that performs a user supplied action (function that takes no arguments has no return type) at a user supplied interval. This action should be performed in its own thread--i.e. when the timer is created, a new thread is created, and that thread consists of a loop that uses sigwait to wait for the signal to come in before performing the callback. The signal I want to use will be anywhere from SIGRTMIN to SIGRTMAX. I want to be able to create multiple timer objects which means multiple threads and multiple signals (one thread and signal per timer). Using this post, the timer_create man page, and pthread_sigmask man page as references, this is what I have:
//timer.h
#ifndef TIMERS_H
#define TIMERS_H
#include <signal.h>
#include <time.h>
#include <inttypes.h>
#include <stdio.h>
#include <pthread.h>
class CTimer
{
public:
CTimer(uint64_t period_ms, void(*callback)(void), int sig );
private:
typedef void (*Callback)(void);
Callback m_pCallback;
timer_t timerID;
struct sigevent sev;
struct itimerspec its;
struct sigaction sa;
uint8_t timerNum;
pthread_t thread_id;
sigset_t set;
int signal_ID;
void* loop();
friend void* run_loop(void* arg);
};
#endif // TIMERS_H
and
//timer.cpp
#include "timers.h"
void* run_loop(void* arg)
{
return static_cast<CTimer*>(arg)->loop();
}
CTimer::CTimer(uint64_t period_ms, void(*callback)(void), int sig):
m_pCallback(callback), signal_ID(sig)
{
//create mask to send appropriate signal to thread
int s;
sigemptyset(&set);
s = sigaddset(&set, signal_ID);
if (s != 0)
{
printf("error on sigaddset\n");
}
s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
{
printf("error on pthread_sigmask\n");
}
//create new thread that will run the signal handler
s = pthread_create(&thread_id, NULL, run_loop, this);
if (s != 0)
{
printf("error on pthread_create\n");
}
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = signal_ID;
sev.sigev_value.sival_ptr = &timerID;
if (timer_create(CLOCK_REALTIME, &sev, &timerID) == -1)
{
printf("error on timer create\n");
}
its.it_value.tv_sec = period_ms / 1000;
its.it_value.tv_nsec = period_ms % 1000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timer_settime(timerID, 0, &its, NULL) == -1)
{
printf("error on timer settime\n");
}
}
void* CTimer::loop()
{
int s = 0;
while (1)
{
s = sigwait(&set, &signal_ID);
m_pCallback();
}
}
For testing I am using this:
//driver.cpp
#include <stdio.h>
#include <unistd.h>
#include "sys/time.h"
#include "timers.h"
uint64_t get_time_usec()
{
static struct timeval _time_stamp;
gettimeofday(&_time_stamp, NULL);
return _time_stamp.tv_sec*1000000 + _time_stamp.tv_usec;
}
void callbacktest1()
{
printf("tick1 %" PRIu64 " \n", get_time_usec());
}
void callbacktest2()
{
printf("tick2 %" PRIu64 " \n", get_time_usec());
}
int main(int argv, char *argc[])
{
CTimer t1(1000, callbacktest1, SIGRTMIN);
CTimer t2(2000, callbacktest2, SIGRTMIN+1);
pause();
}
When running, it will crash pretty quickly with the error "Real-time signal 1". If I run it in gdb, I get
Starting program: /home/overlord/MySource/Timer/driver
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff75ee700 (LWP 21455)]
[New Thread 0x7ffff6ded700 (LWP 21456)]
tick1 1477336403700925
tick1 1477336404700920
Program received signal SIG35, Real-time event 35.
[Switching to Thread 0x7ffff75ee700 (LWP 21455)]
0x00007ffff7bcc0c1 in do_sigwait (sig=0x7fffffffdbc8, set=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/../../../../../sysdeps/unix/sysv/linux/sigwait.c:60
60 ../nptl/sysdeps/unix/sysv/linux/../../../../../sysdeps/unix/sysv/linux/sigwait.c: No such file or directory.
which is interesting because 35 is what SIGRTMIN+1 is equal to. So maybe I'm not routing the signals correctly? If I only create once instance of the timer in the driver.cpp file, things appear to work ok. Any thoughts are appreciated.
I'm also curious if this is even the right approach to what I'm trying to do. In some brief tests I did, using the system signals seems way more stable than using sleep and usleep to burn up unused loop time.
My guess is that the signal used to wake second thread (CTimer t2) is not blocked by the first thread (CTimer t1). Signal mask in a thread is inherited from a parent thread, so when you start first thread it only has SIGRTMIN signal blocked, but SIGRTMIN+1 can still be delivered to it. Standard reaction to real-time signals is to terminate process, this is what happens. You can test this theory by blocking all real-time signals in all threads started by CTimer class.
I'm not sure why you think that sleep/usleep is less reliable than your own solution, using the right patterns with usleep (basically expecting that it can return sooner and waiting in a loop) always worked OK for me.
I'm not sure why you think that sleep/usleep is less reliable than
your own solution, using the right patterns with usleep (basically
expecting that it can return sooner and waiting in a loop) always
worked OK for me.
I did a basic test using the following code:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include <math.h>
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
#define SQ(x) ((x)*(x))
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
uint64_t start_time_us = 0;
double error_ms = 0;
int64_t count = 0;
int64_t last_time_us = 0;
uint64_t period_ns;
uint64_t get_time_usec()
{
static struct timeval _time_stamp;
gettimeofday(&_time_stamp, NULL);
return _time_stamp.tv_sec*1000000 + _time_stamp.tv_usec;
}
static void
handler(int sig, siginfo_t *si, void *uc)
{
uint64_t timestamp_us = get_time_usec();
double dt_ms = (double)(timestamp_us - last_time_us)/1000;
double elapsed_ms = (double)(timestamp_us - start_time_us)/1000;
error_ms += SQ((dt_ms - (double)period_ns/1000000.0));
count++;
last_time_us = timestamp_us;
}
namespace hidi
{
void pause(const double& tSeconds)
{
unsigned int decimal = static_cast<unsigned int>(floor(tSeconds));
double fraction = tSeconds-static_cast<double>(decimal);
if (decimal > 0)
sleep(decimal);
usleep(static_cast<unsigned long>(floor(fraction*1000000.0)));
return;
}
}
int
main(int argc, char *argv[])
{
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
//sigset_t mask;
struct sigaction sa;
if (argc != 3) {
fprintf(stderr, "Usage: %s <test length-secs> <period-millisec>\n",
argv[0]);
exit(EXIT_FAILURE);
}
uint64_t period_ms = atoll(argv[2]);
period_ns = period_ms * 1000000;
/// FIRST TEST LOOP RATE STABILITY USING THE TIMER
// THE TIMER WILL USE SIGRTMIN (DEFINED ABOVE) AND WILL MEASURE
// STATISTICS ON LOOP STABILITY
/* Establish handler for timer signal */
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG, &sa, NULL) == -1)
errExit("sigaction");
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCKID, &sev, &timerid) == -1)
errExit("timer_create");
printf("timer ID is 0x%lx\n", (long) timerid);
/* Start the timer */
printf("Timing period is %zu ms\n", period_ms);
its.it_value.tv_sec = period_ns / 1000000000;
its.it_value.tv_nsec = period_ns % 1000000000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timer_settime(timerid, 0, &its, NULL) == -1)
errExit("timer_settime");
start_time_us = last_time_us = get_time_usec();
printf("Sleeping for %d seconds\n", atoi(argv[1]));
while ((get_time_usec()-start_time_us)/1000000 < atoi(argv[1]))
{
sleep(1); //this just prevents the while loop from spinning out of control
// the sleep function is interrupted with the signal callback is
// executed. All the magic happens in the callback.
}
printf("ave error: %8.6f ms %zu samples\n",sqrt((double)error_ms/(double)count), count);
timer_delete(timerid); // disarm / delete timer
/// START TEST USING SLEEP / USLEEP
start_time_us = last_time_us = get_time_usec();
error_ms = count = 0;
while ((get_time_usec()-start_time_us)/1000000 < atoi(argv[1]))
{
uint64_t timestamp_us = get_time_usec();
double dt_ms = (double)(timestamp_us - last_time_us)/1000;
double elapsed_ms = (double)(timestamp_us - start_time_us)/1000;
error_ms += SQ((dt_ms - (double)period_ns/1000000.0));
//printf("et=%8.6f ms, dt=%8.6f ms ave error %f\n", elapsed_ms, dt_ms, error_ms/count);
count++;
last_time_us = timestamp_us;
uint64_t consumed_time_us = get_time_usec()-timestamp_us;
uint64_t remaining_looptime_us = (period_ns/1000) - consumed_time_us;
hidi::pause((double)remaining_looptime_us/1000000.0);
}
printf("ave error: %8.6f ms %zu samples\n",sqrt((double)error_ms/(double)count), count);
exit(EXIT_SUCCESS);
}
testing periods from 10 ms to 2 seconds, using timers just seemed way more stable. It seemed using the sleep/usleep method the error was proportional to the period i.e. it was 2-3 ms for a 10 ms period, but 300 - 400 ms for a 1000 ms period. using the timer, the error was pretty constant with different periods.

SIGALRM kills process

I have a trouble with SIGALRM. I am using it to write pids of active processes every 3 seconds.
But after it fires once it kills main process. What did I do wrong?
I am also using signals to kill each of child processes after some time, I am using SIGTERM there.
It wors just fine until I add this part to list active processes. Even after killing main one the others are still going.
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <time.h>
#include <map>
using namespace std;
////DARK SORROW PLACE////////////////////////////
#define CHLD_DELAY 3
std::map<pid_t, bool> pidy;
/////////////////////////////////////////////////
void sweetDreams(int sec, int nanosec)
{
timespec ttw;
ttw.tv_sec = sec;
ttw.tv_nsec = nanosec;
while( nanosleep( &ttw, &ttw) && ( errno != EINTR ) ) continue;
}
//////////////////////////////////////////////////
void ns(long int ns, timespec *ts)
{
ts->tv_sec = (time_t)ns/1000000000;
ts->tv_nsec = (long)(ns - ts->tv_sec*1000000000);
}
//////////////////////////////////////////////////
class Order
{
public:
char* table;
int start;
int step;
int shift;
long int dt;
long int dieAfter;
};
////////////////////////////////////////////////////
void killer(int sig, siginfo_t *siginfo, void *context)
{
// kill(siginfo->si_pid, NULL);
_exit(0);
}
////////////////////////////////////////////////////
void carefullDad(int sig)
{
cout << "lista zywych dzieci:\n-----------------------" << endl;
for(auto i : pidy)
{
if( i.second ) cout << i.first << endl;
}
cout << "-----------------------" << endl;
}
////////////////////////////////////////////////////
int main(int argc, char** argv)
{
char test[] = { 't', 'e', 's', 't' };
Order orderArr[2] = {
{test, 0, 2, 0, 1000000000L, 10000000000L},
{test, 1, 3, -32 , 2000000000L, 6000000000L}
};
//pid_t pidArr[sizeof(orderArr) / sizeof(Order)];
pid_t wpid;
int status = 0;
struct sigevent st;
// memset(&st, 0, sizeof(st));
st.sigev_notify = SIGEV_SIGNAL;
st.sigev_signo = SIGALRM;
struct itimerspec it;
//memset(&it, 0, sizeof(it));
it.it_value = { CHLD_DELAY,0L};
it.it_interval = {CHLD_DELAY,0L};
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = carefullDad;
sigaction(SIGALRM, &act, NULL);
timer_t timer;
timer_create( CLOCK_REALTIME, &st, &timer);
timer_settime(timer, 0, &it, NULL);
for(Order ord : orderArr)
{
// static int i = 0;
pid_t pid = fork();
if(pid == -1)
{
cerr << "Blad!!!" << endl;
exit(1);
}
if(!pid)
{
//some code here
//end rest is here
You forgot to set act.sa_flags.
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = carefullDad;
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
When you set the signal handler it probably had the SA_RESETHAND flag set.
Either ignore the SIGALRM using sigprocmask() or install a valid handler using sigaction().
For everyone encountering this problem in the future.
You need to set:
struct sigaction act;
act.sa_handler = carefullDad;
act.sa_flags = SA_RESTART;
sigaction(SIGALRM, &act, NULL);

setitimer signalling seems only to work after fork

I would like to use the following C++ code to wait for a predefined amount of time (in this example always 2 seconds), but still be interruptible by a signal (that's why I don't use sleep):
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <iostream>
using namespace std;
int measure() {
itimerval idle;
sigset_t sigset;
int sig;
idle.it_value.tv_sec = 2;
idle.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &idle, NULL); // TODO: check return value
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM); // TODO return values
sigaddset(&sigset, SIGUSR1);
sigprocmask(SIG_BLOCK, &sigset, NULL); // TODO return value?
sigwait(&sigset, &sig); // TODO check return value
while(sig != SIGUSR1) {
cout << "Hohoho" << endl;
idle.it_value.tv_sec = 2;
idle.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &idle, NULL); // TODO: check return value
sigwait(&sigset, &sig); // TODO check return value
}
cout << "Done with measurements." << endl;
return 0;
}
int main(int argc, char **argv) {
//if(fork() != 0) exit(0);
//if(fork() == 0) exit(0);
return measure();
}
I would expect this code to print "Hohoho" every 2 seconds until it receives SIGUSR1. Then it prints "Done with measurements." and exits. The second part works as expected. However, I see no "Hohoho", so it seems to me that the SIGALRM from setitimer somehow is not received. The strange thing is that if I do a fork before, the program works as expected. More specifically, if I uncomment either one of the two fork commands at the end, it works. Hence it does not depend on if it's the parent or child process, but somehow the fork event matters. Can someone explain to me what's going on and how to fix my code?
Thanks a lot,
Lutz
(1) Your setitimer is failing because you haven't set it correctly. Struct itimerval contains two structs of type timeval. You are only setting one and thereby picking up whatever garbage was in local storage when idle was declared.
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
If you want a repeating timer every 2 seconds then set the 2nd set to repeat with the same values.
idle.it_value.tv_sec = 2;
idle.it_value.tv_usec = 0;
idle.it_interval.tv_sec = 2;
idle.it_interval.tv_usec = 0;

How to implement a recv() callback

I'm trying to improve my knowledge of OOP and decided to create a simple class to simplify sockets programming.
This is a learning experiment so I do not want to use boost, or other libraries.
I want to implement an event-driven recv(). Meaning, everytime there is new data coming in, it should call my function.
I think I need to create a thread to run a recv() loop and then call my function everytime there is new data. Is there other way around using threads? I want my code to be portable.
Here is my simple Class and example code:
class.h:
#ifndef _SOCKETSCLASS_H
#define _SOCKETSCLASS_H
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define W32
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SOCKET int
#endif
#include <string>
#include<ctime>
#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>
#include <tchar.h>
using namespace std;
#ifdef _DEBUG
#define DEBUG(msg) XTrace(msg)
#else
#define DEBUG(msg, params)
#endif
struct TCP_Client_opts
{
BOOL UseSCprotocol;
BOOL UseEncryption;
BOOL UseCompression;
int CompressionLevel;
void *Callback;
BOOL async;
};
struct TCP_Stats
{
unsigned long int upload; //bytes
unsigned long int download;//bytes
time_t uptime; //seconds
};
class TCP_Client
{
public:
TCP_Client();
TCP_Client(TCP_Client_opts opts_set);
~TCP_Client();
SOCKET GetSocket();
void SetOptions(TCP_Client_opts opts_set);
TCP_Client_opts GetOptions();
BOOL Connect(string server, int port);
int Send(string data);
int Recv(string *data);
BOOL IsConnected();
int Disconnect();
TCP_Stats GetStats();
private:
SOCKET s = SOCKET_ERROR;
TCP_Client_opts opts;
TCP_Stats stats;
BOOL connected = FALSE;
time_t starttime;
};
#endif
class.cpp:
#include "SocketsClass.h"
void XTrace(LPCTSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
int nBuf;
TCHAR szBuffer[512]; // get rid of this hard-coded buffer
nBuf = _vsnwprintf_s(szBuffer, 511, lpszFormat, args);
::OutputDebugString(szBuffer);
va_end(args);
}
TCP_Client::TCP_Client(TCP_Client_opts opts_set)
{
SetOptions(opts_set);
}
TCP_Client::~TCP_Client()
{
Disconnect();
}
TCP_Client::TCP_Client()
{
}
void TCP_Client::SetOptions(TCP_Client_opts opts_set)
{
opts = opts_set;
}
TCP_Client_opts TCP_Client::GetOptions()
{
return opts;
}
SOCKET TCP_Client::GetSocket()
{
return s;
}
BOOL TCP_Client::IsConnected()
{
return connected;
}
int TCP_Client::Disconnect()
{
connected = FALSE;
stats.uptime = time(0) - starttime;
return shutdown(s, 2);
}
BOOL TCP_Client::Connect(string server, int port)
{
struct sockaddr_in RemoteHost;
#ifdef W32
WSADATA wsd;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
DEBUG(L"Failed to load Winsock!\n");
return FALSE;
}
#endif
//create socket if it is not already created
if (s == SOCKET_ERROR)
{
//Create socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == SOCKET_ERROR)
{
DEBUG(L"Could not create socket");
return FALSE;
}
}
//setup address structure
if (inet_addr(server.c_str()) == INADDR_NONE)
{
struct hostent *he;
//resolve the hostname, its not an ip address
if ((he = gethostbyname(server.c_str())) == NULL)
{
//gethostbyname failed
DEBUG(L"gethostbyname() - Failed to resolve hostname\n");
return FALSE;
}
}
else//plain ip address
{
RemoteHost.sin_addr.s_addr = inet_addr(server.c_str());
}
RemoteHost.sin_family = AF_INET;
RemoteHost.sin_port = htons(port);
//Connect to remote server
if (connect(s, (struct sockaddr *)&RemoteHost, sizeof(RemoteHost)) < 0)
{
DEBUG(L"connect() failed");
return FALSE;
}
connected = TRUE;
starttime = time(0);
stats.download = 0;
stats.upload = 0;
return TRUE;
}
TCP_Stats TCP_Client::GetStats()
{
if (connected==TRUE)
stats.uptime = time(0)-starttime;
return stats;
}
int TCP_Client::Send(string data)
{
stats.upload += data.length();
return send(s, data.c_str(), data.length(), 0);
}
int TCP_Client::Recv(string *data)
{
int ret = 0;
char buffer[512];
ret = recv(s, buffer, sizeof(buffer), 0);
data->assign(buffer);
data->resize(ret);
stats.download += data->length();
return ret;
}
main.cpp:
#include <stdio.h>
#include <string.h>
#include "SocketsClass.h"
using namespace std;
int main(int argc, char *argv)
{
TCP_Client tc;
tc.Connect("127.0.0.1", 9999);
tc.Send("HEllo");
string data;
tc.Recv(&data);
puts(data.c_str());
tc.Disconnect();
printf("\n\nDL: %i\nUP: %i\nUptime: %u\n", tc.GetStats().download, tc.GetStats().upload, tc.GetStats().uptime);
return 0;
}
Some extra questions:
Imagine I'm sending a file. How would my function know that the current data is related to the previous message?
How is my class design and implementation? SHould I change anything?
Thank you
If by "portable" you mean runs on other platforms besides Windows then a recv() loop in a worker thread is your only portable option. On Windows specifically, you have some additional choices:
Allocate a hidden window and then use WSAAsyncSelect() to receive FD_READ notifications. This requires a message loop, which you can put in a worker thread.
Use WSAEventSelect() to register a waitable event for FD_READ notifications and then wait for those events via WSAWaitForMultipleEvents() in a thread.
use WSARecv() with an I/O Completion Port. Poll the IOCP via GetQueuedCompletionResult() in a thread.
As for your question regarding messaging, TCP is a byte stream, it has no concept of messages. You have to frame your messages yourself. You can either:
give each message a fixed header that contains the message length. Read the header first, then read however many bytes it says, then read the next header, and so on.
separate each message with a unique delimiter that does not appear in the message data. Read until you encounter that delimiter, then read until the next delimiter, and so on.
Have your event loop call either poll or select to determine if there is data that can be read on the socket(s). Then read it, and call the appropriate callback function.

C++ (under Linux) program not giving expected output ( timer )

I have this C++ program. It has a simple for loop which prints numbers from 1 to 20. Between this, execution, the timer expires multiple times, and each time it expires, it should print an output from signal handler.
Unfortunately i am not getting this output. And it is just simply printing the numbers from 1 to 20. Can someone please help me?
Thanks in advance
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
#include <iostream>
using namespace std;
static int flag=0;
class timer{
static void
handler(int sig) {
printf("Caught signal %d\n", sig);
::flag=1;
signal(sig, handler);
}
public:
void timer_func()
{
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
long long freq_nanosecs=1; // The timer frequency in nanosecs
sigset_t mask;
struct sigaction sa;
/* Establish handler for timer signal */
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_RESETHAND;
sa.sa_handler = handler;
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCKID, &sev, &timerid) == -1)
errExit("timer_create");
printf("timer ID is 0x%lx\n", (long) timerid);
/* Start the timer */
its.it_value.tv_sec = freq_nanosecs / 1000000000;
its.it_value.tv_nsec = freq_nanosecs % 1000000000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timer_settime(timerid, 0, &its, NULL) == -1)
errExit("timer_settime");
}
};
int main() {
timer ob;
ob.timer_func();
for(int i=0; i<20; i++) {
sleep(1);
if(flag) {
cout << "Timer called" << endl;
flag=0;
}
cout << "Printing i: " << i << endl;
}
}
But if i set "long long freq_nanosecs = 1" in that also the output is only once from the timer. It should be repeated
0.011 is a double literal, you assign it to a long, so it's converted to 0. This simply sets freq_nanosecs to 0.
long freq_nanosecs=0.011
This disarms the timer since the timer values are 0.
timer_settime(timerid, 0, &its, NULL)
First things first: please do not use signal(2) to install signal handlers. Use sigaction(2) instead; signal(2) is unreliable and very unportable.
Next, you're trying to use printf(3) inside a signal handler. printf(3) is not a signal-safe function. The only "standard" functions you are allowed to use inside a signal handler are listed in signal(7). (You can also call your own functions, iff they only call functions listed in signal(7) or do very simple operations, such as setting a flag to indicate that a signal has occured.)
The signal handler is not installed. Call sigaction function as follows:
sa.sa_flags = SA_RESETHAND;
sa.sa_handler = handler;
sigaction(SIG, &sa, NULL);
The sa_flags SA_RESETHAND (man sigaction)
Restore the signal action to the default state once the signal handler has been called
And (man 7 signal)
The default action for an unhandled real-time signal is to terminate
the receiving process.
Before print, next real-time signal received, and exit the program.
Please set the sa_flags to zero.