C, timer_settime, disarm timer and overwrite associated data? - c

I have to do for University a project about UDP, where i have to guarantee reliable communication; for packets, i want use timer_gettime() and timer_Settime() functions, because i can queue signals and i can associate to them a timer; in particular, struct sigevent has a field which union sigval where i can pass value to handler when signal arrived; I would like to take advantage of this passing to handler number of packets for which timer expired; I have a problem, and I've done a simple program to verify this; when I start timer, i can disarm it setting it_value of struct sigevent to 0; but data doesn't change; if I send 100 signal, header receives only data of first signal. This is my code:
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int d;
void err_exit(char* str)
{
perror(str);
exit(EXIT_FAILURE);
}
void sighandler(int sig, siginfo_t *si, void *uc)
{
(void) sig;
(void) uc;
d = si->si_value.sival_int;
}
void handle_signal(struct sigaction* sa)
{
sa->sa_flags = SA_SIGINFO;
sa->sa_sigaction = sighandler;
sigemptyset(&sa->sa_mask);
if (sigaction(SIGRTMAX,sa,NULL) == -1)
err_exit("sigaction");
}
void create_timer(struct sigevent* sev,timer_t* timer_id,int i)
{
union sigval s;
s.sival_int = i;
printf("value: %d\n",i);
sev->sigev_notify = SIGEV_SIGNAL;
sev->sigev_signo = SIGRTMAX;
sev->sigev_value = s;
timer_create(CLOCK_REALTIME,sev,timer_id);
}
void set_timer(timer_t timer_id,struct itimerspec* ts)
{
if(ts == NULL)
printf("itimerspec null\n");
if (timer_settime(timer_id, 0, ts, NULL) == -1){
printf("errno code: %d\n",errno);
err_exit("timer_settime");
}
}
void initialize_timerspec(struct itimerspec* ts)
{
ts->it_value.tv_sec = 2;
ts->it_value.tv_nsec = 5;
ts->it_interval.tv_sec = 0;
ts->it_interval.tv_nsec = 0;
}
void reset_timer(timer_t timer_id, struct itimerspec* ts)
{
ts->it_value.tv_sec = 0;
ts->it_value.tv_nsec = 0;
ts->it_interval.tv_sec = 0;
ts->it_interval.tv_nsec = 0;
if (timer_settime(timer_id, 0, ts, NULL) == -1){
printf("errno code: %d\n",errno);
err_exit("timer_settime");
}
}
int main()
{
struct sigaction sa;
struct itimerspec ts[2];
struct sigevent sev[2];
timer_t timer_id[2];
handle_signal(&sa);
create_timer(sev,timer_id,0);
initialize_timerspec(ts);
set_timer(timer_id,ts);
reset_timer(timer_id,ts);
create_timer(sev + 1,timer_id + 1,1);
initialize_timerspec(ts + 1);
set_timer(timer_id,ts + 1);
printf("id1: %ju id2: %ju\n",timer_id[0],timer_id[1]);
sleep(10);
printf("d = %d\n",d);
exit(EXIT_SUCCESS);
}
I disarm first timer, and send another signal; but handler receives data associated to first signal, because it prints 0. Is there a way to send to overwrite data, sending to handler data of second signal(in this case 1)?

Related

How to implement 2 timers in linux

