A problem occurs when POSIX timer function timer_creat () is used.

Source: Internet
Author: User
Tags call back

POSIX provides the following interfaces to set the timer:

Int timer_create (clockid_t which_clock, struct sigevent * timer_event_spec, timer_t * created_timer_id );

Int timer_gettime (timer_t timer_id, struct itimerspec * setting );

Int timer_settime (timer_t timer_id, int flags, struct itimerspec * new_setting, struct itimerspec * old_setting );

Int timer_delete (timer_t timer_id );

The following functions are unexpected during running (test_rate is the length of the timer passed in, test_on is of the static volatile sig_atomic_t type, and the initialization is 1 ):

int test_dejitter(unsigned int test_rate){struct sigevent event;timer_t timer_id;struct itimerspec value;struct itimerspec get_time;int ret = -1;/////////unsigned int traup_len,bts_id,cxn;/////////printf("test time interval is %ds \n",test_rate);event.sigev_notify = SIGEV_THREAD;event.sigev_notify_function=timer_cb;value.it_interval.tv_sec        = 0;value.it_interval.tv_nsec       = 0;value.it_value.tv_sec           = test_rate;value.it_value.tv_nsec          = 0;ret = timer_create(CLOCK_REALTIME,&event,&timer_id);   printf("timer_create() returns %d\n",ret);   printf("Start to test! \n");gettimeofday(&tv1, NULL);ret = timer_settime(timer_id, 0, &value, NULL);   printf("timer_settime() returns %d\n",ret);while(test_on){bts_id = rand()%NBBTS;cxn = rand()%NBCONNECTONS;rtp_parsing_sim(cxn,&dl_rtp[0],0,btss[bts_id].pos,&traup_len);   printf("In loop after rtp_parsing_sim(), test_on = %d! \n",test_on);timer_gettime(timer_id,&get_time);   printf("\033[33mtimer_gettime() returns:interval.tv_sec = %d, \                                    interval.tv_nsec = %d, it_value.tv_sec = %d, \                                    it_value.tv_nsec = %d \033[0m\n",\                                    get_time.it_interval.tv_sec,\                                    get_time.it_interval.tv_nsec,\                                    get_time.it_value.tv_sec,\                                    get_time.it_value.tv_nsec);sleep(1);}gettimeofday(&tv2, NULL);printf("\033[35mTest ended at %fs ! \033[0m\n",\        tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec)/1000000.0);return 1;}

Among them, there is no relation between the 28th-row function and the timer and test_on.

Run this function. When the value of test_rate is 3, the result is:

Test Time Interval is 3 S
Timer_create () returns 0
Start to test!
Timer_settime () returns 0
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 2, it_value. TV _nsec = 999966476
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 1, it_value. TV _nsec = 999153244
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 0, it_value. TV _nsec = 998183847
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 0, it_value. TV _nsec = 0
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 0, it_value. TV _nsec = 0

The timer does not respond to the callback function !!

If you comment out 28th lines of code and do not run this function, the timer callback is successful!

Change the Event Notification Method of the timer:

event.sigev_notify = SIGEV_SIGNAL; //SIGEV_THREAD;//event.sigev_signo = SIGALRM;

And add the signal processing function:

signal(SIGALRM,timer_cb);

The result is as follows:

Test Time Interval is 3 S
Timer_create () returns 0
Start to test!
Timer_settime () returns 0
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 2, it_value. TV _nsec = 999965638
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 1, it_value. TV _nsec = 998988977
In loop after rtp_parsing_sim (), test_on = 1!
Timer_gettime () returns: interval. TV _sec = 0, interval. TV _nsec = 0, it_value. TV _sec = 0, it_value. TV _nsec = 998036342
Time call back
Teston is set to 0, test is going to stop after 3.001004 s!
Test ended at 3.001125 s!
Timer callback successful!

The initial cause of the analysis is that a thread is created using the timer response notification method in sigev_thread mode to collect signals. Through PS-elfm, we can see that the process has created a thread, but the main thread has no response when the timer responds, the preliminary judgment thread is not executed! It may be related to the setting of the sigev_policy_attributes member in the sigevent struct. For details, see manpage, (http://www.chineselinuxuniversity.net/man-pages/man7/sigevent.7.html)

In the form of signal, the callback is successful!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.