I am running this program where I have multiple threads. Three threads are generating signals for the same parent process. There are four handler threads for handling the signals generated by the signal generating threads. I have a monitoring thread which also receives the signals and processes according. However, I have a situation. I can see that the signals are not divided equally. I mean the signals are directed to the same process. I have four handler threads and one monitoring thread waiting for the signal. So anyone of them can receive the signal. I was expecting it to be distributed uniformly. However, I could see that at time a whole burst of signals are received by the handler threads. The very next time the whole burst of signals are handled by the monitor thread. Why isn’t it uniform. I have added a sleep call after the handler/ monitor threads complete handling one signal. So as soon as the handler/monitor completes one signal it should have given the other opportunity to handle the next signal. However, the output doesn’t show the case
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <cstdio>
#include <stdlib.h>
#define NUM_SENDER_PROCESSES 3
#define NUM_HANDLER_PROCESSES 4
#define NUM_SIGNAL_REPORT 10
#define MAX_SIGNAL_COUNT 100000
using namespace std;
volatile int usrsig1_handler_count = 0;
int usrsig2_handler_count = 0;
int usrsig1_sender_count = 0;
int usrsig2_sender_count = 0;
int monitor_count = 0;
int usrsig1_monitor_count = 0;
int usrsig2_monitor_count = 0;
double time_1[10];
double time_2[10];
int lock_1 = 0;
int lock_2 = 0;
int lock_3 = 0;
int lock_4 = 0;
int lock_5 = 0;
double timestamp() {
struct timeval tp;
gettimeofday(&tp, NULL);
return (double)tp.tv_sec + tp.tv_usec / 1000000.;
}
void sleepMs(double seconds) {
usleep((unsigned int)(seconds*1000000));
}
void *senderfunc(void *parm) {
srand(time(0));
while(true) {
int signal_id = rand()%2 + 1;
if(signal_id == 1) {
while(__sync_lock_test_and_set(&lock_3,1) != 0) {
}
usrsig1_sender_count++;
lock_3 = 0;
kill(getpid(), SIGUSR1);
} else {
while(__sync_lock_test_and_set(&lock_4,1) != 0) {
}
usrsig2_sender_count++;
lock_4 = 0;
kill(getpid(), SIGUSR2);
}
int r = rand()%10 + 1;
double s = (double)r/100;
sleepMs(s);
}
}
void *handlerfunc(void *parm)
{
int *index = (int *)parm;
sigset_t set;
sigemptyset(&set);
//cout << (*index) << endl;
if((*index) % 2 == 0) {
sigaddset(&set, SIGUSR1);
} else {
sigaddset(&set, SIGUSR2);
}
int sig;
while(true) {
sigwait(&set, &sig);
//cout << "Handler" << endl;
if (sig == SIGUSR1) {
while(__sync_lock_test_and_set(&lock_1,1) != 0) {
}
usrsig1_handler_count++;
lock_1 = 0;
} else if(sig == SIGUSR2) {
while(__sync_lock_test_and_set(&lock_2,1) != 0) {
}
usrsig2_handler_count++;
lock_2 = 0;
}
sleepMs(0.0001);
}
}
void *monitorfunc(void *parm) {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
int sig;
while(true) {
sigwait(&set, &sig);
//cout << "Monitor" << endl;
if(sig == SIGUSR1) {
time_1[usrsig1_monitor_count] = timestamp();
usrsig1_monitor_count++;
} else if(sig == SIGUSR2) {
time_2[usrsig2_monitor_count] = timestamp();
usrsig2_monitor_count++;
}
monitor_count++;
//cout << monitor_count << endl;
if(monitor_count == NUM_SIGNAL_REPORT) {
double difference_1 = 0;
double difference_2 = 0;
if(usrsig1_monitor_count > 1) {
for(int i=0; i<usrsig1_monitor_count-1; i++) {
difference_1 = difference_1 + time_1[i+1] - time_1[i];
}
cout << "Interval SIGUSR1 = " << difference_1/(usrsig1_monitor_count-1)<< endl;
}
if(usrsig2_monitor_count > 1) {
for(int i=0; i<usrsig2_monitor_count-1; i++) {
difference_2 = difference_2 + time_2[i+1] - time_2[i];
}
cout << "Interval SIGUSR2 = " << difference_2/(usrsig2_monitor_count-1) << endl;
}
cout << "Count SIGUSR1 = " << usrsig1_sender_count << endl;
cout << "Count SIGUSR2 = " << usrsig2_sender_count << endl;
monitor_count = 0;
usrsig1_monitor_count = 0;
usrsig2_monitor_count = 0;
}
sleepMs(0.001);
}
}
int main(int argc, char **argv)
{
if(argc != 2) {
cout << "Required parameters missing. " << endl;
cout << "Option 1 = 1 which means run for 30 seconds" << endl;
cout << "Option 2 = 2 which means run until 100000 signals" << endl;
exit(0);
}
int option = atoi(argv[1]);
int i;
pthread_t handlers[NUM_HANDLER_PROCESSES];
pthread_t generators[NUM_SENDER_PROCESSES];
pthread_t monitor;
sigset_t set;
sigset_t oldset;
sigemptyset(&oldset);
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
pthread_sigmask(SIG_BLOCK, &set, &oldset);
int handler_mask[4] = {0,1,2,3};
//Initializing the handler threads
for(i=0; i<NUM_HANDLER_PROCESSES; i++) {
pthread_create(&handlers[i], NULL, handlerfunc, (void *)&handler_mask[i]);
}
pthread_create(&monitor, NULL, monitorfunc, NULL);
sleep(5);
for(i=0; i<NUM_SENDER_PROCESSES; i++) {
pthread_create(&generators[i], NULL, senderfunc, NULL);
}
if(option == 1) {
cout << "Option 1 " << endl;
//sleep(30);
while(true){
}
exit(0);
} else {
while(true) {
if((usrsig1_handler_count + usrsig2_handler_count) >= MAX_SIGNAL_COUNT) {
cout << "Count SIGUSR1 = " << usrsig1_handler_count << endl;
cout << "Count SIGUSR2 = " << usrsig2_handler_count << endl;
exit(0);
} else {
pthread_yield();
}
}
}
}
Here is my output
HandlerHandler
Handler
Handler
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Monitor
Handler
Handler
Handler
Handler
Handler
Handler
Handler
Handler
Handler
Handler
You can see the burst of monitor followed by burst of handler. However, in the code once a handler/monitor handles a signal and goes for sigwait, I have added a sleep call so that the turn is passed to the next available thread. However, this is not helping. This should have made it uniform I guess. However, still monitor gets burst and print. Even though in monitor I have placed sleep after it has finished its job for a signal
There are two different problems with your code giving you the result you see.
The first and the most important one is that each thread is allocated a time slice for running. This time slice can be interrupted by signals or by IO but rather than that a thread will run until it finishes it’s time slice. So even if you put a sleep – if the sleep time is smaller than the time slice for that thread it will not transfer execution to other threads. If I remember on Windows this time slice is at least 5ms but generally 40ms (Although I may have the number wrong). On linux this time slice may be shorter but for general purpose linux box I think it’s the same. I used to use Sleep(0) to relinquish the time slice on Windows. usleep(0) may do the same. So the way you use sleep will not do what you want. However since you are using pthreads just call pthread_yield to relinquish CPU or sched_yield() which supposed to be better practice but who knows…
The second problem you may be facing with the output of your test is that you actually have no direct way of controlling the order of characters output to a stream to in a multi-threaded environment. If you really want the output to be in order you need to implement a separate thread for doing the output and send messages to i using a queue and either some locking mechanism (Critical-section, mutex) or a free-lock mechanism.
Hope this get’s you in the right direction