I m trying to set the flag variable on(working with raspbery pi. I need pin on) for 500 useconds(micro seconds) and flag off for 300 useconds continuously(infinitely until I stop it). I thought of implementing it using 2 timers.
Now In this program i have written for 5 seconds and 3 seconds.
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
struct sigaction sa;
struct itimerval timer1,timer2;
int count=1;
void timer_handler (int signum)
{
if(count++%2==1)
printf("High\n"); //flag=1
else
printf("Low\n"); //flag=0
}
int main ()
{
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sa.sa_flags = SA_RESTART;
sigaction (SIGALRM, &sa, NULL);
int i=0;
while(1){
scanf(" %d",&i);
if(i==1){ // I m starting 2 timers here
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 1;
timer1.it_interval.tv_sec = 8; //5+3
timer1.it_interval.tv_usec = 0;
timer2.it_value.tv_sec = 5;
timer2.it_value.tv_usec = 0;
timer2.it_interval.tv_sec = 8;
timer2.it_interval.tv_usec = 0;
setitimer (ITIMER_REAL, &timer1, NULL);
setitimer (ITIMER_REAL, &timer2, NULL);
}
else if(i==2) // I m stopping here
{
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 0;
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 0;
timer2.it_value.tv_sec = 0;
timer2.it_value.tv_usec = 0;
timer2.it_interval.tv_sec = 0;
timer2.it_interval.tv_usec = 0;
setitimer (ITIMER_REAL, &timer1, NULL); // 1st timer on
setitimer (ITIMER_REAL, &timer2, NULL); //2nd timer on
}
}
}
This is code I have written.
what actually happening is the second timer is running and first timer is not running. I think its overwritten.
Ps. I dont want to use sleep function as it takes more time. I m using timers as the resolution is microsecond.
1.How do I do this using two timers?
2.Is there any better method to do this task?
There is only one ITIMER_REAL, so you must create virtual timers yourself. A simple and reliable possibility if you don't need microsecond precision, is to use a periodic timer with a small interval and implement your virtual timers on top of that (so every "tick" from your periodic timer will decrement your virtual timers).
Following an example how you could implement it:
vtimer.h
#ifndef VTIMER_H
#define VTIMER_H
typedef void (vtimer_timeout)(void *arg);
typedef struct vtimer
{
int msec;
int periodic;
int current;
vtimer_timeout *timeout;
} vtimer;
#define vtimer_init(m, p, cb) { \
.msec=(m), .periodic=(p), .current=0, .timeout=cb}
void vtimer_start(vtimer *self, void *timeoutArg);
void vtimer_stop(vtimer *self);
// call this periodically, e.g. after each interrupted library call:
void vtimer_dispatch();
#endif
vtimer.c
#define _POSIX_C_SOURCE 200101L
#include "vtimer.h"
#include <stddef.h>
#include <signal.h>
#include <sys/time.h>
#define NUM_TIMERS 8
static vtimer *timers[NUM_TIMERS] = {0};
static void *timoutArgs[NUM_TIMERS] = {0};
static size_t ntimers = 0;
static volatile sig_atomic_t ticks = 0;
static void tickhandler(int signum)
{
(void)signum;
++ticks;
}
static struct sigaction timerAction = {.sa_handler = tickhandler};
static struct sigaction defaultAction;
static struct itimerval tickTimerval = {{0, 1000}, {0, 1000}};
static struct itimerval disableTimerval = {{0,0},{0,0}};
void vtimer_start(vtimer *self, void *timeoutArg)
{
int found = 0;
for (size_t idx = 0; idx < NUM_TIMERS; ++idx)
{
if (timers[idx] == self)
{
found = 1;
break;
}
}
if (!found)
{
if (ntimers == NUM_TIMERS) return; // or maybe return error
if (!ntimers++)
{
// only start the "ticking" timer when necessary
sigaction(SIGALRM, &timerAction, &defaultAction);
setitimer(ITIMER_REAL, &tickTimerval, 0);
}
for (size_t idx = 0; idx < NUM_TIMERS; ++idx)
{
if (!timers[idx])
{
timers[idx] = self;
timoutArgs[idx] = timeoutArg;
break;
}
}
}
self->current = self->msec;
}
void vtimer_stop(vtimer *self)
{
int found = 0;
for (size_t idx = 0; idx < NUM_TIMERS; ++idx)
{
if (timers[idx] == self)
{
timers[idx] = 0;
found = 1;
break;
}
}
if (found && !--ntimers)
{
// no virtual timers running -> stop ticking timer
setitimer(ITIMER_REAL, &disableTimerval, 0);
sigaction(SIGALRM, &defaultAction, 0);
}
}
void vtimer_dispatch(void)
{
while (ticks)
{
--ticks;
for (size_t idx = 0; idx < NUM_TIMERS; ++idx)
{
if (timers[idx])
{
if (!--(timers[idx]->current))
{
timers[idx]->timeout(timoutArgs[idx]);
if (timers[idx]->periodic)
{
timers[idx]->current = timers[idx]->msec;
}
else vtimer_stop(timers[idx]);
}
}
}
}
}
Example program using this:
#include "vtimer.h"
#include <stdio.h>
#include <errno.h>
static void timer1_timeout(void *arg)
{
(void) arg;
puts("timer 1");
}
static void timer2_timeout(void *arg)
{
(void) arg;
puts("timer 2");
}
int main(void)
{
vtimer timer1 = vtimer_init(5000, 1, timer1_timeout);
vtimer timer2 = vtimer_init(8000, 1, timer2_timeout);
vtimer_start(&timer1, 0);
vtimer_start(&timer2, 0);
for (;;)
{
errno = 0;
int c = getchar();
if (c == EOF && errno != EINTR) break;
if (c == 'q') break;
vtimer_dispatch();
}
vtimer_stop(&timer2);
vtimer_stop(&timer1);
return 0;
}
There are a lot of design decisions on the way (e.g. how frequent your ticks should be (here 1ms), having a fixed number of virtual timers vs a dynamic one, using pointers as "timer handles" or maybe integers, and so on), so think about what you need and try to write your own.

