Confusion related to sigwait in multiprocess system - multithreading

I am having difficulty in understanding IPC in multiprocess system. I have this system where there are three child processes that send two types of signals to their process group. There are four types of signal handling processes responsible for a particular type of signal.
There is this monitoring process which waits for both the signals and then processes accordingly. When I run this program for a while, the monitoring process doesn't seem to pick up the signal as well as the signal handling process. I could see in the log that the signal is only being generated but not handled at all.
My code is given below
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
using namespace std;
double timestamp() {
struct timeval tp;
gettimeofday(&tp, NULL);
return (double)tp.tv_sec + tp.tv_usec / 1000000.;
}
double getinterval() {
srand(time(NULL));
int r = rand()%10 + 1;
double s = (double)r/100;
}
int count;
int count_1;
int count_2;
double time_1[10];
double time_2[10];
pid_t senders[1];
pid_t handlers[4];
pid_t reporter;
void catcher(int sig) {
printf("Signal catcher called for %d",sig);
}
int main(int argc, char *argv[]) {
void signal_catcher_int(int);
pid_t pid,w;
int status;
if(signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
perror("1");
return 1;
}
if(signal(SIGUSR2 ,SIG_IGN) == SIG_ERR) {
perror("2");
return 2;
}
if(signal(SIGINT,signal_catcher_int) == SIG_ERR) {
perror("3");
return 2;
}
//Registering the signal handler
for(int i=0; i<4; i++) {
if((pid = fork()) == 0) {
cout << i << endl;
//struct sigaction sigact;
sigset_t sigset;
int sig;
int result = 0;
sigemptyset(&sigset);
if(i%2 == 0) {
if(signal(SIGUSR2, SIG_IGN) == SIG_ERR) {
perror("2");
return 2;
}
sigaddset(&sigset, SIGUSR1);
sigprocmask(SIG_BLOCK, &sigset, NULL);
} else {
if(signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
perror("2");
return 2;
}
sigaddset(&sigset, SIGUSR2);
sigprocmask(SIG_BLOCK, &sigset, NULL);
}
while(true) {
int result = sigwait(&sigset, &sig);
if(result == 0) {
cout << "The caught signal is " << sig << endl;
}
}
exit(0);
} else {
cout << "Registerd the handler " << pid << endl;
handlers[i] = pid;
}
}
//Registering the monitoring process
if((pid = fork()) == 0) {
sigset_t sigset;
int sig;
int result = 0;
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
sigaddset(&sigset, SIGUSR2);
sigprocmask(SIG_BLOCK, &sigset, NULL);
while(true) {
int result = sigwait(&sigset, &sig);
if(result == 0) {
cout << "The monitored signal is " << sig << endl;
} else {
cout << "error" << endl;
}
}
} else {
reporter = pid;
}
sleep(3);
//Registering the signal generator
for(int i=0; i<1; i++) {
if((pid = fork()) == 0) {
if(signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
perror("1");
return 1;
}
if(signal(SIGUSR2, SIG_IGN) == SIG_ERR) {
perror("2");
return 2;
}
srand(time(0));
while(true) {
volatile int signal_id = rand()%2 + 1;
cout << "Generating the signal " << signal_id << endl;
if(signal_id == 1) {
killpg(getpgid(getpid()), SIGUSR1);
} else {
killpg(getpgid(getpid()), SIGUSR2);
}
int r = rand()%10 + 1;
double s = (double)r/100;
sleep(s);
}
exit(0);
} else {
cout << "Registered the sender " << pid << endl;
senders[i] = pid;
}
}
while(w = wait(&status)) {
cout << "Wait on PID " << w << endl;
}
}
void signal_catcher_int(int the_sig) {
//cout << "Handling the Ctrl C signal " << endl;
for(int i=0; i<1; i++) {
kill(senders[i],SIGKILL);
}
for(int i=0; i<4; i++) {
kill(handlers[i],SIGKILL);
}
kill(reporter,SIGKILL);
exit(3);
}
Any suggestions?
Here is a sample of the output as well
In the beginning
Registerd the handler 9544
Registerd the handler 9545
1
Registerd the handler 9546
Registerd the handler 9547
2
3
0
Registered the sender 9550
Generating the signal 1
The caught signal is 10
The monitored signal is 10
The caught signal is 10
Generating the signal 1
The caught signal is 10
The monitored signal is 10
The caught signal is 10
Generating the signal 1
The caught signal is 10
The monitored signal is 10
The caught signal is 10
Generating the signal 1
The caught signal is 10
The monitored signal is 10
The caught signal is 10
Generating the signal 2
The caught signal is 12
The caught signal is 12
The monitored signal is 12
Generating the signal 2
Generating the signal 2
The caught signal is 12
The caught signal is 12
Generating the signal 1
The caught signal is 12
The monitored signal is 10
The monitored signal is 12
Generating the signal 1
Generating the signal 2
The caught signal is 12
Generating the signal 1
Generating the signal 2
10
The monitored signal is 10
The caught signal is 12
Generating the signal 1
The caught signal is 12
The monitored signal is GenThe caught signal is TheThe caught signal is 10
Generating the signal 2
Later on
The monitored signal is GenThe monitored signal is 10
Generating the signal 1
Generating the signal 2
The caught signal is 10
The caught signal is 10
The caught signal is 10
The caught signal is 12
Generating the signal 1
Generating the signal 2
Generating the signal 1
Generating the signal 1
Generating the signal 2
Generating the signal 2
Generating the signal 2
Generating the signal 2
Generating the signal 2
Generating the signal 1
The caught signal is 12
The caught signal is 10
The caught signal is 10
Generating the signal 2
Generating the signal 1
Generating the signal 1
Generating the signal 2
Generating the signal 1
Generating the signal 2
Generating the signal 2
Generating the signal 2
Generating the signal 1
Generating the signal 2
Generating the signal 1
Generating the signal 2
Generating the signal 2
The caught signal is 10
Generating the signal 2
Generating the signal 1
Generating the signal 1
As you can see initially, the signal was generated and handled both by my signal handlers and monitoring processes. But later on the signal was generated a lot, but it was not quite processes in the same magnitude as before. Further I could see very less signal processing by the monitoring process
Can anyone please provide some insights. What's going on?

If multiple signals of the same type are pending, Linux by default delivers only one such signal. This is inline with the sigwait documentation:
If prior to the call to sigwait() there are multiple pending instances
of a single signal number, it is implementation-dependent whether upon
successful return there are any remaining pending signals for that
signal number.
So the output of your program depends on the scheduler, if kill is called multiple times and scheduler does not awakes monitoring processes in a mean time, signals of the same types are collapsed into one.
Linux allows to change the default behavior.

Related

Why sleep() in a thread cannot be interrupted by signal in my code?

#include <iostream>
#include <thread>
#include <signal.h>
#include <unistd.h>
void handler(int sig){
std::cout << "handler" << std::endl;
}
void func() {
sleep(100);
perror("sleep err:");
}
int main(void) {
signal(SIGINT, handler);
std::thread t(func);
pthread_kill(t.native_handle(), SIGINT);
perror("kill err:");
t.join();
return 0;
}
If I put sleep() inside main function, and send a signal by pressing ctrl+c, sleep will be interrupted and return immediately with perror() saying it's interrupted.
But with the code above, the "handler" in handler function will be printed, but sleep will not return and the program keeps running. The output of this program is:
kill err:: Success
handler
And if I replace sleep() with recvfrom(), recvfrom() will not be interrupted even it's inside the main thread.
#include <vector>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
void SigHandler(int sig){
std::cout << "handler" << std::endl;
}
int main(void) {
signal(SIGINT, SigHandler);
int bind_fd_;
if ((bind_fd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cout << "socket creation failed " << strerror(errno) << std::endl;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(12345);
if (bind(bind_fd_, reinterpret_cast<const struct sockaddr *>(&servaddr),
sizeof(servaddr)) < 0) {
std::cout << "socket bind failed " << strerror(errno) << std::endl;
}
struct sockaddr_in cliaddr;
socklen_t cliaddr_len = sizeof(cliaddr);
std::vector<char> buffer(10*1024*1024,0);
std::cout << "Wait for new request"<< std::endl;
int n = 0;
while (n == 0) {
std::cout << "before recvfrom" << std::endl;
n = recvfrom(bind_fd_, buffer.data(), buffer.size(), 0,
reinterpret_cast<struct sockaddr *>(&cliaddr), &cliaddr_len);
// sleep(100);
perror("recvfrom err: ");
std::cout << "recv " << n << " bytes from " << cliaddr.sin_port<< std::endl;
}
}
I don't know what is wrong with my code, hoping your help, thanks
At the time you direct the signal to the thread, that thread has not yet proceeded far enough to block in sleep(). Chances are that it has not even been scheduled for the first time. Change the code to something like
std::thread t(func);
sleep(5); // give t enough time to arrive in sleep()
pthread_kill(t.native_handle(), SIGINT);
and you'll see what you expect.
Note that using signals in a multithreaded program is not usually a good idea because certain aspects are undefined/not-so-clearly defined.
Note also that it is not correct to use iostreams inside a signal handler. Signal handlers run in a context where pretty much nothing is safe to do, much like an interrupt service routine on bare metal. See here for a thorough explanation of that matter.

problem with understanding example of code from Boost.Fiber library

I am currently trying (in order to learn) to understand example of code from the Boost.Fiber library : https://www.boost.org/doc/libs/1_71_0/libs/fiber/examples/work_sharing.cpp
// Copyright Nat Goodspeed + Oliver Kowalke 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <deque>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include <boost/assert.hpp>
#include <boost/fiber/all.hpp>
#include <boost/fiber/detail/thread_barrier.hpp>
static std::size_t fiber_count{ 0 };
static std::mutex mtx_count{};
static boost::fibers::condition_variable_any cnd_count{};
typedef std::unique_lock< std::mutex > lock_type;
/*****************************************************************************
* example fiber function
*****************************************************************************/
//[fiber_fn_ws
void whatevah( char me) {
try {
std::thread::id my_thread = std::this_thread::get_id(); /**< get ID of initial thread >*/
{
std::ostringstream buffer;
buffer << "fiber " << me << " started on thread " << my_thread << '\n';
std::cout << buffer.str() << std::flush;
}
for ( unsigned i = 0; i < 10; ++i) { /**< loop ten times >*/
boost::this_fiber::yield(); /**< yield to other fibers >*/
std::thread::id new_thread = std::this_thread::get_id(); /**< get ID of current thread >*/
if ( new_thread != my_thread) { /**< test if fiber was migrated to another thread >*/
my_thread = new_thread;
std::ostringstream buffer;
buffer << "fiber " << me << " switched to thread " << my_thread << '\n';
std::cout << buffer.str() << std::flush;
}
}
} catch ( ... ) {
}
lock_type lk( mtx_count);
if ( 0 == --fiber_count) { /**< Decrement fiber counter for each completed fiber. >*/
lk.unlock();
cnd_count.notify_all(); /**< Notify all fibers waiting on `cnd_count`. >*/
}
}
//]
/*****************************************************************************
* example thread function
*****************************************************************************/
//[thread_fn_ws
void thread( boost::fibers::detail::thread_barrier * b) {
std::ostringstream buffer;
buffer << "thread started " << std::this_thread::get_id() << std::endl;
std::cout << buffer.str() << std::flush;
boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >(); /**<
Install the scheduling algorithm `boost::fibers::algo::shared_work` in order to
join the work sharing.
>*/
b->wait(); /**< sync with other threads: allow them to start processing >*/
lock_type lk( mtx_count);
cnd_count.wait( lk, [](){ return 0 == fiber_count; } ); /**<
Suspend main fiber and resume worker fibers in the meanwhile.
Main fiber gets resumed (e.g returns from `condition_variable_any::wait()`)
if all worker fibers are complete.
>*/
BOOST_ASSERT( 0 == fiber_count);
}
//]
/*****************************************************************************
* main()
*****************************************************************************/
int main( int argc, char *argv[]) {
std::cout << "main thread started " << std::this_thread::get_id() << std::endl;
//[main_ws
boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >(); /*<
Install the scheduling algorithm `boost::fibers::algo::shared_work` in the main thread
too, so each new fiber gets launched into the shared pool.
>*/
for ( char c : std::string("abcdefghijklmnopqrstuvwxyz")) { /*<
Launch a number of worker fibers; each worker fiber picks up a character
that is passed as parameter to fiber-function `whatevah`.
Each worker fiber gets detached.
>*/
boost::fibers::fiber([c](){ whatevah( c); }).detach();
++fiber_count; /*< Increment fiber counter for each new fiber. >*/
}
boost::fibers::detail::thread_barrier b( 4);
std::thread threads[] = { /*<
Launch a couple of threads that join the work sharing.
>*/
std::thread( thread, & b),
std::thread( thread, & b),
std::thread( thread, & b)
};
b.wait(); /*< sync with other threads: allow them to start processing >*/
{
lock_type/*< `lock_type` is typedef'ed as __unique_lock__< [#http://en.cppreference.com/w/cpp/thread/mutex `std::mutex`] > >*/ lk( mtx_count);
cnd_count.wait( lk, [](){ return 0 == fiber_count; } ); /*<
Suspend main fiber and resume worker fibers in the meanwhile.
Main fiber gets resumed (e.g returns from `condition_variable_any::wait()`)
if all worker fibers are complete.
>*/
} /*<
Releasing lock of mtx_count is required before joining the threads, otherwise
the other threads would be blocked inside condition_variable::wait() and
would never return (deadlock).
>*/
BOOST_ASSERT( 0 == fiber_count);
for ( std::thread & t : threads) { /*< wait for threads to terminate >*/
t.join();
}
//]
std::cout << "done." << std::endl;
return EXIT_SUCCESS;
}
I have a problem with to understand how the fibers can continue their execution, in the different thread when all the thread are waiting for the notify_all(), because when all the thread are blocking because of the wait function : cnd_count.wait( lk, [](){ return 0 == fiber_count; } ).
So if there are all blocked by the wait function, how is it possible that the fibers continue to execute, I thought that the fibers were executed by the thread with the help of their own scheduling manager. I have read that the threads are just execution unit that the fibers use in order to run their own callable or function in their. So why all the fibers continue to run when all the threads are blocking ?
See: https://www.boost.org/doc/libs/1_71_0/libs/fiber/doc/html/fiber/scheduling.html
Whenever a thread is suspended the scheduler run the next ready fiber, so whatevah got executed when cnd_count.wait is called.
You can try removing the line boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >(); on thread_fn_ws, and you will find all the fiber are ran in the main thread, because scheduler is not installed and thus fiber won't be ran.

Is there an alternative way to sync

Is there an alternative way to be sure that the threads are ready to recieve the broadcast signal. I want to replace the Sleep(1) function in main.
#include <iostream>
#include <pthread.h>
#define NUM 4
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_t tid[NUM];
void *threads(void *arg){
int tid = (int)arg;
while(true){
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
//do some work
cout<<"Thread: "<<tid<<endl;;
pthread_mutex_unlock(&mutex);
}
}
int main(){
for(int i=0;i<NUM;i++){
pthread_create(&tid[i],NULL,threads,(void*)i);
}
Sleep(1);
pthread_cond_broadcast(&cond);
Sleep(1);
pthread_cond_broadcast(&cond);
Sleep(1);
pthread_cond_broadcast(&cond);
return 0;
}
I tried memory barriers before pthread_cond_wait and i thought of using an counter, but nothing worked for me yet.
Condition variables are usually connected to a predicate. In the other threads, check if predicate is already fulfilled (check while holding the mutex protecting the predicate), if so, do not wait on the condition variable. In main, acquire mutex, change predicate while holding the mutex. Then release mutex and signal or broadcast on the condvar. Here is a similar question:
Synchronisation before pthread_cond_broadcast
Here is some example code:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#define NUM 4
#define SIZE 256
using std::cout;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_t tid[NUM];
int work_available;
void *threads(void *arg)
{
int tid = *((int*)arg);
while (1) {
pthread_mutex_lock(&mutex);
while (work_available == 0) {
// While loop since cond_wait can have spurious wakeups.
pthread_cond_wait(&cond, &mutex);
cout << "Worker " << tid << " woke up...\n";
cout << "Work available: " << work_available << '\n';
}
if (work_available == -1) {
cout << "Worker " << tid << " quitting\n";
pthread_mutex_unlock(&mutex); // Easy to forget, better to use C++11 RAII mutexes.
break;
}
assert(work_available > 0);
work_available--;
cout << "Worker " << tid << " took one item of work\n";
pthread_mutex_unlock(&mutex);
//do some work
sleep(2); // simulated work
pthread_mutex_lock(&mutex);
cout << "Worker " << tid << " done with one item of work.\n";
pthread_mutex_unlock(&mutex);
}
}
int main()
{
work_available = 0;
int args[NUM];
for (int i=0; i<NUM; i++) {
args[i] = i;
pthread_create(&tid[i], NULL, threads, (void*)&args[i]);
}
const int MAX_TIME = 10;
for (int i = 0; i < MAX_TIME; i++)
{
pthread_mutex_lock(&mutex);
work_available++;
cout << "Main thread, work available: " << work_available << '\n';
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
sleep(1);
}
pthread_mutex_lock(&mutex);
cout << "Main signalling threads to quit\n";
work_available = -1;
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
for (int i = 0; i < NUM; i++)
{
pthread_join(tid[i], NULL);
}
return 0;
}

Thread running between fork and exec blocks other thread read

While studying the possibility of improving Recoll performance by using vfork() instead of fork(), I've encountered a fork() issue which I can't explain.
Recoll repeatedly execs external commands to translate files, so that's what the sample program does: it starts threads which repeatedly execute "ls" and read back the output.
The following problem is not a "real" one, in the sense that an actual program would not do what triggers the issue. I just stumbled on it while having a look at what threads were stopped or not between fork()/vfork() and exec().
When I have one of the threads busy-looping between fork() and exec(), the other thread never completes the data reading: the last read(), which should indicate eof, is blocked forever or until the other thread's looping ends (at which point everything resumes normally, which you can see by replacing the infinite loop with one which completes). While read() is blocked, the "ls" command has exited (ps shows <defunct>, a zombie).
There is a random aspect to the issue, but the sample program "succeeds" most of the time. I tested with Linux kernels 3.2.0 (Debian), 3.13.0 (Ubuntu) and 3.19 (Ubuntu). Works on a VM, but you need at least 2 procs, I could not make it work with one processor.
Here follows the sample program, I can't see what I'm doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <iostream>
using namespace std;
struct thread_arg {
int tnum;
int loopcount;
const char *cmd;
};
void* task(void *rarg)
{
struct thread_arg *arg = (struct thread_arg *)rarg;
const char *cmd = arg->cmd;
for (int i = 0; i < arg->loopcount; i++) {
pid_t pid;
int pipefd[2];
if (pipe(pipefd)) {
perror("pipe");
exit(1);
}
pid = fork();
if (pid) {
cerr << "Thread " << arg->tnum << " parent " << endl;
if (pid < 0) {
perror("fork");
exit(1);
}
} else {
// Child code. Either exec ls or loop (thread 1)
if (arg->tnum == 1) {
cerr << "Thread " << arg->tnum << " looping" <<endl;
for (;;);
//for (int cc = 0; cc < 1000 * 1000 * 1000; cc++);
} else {
cerr << "Thread " << arg->tnum << " child" <<endl;
}
close(pipefd[0]);
if (pipefd[1] != 1) {
dup2(pipefd[1], 1);
close(pipefd[1]);
}
cerr << "Thread " << arg->tnum << " child calling exec" <<
endl;
execlp(cmd, cmd, NULL);
perror("execlp");
_exit(255);
}
// Parent closes write side of pipe
close(pipefd[1]);
int ntot = 0, nread;
char buf[1000];
while ((nread = read(pipefd[0], buf, 1000)) > 0) {
ntot += nread;
cerr << "Thread " << arg->tnum << " nread " << nread << endl;
}
cerr << "Total " << ntot << endl;
close(pipefd[0]);
int status;
cerr << "Thread " << arg->tnum << " waiting for process " << pid
<< endl;
if (waitpid(pid, &status, 0) != -1) {
if (status) {
cerr << "Child exited with status " << status << endl;
}
} else {
perror("waitpid");
}
}
return 0;
}
int main(int, char **)
{
int loopcount = 5;
const char *cmd = "ls";
cerr << "cmd [" << cmd << "]" << " loopcount " << loopcount << endl;
const int nthreads = 2;
pthread_t threads[nthreads];
for (int i = 0; i < nthreads; i++) {
struct thread_arg *arg = new struct thread_arg;
arg->tnum = i;
arg->loopcount = loopcount;
arg->cmd = cmd;
int err;
if ((err = pthread_create(&threads[i], 0, task, arg))) {
cerr << "pthread_create failed, err " << err << endl;
exit(1);
}
}
void *status;
for (int i = 0; i < nthreads; i++) {
pthread_join(threads[i], &status);
if (status) {
cerr << "pthread_join: " << status << endl;
exit(1);
}
}
}
What's happening is that your pipes are getting inherited by both child processes instead of just one.
What you want to do is:
Create pipe with 2 ends
fork(), child inherits both ends of the pipe
child closes the read end, parent closes the write end
...so that the child ends up with just one end of one pipe, which is dup2()'ed to stdout.
But your threads race with each other, so what can happen is this:
Thread 1 creates pipe with 2 ends
Thread 0 creates pipe with 2 ends
Thread 1 fork()s. The child process has inherited 4 file descriptors, not 2!
Thread 1's child closes the read end of the pipe that thread 1 opened, but it keeps a reference to the read end and write end of thread 0's pipe too.
Later, thread 0 waits forever because it never gets an EOF on the pipe it is reading because the write end of that pipe is still held open by thread 1's child.
You will need to define a critical section that starts before pipe(), encloses the fork(), and ends after close() in the parent, and enter that critical section from only one thread at a time using a mutex.

pthread_exit in signal handler causes segmentation fault

The program below sets SIG_ALRM handler for the whole process, creates a thread, sends SIG_ALRM signal to new created thread.
In SIG_ALRM handler pthread_exit is called.
The result - segmentation fault.
If you sleep before sending signal - OK.
It looks like new thread has not been started at the moment of pthread_exit.
I tried to locate segmentation fault with gdb but couldn't reproduce the crash with gdb.
What causes segmentation fault?
Thanks!
#include <signal.h>
#include <pthread.h>
#include <iostream>
#include <cassert>
using namespace std;
void* threadFunc(void* arg) {
cout << "thread: started. sleeping..: " << pthread_self() << endl;
sleep(10);
cout << "thread: exit" << endl;
return NULL;
}
void alrm_handler(int signo) {
cout << "alrm_handler: " << pthread_self() << endl;
pthread_exit(NULL); //if comment - no segmentation fault
}
int main() {
cout << "main: " << pthread_self() << endl;
struct sigaction act;
act.sa_handler = alrm_handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
pthread_t t;
int rc = pthread_create(&t, NULL, threadFunc, NULL);
assert(rc == 0);
// usleep(1000); //if Uncomment - no segmentation fault
rc = pthread_kill(t, SIGALRM);
assert(rc == 0);
pthread_join(t, NULL);
cout << "main: exit" << endl;
return 0;
}
The output:
main: 140130531731232
alrm_handler: 140130504095488
Segmentation fault
pthread_exit is not async-signal-safe. You cannot call it from signal handlers unless you can be sure the signal handler is not interrupting an async-signal-unsafe function. In particular, the time between calling pthread_create and the entry to your new thread's start function must be considered async-signal-unsafe - this is never explicitly spelled out in the standard, but you can think of the new thread as still being "in pthread_create" (which is async-signal-unsafe) if you like.
Give change for thread initialization process to be completed. so just uncomment the below line is the right approach.
usleep(1000);

Resources