I tried to install SIGINT handler for the child thread in the code below. I expect the child thread to print hello when it receives SIGINT from the parent process. However, nothing comes out and the program exits immediately.
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
typedef struct proxy_node_t{
pthread_t sub_thread;
pthread_t p_self;
}proxy_node;
proxy_node* proxy;
static void proxy_singnal_handler(){
printf("Hello\n");
return;
}
static void* t_consensus(void *arg){
signal(SIGINT,proxy_singnal_handler);
sleep(1);
return NULL;
}
int main(int argc, char **argv)
{
proxy = (proxy_node*)malloc(sizeof(proxy_node));
proxy->p_self = pthread_self();
pthread_create(&proxy->sub_thread,NULL,t_consensus,NULL);
pthread_kill(proxy->sub_thread,SIGINT);
sleep(1);
return 0;
}
There are several problems.
1) The signal handler signature is not correct. It should take an int whereas you define it with no parameter.
i.e.
static void proxy_singnal_handler(){
should be
static void proxy_singnal_handler(int sig){
2) You can't call functions that are not async-signal-safe from a signal handler (printf() in your case). See signal(7) for details. You can instead use write(2) to print that message:
printf("Hello\n");
can be:
write(1, "Hello\n", 6);
3) When main thread sends SIGINT, the t_consensus thread might not have even started. So, signal() may not have been installed yet. So, you need to make sure signal() is installed before pthread_kill() could send SIGINT.
Just to demonstrate it, I have added some sleep calls (see comments in the code). But please note that sleep() is not a good way to synchronization and if you intend to adapt this example then you should use a conditional variable instead.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
typedef struct proxy_node_t{
pthread_t sub_thread;
pthread_t p_self;
}proxy_node;
proxy_node* proxy;
static void proxy_singnal_handler(int sig){
write(1, "Hello\n", 6);
return;
}
static void* t_consensus(void *arg){
signal(SIGINT,proxy_singnal_handler);
while(1); /* infinite loop */
return NULL;
}
int main(int argc, char **argv)
{
proxy = (proxy_node*)malloc(sizeof(proxy_node));
proxy->p_self = pthread_self();
pthread_create(&proxy->sub_thread,NULL,t_consensus,NULL);
sleep(2); /* delay to ensure signal handler is installed */
pthread_kill(proxy->sub_thread,SIGINT);
sleep(2); /* delay to ensure signal gets executed before the process exits */
return 0;
}
Since #Maxim Egorushkin wanted to see a solution that exits gracefully and uses semaphores:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <semaphore.h>
typedef struct proxy_node_t{
pthread_t sub_thread;
pthread_t p_self;
}proxy_node;
proxy_node* proxy;
static void proxy_singnal_handler(int sig)
{
write(1, "Hello\n", 6);
return;
}
sem_t sema1;
sem_t sema2;
static void* t_consensus(void *arg)
{
signal(SIGINT,proxy_singnal_handler);
sem_post(&sema1); /*notify main thread that signal-handler is installed*/
sem_wait(&sema2); /*ensure thread exists to be pthread_kill'ed, could use sigsuspend instead*/
return NULL;
}
int main(int argc, char **argv)
{
sem_init(&sema1, 0, 0);
sem_init(&sema2, 0, 0);
proxy = (proxy_node*)malloc(sizeof(proxy_node));
proxy->p_self = pthread_self();
pthread_create(&proxy->sub_thread,NULL,t_consensus,NULL);
sem_wait(&sema1); /*wait until the thread has installed the signal handler*/
pthread_kill(proxy->sub_thread,SIGINT);
sem_post(&sema2); /*not strictly necessary if the thread uses sigsuspend*/
pthread_join(proxy->sub_thread, NULL);
free(proxy); /*not strictly necessary before exiting*/
sem_destroy(&sema1);
sem_destroy(&sema2);
return 0;
}
Related
I'm trying to do something simple with alarms, however the printf is never executing after I do the alarm, why's that?
#include <stdio.h>
#include <signal.h>
int main() {
alarm(3);
printf("Hello...\n");
alarm(6);
while(1);
printf("Hello2\n");
}
I want hello and hello2 to be printed, only hello is being printed for now
You didn't specify a handler for SIGALRM, and its default behavior (per man 7 signal) is to terminate the program. Even if you did specify a handler, after it ran, you'd still be in the while(1) loop.
Here's how you'd modify your program to fix both of those problems:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
volatile sig_atomic_t got_sigalrm = 0;
void handle_sigalrm(int signum) {
got_sigalrm = 1;
}
int main() {
struct sigaction act = { .sa_handler = handle_sigalrm };
sigaction(SIGALRM, &act, NULL);
alarm(3);
printf("Hello...\n");
alarm(6);
while(!got_sigalrm);
printf("Hello2\n");
}
I've use pthread for multithreded program and I've got the following situation.
When I run the code without sleep command it causes error at run time, and when I add the sleep command program runs as expected.
With sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
sleep(12);
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Without sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Error:
futex(0x559c3d3df0a0, FUTEX_WAIT_PRIVATE, 2, NULLHello
) = -1 EAGAIN (Resource temporarily unavailable)
strace shows results of system calls. Pthread functions in Linux are not system calls, they are libc functions implemented on top of (potentilally non-trivial) system calls. You are not interested in what internal system calls return. A single successfull call to ptread_mutex_lock may and sometimes does entail several failing syscalls.
In particular, pthread_mutex_lock cannot possibly result in EAGAIN in this program because there are no attempts to lock the mutex recursively, and the default mutex is not recursive in Linux anyway. The FUTEX_WAIT_PRIVATE syscall that pthread_mutex_lock uses internally can and will result in EAGAIN. This is of no interest whatsoever to the application programmer.
This question already has an answer here:
Thread created (detached) never executed
(1 answer)
Closed 9 years ago.
I'm trying to compile and run the following code using Detached Threads in C Linux. The thing is that I want every thread to show me the corresponding printf from the handler *idThreadMethod and it doesn't show me anything! I tried using a printf before calling the pthread_create function and it shows it, but the problem should be inside the *idThreadMethod (handler function). The code:
//gcc detachedThreads.c -lpthread -o p
//./p 4
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int i;
void *idThreadMethod(void *args)
{
int pid;
pid = *((int *)args);
printf("\nI'm The Detached Thread %d\n", i);
printf("\nMy PID is: %d\n", pid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int quantityThreads, returnThread, pid;
pthread_t idThread[15];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if(argc-1 < 1)
{
printf("\nSome arguments are missing\n");
return EXIT_FAILURE;
}
quantityThreads = atoi(argv[1]);
pid=getpid();
int *it = &pid;
for(i=0;i<quantityThreads;i++)
{
returnThread = pthread_create(&idThread[i],&attr,idThreadMethod,it);
if(returnThread == -1)
{
printf("\nThere is an error trying to create the thread\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
What could I do in order to show the printf messages from the *idThreadMethod function?
In main, use pthread_exit. Your main is exiting and thus the rest of your threads die, detached or not.
They're not showing because your main thread is exiting before they even get a chance to printf. As your threads are detached, you cannot use pthread_join to wait for them to do their thing, so you'll need some other form of synchronisation.
Seems to me, you don't really want detached threads at all....
I am learning to use semaphores and below is a small scenario which I've tried to implement. Its behaving weird in a way. After sem_wait() gets unblocked first time, its not getting blocked again and keeps on looping, not getting why. Is this the right way or right scenario to use semaphore?
EDIT: I just realized that if I uncomment the sleep after sem_post, it works fine. .Reason being it was repeatedly doing sem_post() before thread could do coin=0 I believe. But is it right to use sleep this way with semaphores. I believe this would be considered a bad practice?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define MAX_MSG_LEN 256
sem_t sem1;
sem_t sem2;
int coin=0;
void *thrdFun1(void *arg);
void *thrdFun2(void *arg);
void toggleCase(char *buf, int cnt);
int main()
{
pthread_t thrd1;
char argmsg1[] = "Thread1: Waiting to deliver\n";
int thNum;
int res;
res = sem_init(&sem1, 0,0);
// res = sem_init(&sem2, 0,0);
res = pthread_create(&thrd1, NULL, thrdFun1, argmsg1);
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1);
// sleep(1);
}
}
return 0;
}
void *thrdFun1(void *arg)
{
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1);
printf("Delivered...\n");
coin=0;
sleep(1);
}
}
Semaphores are used to control Critical-Section Access. In this case, critical section would be the output shell. The thread may or may not start promptly when the pthread_create() is called. Also, sem_wait() will decrease the value of sem1 with each call. Thus, when you include sleep(1) in the thrdFun1 function, there may be undefined behaviour :)
You need to remove sleep(1); from function thrdFun1. After this there will not be any need of sleep(1) in main.
You realized it right, it is repeatedly doing sem_post() before thread could do coin=0 when removing all sleeps.
To solve this you could use second semaphore (you already tried it seems) like below,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define MAX_MSG_LEN 256
sem_t sem1;
sem_t sem2;
int coin=0;
void *thrdFun1(void *arg);
void *thrdFun2(void *arg);
void toggleCase(char *buf, int cnt);
int main()
{
pthread_t thrd1;
char argmsg1[] = "Thread1: Waiting to deliver\n";
int thNum;
int res;
res = sem_init(&sem1, 0,0);
res = sem_init(&sem2, 0,0);
res = pthread_create(&thrd1, NULL, thrdFun1, argmsg1);
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1); // Coin is spun
sem_wait(&sem2); // Wait till it is caught
}
}
return 0;
}
void *thrdFun1(void *arg)
{
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1); // Wait to spin the coin
coin=0;
sem_post(&sem2); // inform as caught
printf("Delivered...\n");
}
}
there is a chance that "Thread1: Waiting to deliver" this string can get printed many times.
What you are trying to achieve is looks like producer-consumer problem.
You required two semaphore to achive this.
main function:
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1);
sem_wait(&sem2)
}
}
in thread function
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1);
printf("Delivered...\n");
coin=0;
sem_post(&sem2)
}
#include <stdio.h>
#include <signal.h>
void ALARMhandler(int sig)
{
signal(SIGALRM, SIG_IGN); /* ignore this signal */
printf("Hello");
signal(SIGALRM, ALARMhandler); /* reinstall the handler */
}
int main(int argc, char *argv[])
{
alarm(2); /* set alarm clock */
while (1)
;
printf("All done");
}
I expect the program to print "hello" after 2 seconds, but instead the output is "zsh: alarm ./a.out"
Any idea what is going on?
You're forgetting to set the alarm handler initially. Change the start of main() like:
int main(int argc, char *argv[])
{
signal(SIGALRM, ALARMhandler);
...
Also, the signal handler will probably print nothing. That's because the C library caches output until it sees an end of line. So:
void ALARMhandler(int sig)
{
signal(SIGALRM, SIG_IGN); /* ignore this signal */
printf("Hello\n");
signal(SIGALRM, ALARMhandler); /* reinstall the handler */
}
For a real-world program, printing from a signal handler is not very safe. A signal handler should do as little as it can, preferably only setting a flag here or there. And the flag should be declared volatile.
You're not setting the handler in your main function.
Before you do alarm(2), put the signal(SIGALRM, ALARMhandler); in your main.
It should work then.
Note that your "All Done" will never be printed, because you'll stay in the while(1) loop after the signal processor has run. If you want the loop to be broken, you'll need to have a flag that the signal handler changes.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
/* number of times the handle will run: */
volatile int breakflag = 3;
void handle(int sig) {
printf("Hello\n");
--breakflag;
alarm(1);
}
int main() {
signal(SIGALRM, handle);
alarm(1);
while(breakflag) { sleep(1); }
printf("done\n");
return 0;
}
You are not installing the signal handler first.
You need to tell the system that you want to handle the signal before actually receiving it, so you need to call signal() from main before the signal comes.
int main(int argc, char *argv[])
{
signal(SIGALRM, ALARMhandler); /* install the handler */
alarm(2); /* set alarm clock */
while (1);
}
Andomar is rigth. I test this and,
version 1 prints (every second):
Hi...
Hi...
Hi...
Hi...
BYE
Hi...
...
version 2 prints (every five seconds):
Hi...Hi...Hi...Hi...BYE
Hi...Hi...Hi...Hi...BYE
...
So the code is:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
# define T 5
int flag = T;
void sigalrm_handler(int);
int main(void)
{
signal(SIGALRM, sigalrm_handler);
alarm(1);
while (1);
}
void sigalrm_handler(int sig)
{
if(--flag){
printf("Hi...\n"); /*version 1*/
/*printf("Hi...");*/ /*version 2*/
}else{
printf("BYE\n");
flag=T;
}
alarm(1);
}