Assignment from incompatible pointer type — how to fix?

Trying to implement signal handlers but receiving the warning:
assignment from incompatible pointer type [enabled by default]
act.sa_sigaction = sigChldHandler;
... Also my professor pointed out.
char * argv[3];
argv[0]= GUESSER_PROGNAME;
argv[2]= NULL;
this allocates memory for 3 char pointer vars, but they do NOT point anywhere in particular." But I am not sure what he means by that.
\file 1
#include "assign2Headers.h"
pid_t answererPid;
pid_t guesserPid;
int shouldRun = 1;
void sigAlrmHandler(int sig)
{
kill(answererPid,TIME_OVER_SIGNAL);
kill(guesserPid,TIME_OVER_SIGNAL);
shouldRun=0;
}
void sigChldHandler(int sig)
{
wait(NULL);
shouldRun=0;
}
int main(void){
struct sigaction act;
memset(&act, '\0', sizeof(struct sigaction));
act.sa_handler = sigAlrmHandler;
sigaction(SIGALRM, &act, NULL);
act.sa_sigaction = sighldHandler;
sigaction(SIGCHLD, &act, NULL);
char line[LINE_LEN];
char * argv[3];
argv[0]= GUESSER_PROGNAME;
argv[2]= NULL;
answererPid = fork();
if(answererPid == 0){
execl(ANSWERER_PROGNAME,ANSWERER_PROGNAME,(char*)NULL);
}
else{
sleep(1);
snprintf(line,LINE_LEN,"%d",answererPid);
guesserPid=fork();
if(guesserPid==0)
{
execl(GUESSER_PROGNAME,GUESSER_PROGNAME,argv[0],line,(char*)NULL);
}
else
{ alarm(NUM_SECONDS);
while(shouldRun)
sleep(1);
sleep(1);
sleep(1);
printf("launcher finished\n");
return (EXIT_SUCCESS);
}
}
}
\file 2
#include "assign2Headers.h"
int shouldRun = 1;
void timeoverhandler(int sig)
{ sleep(1);
printf("\nOh no! The time is up!\n");
printf("guesser finished\n");
shouldRun=0;
exit(EXIT_SUCCESS);
}
void winsignalhandler(int sig)
{
printf("\nCongratulations! You found it!\n");
shouldRun=0;
signal(WIN_SIGNAL,winsignalhandler);
}
void correctsignalhandler(int sig)
{
printf("Yay! That was right!\n");
signal(CORRECT_SIGNAL,correctsignalhandler);
}
void incorrectsignalhandler(int sig)
{
printf("Oops! That was wrong. Please restart from the beginning.\n"
"\nRe-starting from the beginning:\n");
signal(INCORRECT_SIGNAL,incorrectsignalhandler);
}
int main(int argc,char* argv[])
{
pid_t answererPid=atoi(argv[1]);
signal(TIME_OVER_SIGNAL,timeoverhandler);
signal(WIN_SIGNAL,winsignalhandler);
signal(CORRECT_SIGNAL,correctsignalhandler);
signal(INCORRECT_SIGNAL,incorrectsignalhandler);
while(shouldRun)
{ int guess;
printf("What would you like your next guess to be: 0 or 1? ");
scanf("%d",&guess);
if(guess==0)
kill(answererPid,ZERO_SIGNAL);
if(guess==1)
kill(answererPid,ONE_SIGNAL);
sleep(2);
}
printf("guesser finished\n");
return (EXIT_SUCCESS);
}
\file 3
//--- Common standard header files ---//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
////--- Common constants: ---//
#define ZERO_SIGNAL SIGUSR1
#define ONE_SIGNAL SIGUSR2
#define CORRECT_SIGNAL SIGUSR1
#define INCORRECT_SIGNAL SIGUSR2
#define WIN_SIGNAL SIGINT
#define TIME_OVER_SIGNAL SIGTERM
#define GUESSER_PROGNAME "guesser"
#define ANSWERER_PROGNAME "answerer"
#define LINE_LEN 256
#define NUM_SECONDS 30
\file 4
//--- Inclusion of header files ---//
#include "assign2Headers.h"
//--- Definition of constants: ---//
#define PATTERN_LEN 4
//--- Definition of global vars: ---//
int answer;
int numCorrect = 0;
int shouldRun = 1;
//--- Definition of global fncs: ---//
void timeUpHandler (int sig
)
{
shouldRun = 0;
}
void guessHandler (int sig,
siginfo_t* infoPtr,
void* dataPtr
)
{
int toSendBack;
int userBit = (sig == ONE_SIGNAL);
int correctBit = ((answer >> numCorrect) & 0x1);
int isCorrect = (correctBit == userBit);
printf("position %d: userBit %d, correctBit %d\n",
numCorrect,userBit,correctBit
);
if (isCorrect)
{
numCorrect++;
if (numCorrect >= PATTERN_LEN)
toSendBack = WIN_SIGNAL;
else
toSendBack = CORRECT_SIGNAL;
}
else
{
numCorrect = 0;
toSendBack = INCORRECT_SIGNAL;
}
kill(infoPtr->si_pid,toSendBack);
}
int main (int argc,
char* argv[]
)
{
// I. Application validity check:
// II. Run program:
// II.A. Initialize random number generator and choice:
srand(getpid());
answer = rand() % (1 << PATTERN_LEN);
printf("(The answer is %d)\n",answer);
// II.B. Install signal handlers:
struct sigaction act;
memset(&act,'\0',sizeof(act));
act.sa_handler = timeUpHandler;
sigaction(TIME_OVER_SIGNAL,&act,NULL);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = guessHandler;
sigaction(ZERO_SIGNAL,&act,NULL);
sigaction(ONE_SIGNAL,&act,NULL);
// II.C. Hand out while game still active:
while ( (numCorrect < PATTERN_LEN) && shouldRun )
sleep(1);
// III. Finished, return answer:
printf("answerer finished\n");
return(EXIT_SUCCESS);
}
how do I remove this warning and what does he mean by that? Someone help me please.
I guess you are working on Linux. From sigaction manpage :
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
The prototype void sigChldHandler(int sig) does not match with sa_sigaction. Hence the warning.
You can use
sa_handler to set the signal handler function or to specify SIG_IGN/SIG_DFL actions.
sa_sigaction to set the signal handler function - if user needs to access more details like user context and siginfo structure (sender process details, signal type etc). Need to set SA_SIGINFO in the sa_flags for this usage.
For your case, setting sa_handler might be sufficient.
In main():
struct sigaction act;
act.sa_handler = sigChldHandler;
act.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &act, NULL);
Also,
Use separate sigaction structures per signal that you want to set unless you want same handler for them.
Do not mix sigaction() & signal() usages. Stick to one. Maybe, sigaction as it's new & provides more features than signal().
This segment:
char * argv[3];
argv[0]= GUESSER_PROGNAME;
argv[2]= NULL;
Is invalid for argv[0]. Since argv is an array of pointers, you need to make sure those pointers are pointing somewhere before you use them.
This can be achieved with strdup():
argv[0]= strdup(GUESSER_PROGNAME);
Or with malloc()/strcpy():
argv[0]= malloc(strlen(GUESSER_PROGNAME)+1);
strcpy(argv[0], GUESSER_PROGNAME);
Note: malloc() should also be checked, it can return NULL on failure. Any memory allocated on the heap should also be free()'d at the end.
In terms of clarity, you could replace:
#define GUESSER_PROGNAME "guesser"
with:
const char *guesser_progname = "guesser";

libevent http client with request timeout

I am using libevent to get some stats of a web site in certain time intervals. I've based the program on this. The only thing I'm missing is a timeout on the request, preferably in subsecond accuracy.
I've tried a few things, but couldn't get it to work. I'd really appreciate any pointers on this.
This is a very crude example: I've used libevent timers to implement the timeout. This could be further improved by putting the timer in the struct and cancelling it in the callback.
#include <stdio.h>
#include <event.h>
#include <evhttp.h>
#include <stdlib.h>
#include <unistd.h>
struct http_client {
struct evhttp_connection *conn;
struct evhttp_request *req;
bool finished;
};
void _reqhandler(struct evhttp_request *req, void *state) {
struct http_client *hc = (http_client*)state;
hc->finished = true;
if (req == NULL) {
printf("timed out!\n");
} else if (req->response_code == 0) {
printf("connection refused!\n");
} else if (req->response_code != 200) {
printf("error: %u %s\n", req->response_code, req->response_code_line);
} else {
printf("success: %u %s\n", req->response_code, req->response_code_line);
}
event_loopexit(NULL);
}
void timeout_cb(int fd, short event, void *arg) {
struct http_client *hc = (http_client*)arg;
printf("Timed out\n");
if (hc->finished == false){ // Can't cancel request if the callback has already executed
evhttp_cancel_request(hc->req);
}
}
int main(int argc, char *argv[]) {
struct http_client *hc = (struct http_client *)malloc(sizeof(struct http_client));
hc->finished = false;
struct event ev;
struct timeval tv;
tv.tv_sec = 3; // Timeout is set to 3.005 seconds
tv.tv_usec = 5000;
const char *addr = "173.194.39.64"; //google.com
unsigned int port = 80;
event_init();
hc->conn = evhttp_connection_new(addr, port);
evhttp_connection_set_timeout(hc->conn, 5);
hc->req = evhttp_request_new(_reqhandler, (void*)hc);
evhttp_add_header(hc->req->output_headers, "Host", addr);
evhttp_add_header(hc->req->output_headers, "Content-Length", "0");
evhttp_make_request(hc->conn, hc->req, EVHTTP_REQ_GET, "/");
evtimer_set(&ev, timeout_cb, (void*)hc); // Set a timer to cancel the request after certain time
evtimer_add(&ev, &tv);
printf("starting event loop..\n");
printf("\n");
event_dispatch();
return 0;
}

