* [Xenomai-help] help on alarms.. [not found] <mailman.271.1146850846.2786.xenomai@xenomai.org> @ 2006-05-07 20:33 ` Prashanti Bedapudi 2006-05-07 21:11 ` Jim Cromie 2006-05-07 22:26 ` Philippe Gerum 0 siblings, 2 replies; 3+ messages in thread From: Prashanti Bedapudi @ 2006-05-07 20:33 UTC (permalink / raw) To: xenomai [-- Attachment #1: Type: text/plain, Size: 558 bytes --] Attached is a piece of code using alarms, but I am seeing some unexpected behavior. Any help will be appreciated. in function main() the program behaves two different ways depending on where I have the last printf() 1. It works fine and terminates as expected if I have it like this while(!quit_now) printf("quit_now = %d\n", quit_now); 2. It does not terminate if I have it this way. I know quit_now is set to 1, since the last statement printed is 'quit_now is set to 1'. while(!quit_now); printf("quit_now = %d\n", quit_now); thanks, Shanti [-- Attachment #2: alarmtest.cpp --] [-- Type: text/plain, Size: 1449 bytes --] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h> #include <native/types.h> #include <native/alarm.h> #include <native/task.h> #define ALARM_NAME "dl_alarm2" #define ALARM_TASK_NAME "dl_alarm_task" #define ALARM_TIME 500000 #define ALARM_INTERVAL 250000 #define ALARM_TASK_PRIO 20 RT_ALARM dl_alarm; RT_TASK alarm_task; bool quit_now = 0; void catch_signal(int sig) { rt_alarm_stop(&dl_alarm); rt_alarm_delete(&dl_alarm); rt_task_delete(&alarm_task); exit(0); } static void alarm_handler(void *cookie) { int i =0; printf("Inside alarm handler\n "); for(i = 0; i<100; i++){ rt_alarm_wait(&dl_alarm); printf("i = %d\n",i); } quit_now = 1; printf("quit_now is set to %d\n",quit_now); return; } int main() { int ret = 0; signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); ret = rt_alarm_create(&dl_alarm, ALARM_NAME) ; if (ret) printf("\ncannot create alarm %d\n", ret); ret = rt_task_spawn(&alarm_task, ALARM_TASK_NAME, 0, ALARM_TASK_PRIO, 0, &alarm_handler, NULL); if(ret) exit(0); rt_alarm_start(&dl_alarm, ALARM_TIME, ALARM_INTERVAL); while(!quit_now); printf("quit_now = %d\n",quit_now); rt_alarm_stop(&dl_alarm); rt_alarm_delete(&dl_alarm); rt_task_delete(&alarm_task); return 0; } ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Xenomai-help] help on alarms.. 2006-05-07 20:33 ` [Xenomai-help] help on alarms Prashanti Bedapudi @ 2006-05-07 21:11 ` Jim Cromie 2006-05-07 22:26 ` Philippe Gerum 1 sibling, 0 replies; 3+ messages in thread From: Jim Cromie @ 2006-05-07 21:11 UTC (permalink / raw) To: Prashanti Bedapudi; +Cc: xenomai Prashanti Bedapudi wrote: > Attached is a piece of code using alarms, but I am seeing some unexpected > behavior. Any help will be appreciated. > > have a look at src/testsuite/latency it does what I believe youre seeking to do (tho I havent read your code) > in function main() the program behaves two different ways depending on where > I have the last printf() > > 1. It works fine and terminates as expected if I have it like this > while(!quit_now) > printf("quit_now = %d\n", quit_now); > > 2. It does not terminate if I have it this way. I know quit_now is set to 1, > since the last statement printed is 'quit_now is set to 1'. > > while(!quit_now); > printf("quit_now = %d\n", quit_now); > > thanks, > Shanti > > ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Xenomai-help] help on alarms.. 2006-05-07 20:33 ` [Xenomai-help] help on alarms Prashanti Bedapudi 2006-05-07 21:11 ` Jim Cromie @ 2006-05-07 22:26 ` Philippe Gerum 1 sibling, 0 replies; 3+ messages in thread From: Philippe Gerum @ 2006-05-07 22:26 UTC (permalink / raw) To: Prashanti Bedapudi; +Cc: xenomai Prashanti Bedapudi wrote: > Attached is a piece of code using alarms, but I am seeing some unexpected > behavior. Any help will be appreciated. > - bool quit_now = 0; + volatile bool quit_now = 0; > in function main() the program behaves two different ways depending on where > I have the last printf() > > 1. It works fine and terminates as expected if I have it like this > while(!quit_now) > printf("quit_now = %d\n", quit_now); > > 2. It does not terminate if I have it this way. I know quit_now is set to 1, > since the last statement printed is 'quit_now is set to 1'. > > while(!quit_now); > printf("quit_now = %d\n", quit_now); > > thanks, > Shanti > > > > ------------------------------------------------------------------------ > > #include <stdio.h> > #include <stdlib.h> > #include <unistd.h> > #include <errno.h> > #include <string.h> > #include <sys/types.h> > #include <sys/wait.h> > #include <signal.h> > #include <native/types.h> > #include <native/alarm.h> > #include <native/task.h> > > > #define ALARM_NAME "dl_alarm2" > #define ALARM_TASK_NAME "dl_alarm_task" > #define ALARM_TIME 500000 > #define ALARM_INTERVAL 250000 > #define ALARM_TASK_PRIO 20 > > RT_ALARM dl_alarm; > RT_TASK alarm_task; > bool quit_now = 0; > > > void catch_signal(int sig) > { > rt_alarm_stop(&dl_alarm); > rt_alarm_delete(&dl_alarm); > rt_task_delete(&alarm_task); > exit(0); > } > > > > static void alarm_handler(void *cookie) > { > int i =0; > > printf("Inside alarm handler\n "); > for(i = 0; i<100; i++){ > rt_alarm_wait(&dl_alarm); > printf("i = %d\n",i); > } > > quit_now = 1; > printf("quit_now is set to %d\n",quit_now); > return; > > } > > int main() > { > int ret = 0; > > signal(SIGTERM, catch_signal); > signal(SIGINT, catch_signal); > > ret = rt_alarm_create(&dl_alarm, ALARM_NAME) ; > if (ret) > printf("\ncannot create alarm %d\n", ret); > > ret = rt_task_spawn(&alarm_task, ALARM_TASK_NAME, 0, ALARM_TASK_PRIO, 0, &alarm_handler, NULL); > if(ret) > exit(0); > > rt_alarm_start(&dl_alarm, ALARM_TIME, ALARM_INTERVAL); > > > while(!quit_now); > printf("quit_now = %d\n",quit_now); > > > rt_alarm_stop(&dl_alarm); > rt_alarm_delete(&dl_alarm); > rt_task_delete(&alarm_task); > > return 0; > } > > > ------------------------------------------------------------------------ > > _______________________________________________ > Xenomai-help mailing list > Xenomai-help@domain.hid > https://mail.gna.org/listinfo/xenomai-help -- Philippe. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-05-07 22:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.271.1146850846.2786.xenomai@xenomai.org>
2006-05-07 20:33 ` [Xenomai-help] help on alarms Prashanti Bedapudi
2006-05-07 21:11 ` Jim Cromie
2006-05-07 22:26 ` Philippe Gerum
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.