Skip to content

Instantly share code, notes, and snippets.

@srsbiz srsbiz/watki.c
Created Jan 11, 2016

Embed
What would you like to do?
#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
You can’t perform that action at this time.