Seg fault error in C

Getting seg-fault when I run this code. I commented where I'm getting the seg-fault (in handler() function). I'm not sure, may be I'm wrapping data twice that's why or what's the problem? It's printing correctly till "start_timer" method.
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
typedef struct _data{
char *name;
}data;
struct timer_list{
void* timer_data;
unsigned long expires;
void (*function)(sigval_t);
};
volatile long int second = 0;
void handler(sigval_t val)
{
data *data_handler = val.sival_ptr;
printf("Handler: address of data: %p\n", data_handler);
printf("Handler: address of &data_handler->name: %p\n", &data_handler->name);
printf("Handler entered with value :%s\n", data_handler->name); `**//**SEG-FAULT HERE****`
}
void timer_handler(union sigval val)
{
printf(" ----- Seconds: %ld\n", ++second);
}
/* start timer with all we got as data is timer */
void start_timer(struct timer_list *timer)
{
printf("\nStart_timer...: Timer->data address: %p\n", timer->timer_data);
data *data_handler = timer->timer_data;
printf("Start_timer...: entered with value :%s\n", data_handler->name);
int Ret;
pthread_attr_t attr;
pthread_attr_init( &attr );
//pthread_t tid;
struct sched_param parm;
parm.sched_priority = 255;
pthread_attr_setschedparam(&attr, &parm);
struct sigevent sig;
sigval_t val;
val.sival_ptr = timer->timer_data;
sig.sigev_notify = SIGEV_THREAD;
sig.sigev_notify_function = timer->function;
// sig.sigev_value.sival_int = val;
sig.sigev_value = val;
sig.sigev_notify_attributes = &attr;
data *data_handler1 = (data *)val.sival_ptr;
printf("From sigval...: handler_data address: %p\n", data_handler1);
printf("From sigval...: handler_data->name address: %p\n", &data_handler1->name);
printf("From sigval...: Handler entered with value :%s\n", data_handler1->name);
//create a new timer.
timer_t timerid;
Ret = timer_create(CLOCK_REALTIME, &sig, &timerid);
if (Ret == 0)
{
struct itimerspec in, out;
in.it_value.tv_sec = timer->expires;
in.it_value.tv_nsec = 0;
in.it_interval.tv_sec = 0;
in.it_interval.tv_nsec = 0;
timer_settime(timerid, 0, &in, &out);
}
}
/* Start_timer_on: wrapping up data into one timer structure, and starting timer */
void start_timer_on(data timer_data, unsigned long expires)
{
struct timer_list *timer = (struct timer_list *)malloc(sizeof(struct timer_list)); //Problem was here ... forgot to use malloc
timer->timer_data = &timer_data;
printf("\nTimer->data address: %p\n", &timer_data);
timer->function = handler;
timer->expires = expires;
start_timer(timer);
}
/* Main */
void main()
{
data handler_data1 = {"Handler Data 1"};
//data handler_data2 = {"Handler Data 2"};
//void *data1 = &handler_data1;
//void *data2 = &handler_data2;
pthread_attr_t attr;
pthread_attr_init( &attr );
struct sched_param parm;
parm.sched_priority = 255;
pthread_attr_setschedparam(&attr, &parm);
struct sigevent sig;
sig.sigev_notify = SIGEV_THREAD;
sig.sigev_notify_function = timer_handler;
sig.sigev_notify_attributes = &attr;
//create a new timer - clock.
timer_t timerid;
timer_create(CLOCK_REALTIME, &sig, &timerid);
struct itimerspec in, out;
in.it_value.tv_sec = 1;
in.it_value.tv_nsec = 0;
in.it_interval.tv_sec = 1;
in.it_interval.tv_nsec = 0;
printf("*** *** *** Main clock starts *** *** ***\n");
timer_settime(timerid, 0, &in, &out);
printf("***** Start timer for data1 for 2 sec *****\n");
start_timer_on(handler_data1, 2);
// printf("***** Start timer for data1 for 5 sec *****\n");
// start_timer(data2, 5);
sleep(20);
}
This might be the problem. In the code below, timer_data is local to function start_timer_on. The object is destroyed as soon as the function exits. So, when accessing the name in handler, it will segfault.
void start_timer_on(data timer_data, unsigned long expires)
{
struct timer_list *timer;
timer->timer_data = &timer_data;
printf("\nTimer->data address: %p\n", &timer_data);
timer->function = handler;
timer->expires = expires;
start_timer(timer);
}
You should use void start_timer_on(data *timer_data, unsigned long expires), so that the data is not freed until main exits.

