Created
January 11, 2016 22:04
-
-
Save srsbiz/0007520542c999f95e1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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