I am trying to find the cause of a segmentation fault error which is happening when I try to generate Signal (^C) when System function is involved.
Also I am trying to understand why I see only one print Thread1 cancelled
and not 3 prints for all three threads? [Corrected the code for this point
]
Below is the code for the testapp.c. This is a kind of stress test app I have made to test my utility program. The utility program is not the actual program which I am using but to demonstrate the use of semaphore used in it. It is executing my utility program using system()
function.
Source code and output is listed below,
/******* testapp.c *****************/
/* gcc testapp.c -o testapp -pthread */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#define SLEEP1 250
#define SLEEP2 250
#define SLEEP3 250
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
static volatile sig_atomic_t isRunning = 1;
void signal_handler(int signal)
{
int rc = 0;
switch (signal)
{
case SIGINT:
case SIGTERM:
case SIGQUIT:
printf("Signal generated, cancelling threads...");
// Graceful shutdown
//isRunning = 0; //commented to see the segmentation fault issue
rc = pthread_cancel(thread1);
if(rc != 0){
printf("signal_handler:pthread_cancel-1 failed - %d (%m)\n", errno);
}
printf("Thread1 cancelled\n");
rc = pthread_cancel(thread2);
if(rc != 0){
printf("signal_handler:pthread_cancel-2 failed - %d (%m)\n", errno);
}
printf("Thread2 cancelled\n");
rc = pthread_cancel(thread3);
if(rc != 0){
printf("signal_handler:pthread_cancel-3 failed - %d (%m)\n", errno);
}
printf("Thread3 cancelled\n");
break;
default:
break;
}
}
void* thread1_worker(void* data)
{
struct timeval timeout;
int ret = 0;
do
{
printf("Requesting cmd1\n");
ret = system("util -c:cmd1 -v 2>> /tmp/stderr.log");
timeout.tv_sec = 0;
timeout.tv_usec = SLEEP1 * 1000; //milliseconds
ret = select(0, NULL, NULL, NULL, &timeout);
}while(isRunning);
printf("Exiting thread1...");
pthread_exit((void*)0);
}
void* thread2_worker(void* data)
{
struct timeval timeout;
int ret = 0;
do
{
printf("Requesting cmd2\n");
ret = system("util -c:cmd2 -v 2>> /tmp/stderr.log");
timeout.tv_sec = 0;
timeout.tv_usec = SLEEP2 * 1000; //milliseconds
int ret = select(0, NULL, NULL, NULL, &timeout);
}while(isRunning);
printf("Exiting thread2...");
pthread_exit((void*)0);
}
void* thread3_worker(void* data)
{
struct timeval timeout;
int ret = 0;
do
{
printf("Requesting cmd3\n");
ret = system("util -c:cmd3 -v 2>> /tmp/stderr.log");
timeout.tv_sec = 0;
timeout.tv_usec = SLEEP3 * 1000; //milliseconds
int ret = select(0, NULL, NULL, NULL, &timeout);
}while(isRunning);
printf("Exiting thread3...");
pthread_exit((void*)0);
}
int main(int argc, char **argv){
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
printf("Starting threads...\n");
pthread_create(&thread1, NULL, thread1_worker, (void*) NULL);
pthread_create(&thread2, NULL, thread2_worker, (void*) NULL);
pthread_create(&thread3, NULL, thread3_worker, (void*) NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
return 0;
}
Output is below, The prints in the square brackets is the actual output of the program and not the util program above,
root@mydevice:~$ testapp
Starting threads...
Requesting cmd1
Requesting cmd2
Requesting cmd3
cmd2:[05 05]
cmd1:[0]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[1]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[2]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[3]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
^Ccmd1:[4]
sh: line 1: 964 Segmentation fault util -c:cmd3 -v 2>> /tmp/stderr.log
Requesting cmd2
cmd2:[05 05]
Requesting cmd1
Requesting cmd3
cmd1:[5]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
cmd2:[05 05]
Requesting cmd1
Requesting cmd3
cmd3:[00 00 00 00 00 ]
Requesting cmd2
cmd1:[84213760]
Requesting cmd3
^Ccmd2:[00 00] -----------------------------------------------> Signal
cmd3:[00 00 00 00 00 ]
Requesting cmd1
cmd1:[7]
Requesting cmd2
Requesting cmd3
cmd2:[05 05]
cmd3:[00 00 00 00 00 ]
Requesting cmd1
cmd1:[8]
^CSignal generated, cancelling threads...Thread1 cancelled ---->Signal
Segmentation fault
root@mydevice:~$