Non blocking timer in linux user space (in C)

Actually I want to implement non-blocking timer, when the timer expires a handler will be called and will do something (for now it prints data). I google and realized that timer_create, timer_settimer are non-blocking timer. BUT still I've issue, I have to wait for my timer to expire (sleep(MAX) or while(1) {;}). But then if I'm calling my start_timer method with different "expiry" time, it should work accordingly, should not block other. e.g. here first time I'm calling timer, and expecting to call handler in 5 sec but before that 2nd call should print its data as, that interval I've given is 1sec only. And of course its not behaving same. Any idea?
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
typedef struct _data{
char *name;
}data;
void handler(union sigval val)
{
data *data_handler = val.sival_ptr;
printf("Handler entered with value :%s\n", data_handler->name);
}
void mod_timer(timer_t timerid, struct sigevent sig, struct itimerspec in, struct itimerspec out)
{
printf("mod_timer\n");
timer_settime(timerid, 0, &in, &out);
while(1)
sleep(1);
//delete the timer.
timer_delete(timerid);
}
void start_timer(void* val, int interval)
{
int Ret;
pthread_attr_t attr;
pthread_attr_init( &attr );
struct sched_param parm;
parm.sched_priority = 255;
pthread_attr_setschedparam(&attr, &parm);
struct sigevent sig;
sig.sigev_notify = SIGEV_THREAD;
sig.sigev_notify_function = handler;
// sig.sigev_value.sival_int = val;
sig.sigev_value.sival_ptr = val;
sig.sigev_notify_attributes = &attr;
//create a new timer.
timer_t timerid;
Ret = timer_create(CLOCK_REALTIME, &sig, &timerid);
if (Ret == 0)
{
struct itimerspec in, out;
in.it_value.tv_sec = 1;
in.it_value.tv_nsec = 0;
in.it_interval.tv_sec = interval;
in.it_interval.tv_nsec = 0;
mod_timer(timerid, sig, in, out);
}
}
void main()
{
// start_timer(1, 5);
// start_timer(2, 1);
data handler_data1 = {"Handler Data 1"};
data handler_data2 = {"Handler Data 2"};
void *data1 = &handler_data1;
void *data2 = &handler_data2;
start_timer(data1, 5);
start_timer(data2, 1);
}
You can use the alarm function to generate a signal, and the signal function to specify the handler to that signal.

Resources