Skip to content

Instantly share code, notes, and snippets.

@srsbiz
Created January 11, 2016 22:04
Show Gist options
  • Save srsbiz/0007520542c999f95e1c to your computer and use it in GitHub Desktop.
Save srsbiz/0007520542c999f95e1c to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
pthread_t tid;
typedef struct data
{
char name[10];
int age;
}data;
void sig_func(int sig)
{
pthread_kill(tid,SIGUSR1);
pthread_join(tid,NULL);
signal(SIGUSR1, SIG_IGN);
}
void func(data *p)
{
strcpy(p->name,"rado rado!");
p->age=31;
if(sleep(15) == 0) { // Sleep to catch the signal
fprintf(stderr, "No sig - override\n");
strcpy(p->name,"Mr. SIGNAL");
p->age=1;
}
}
int main()
{
pthread_attr_t attr;
data d;
data *ptr = &d;
int sleep_time = 20;
signal(SIGUSR1,sig_func); // Register signal handler before going multithread
pthread_attr_init(&attr);
pthread_create(&tid,&attr,(void*)func,ptr);
while((sleep_time = sleep(sleep_time)) > 0){
fprintf(stderr, "Still %d seconds to end\n", sleep_time);
signal(SIGUSR1,sig_func);
}
fprintf(stderr, "Name:%s\n",ptr->name);
fprintf(stderr, "Age:%d\n",ptr->age);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment