LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2011, 09:17 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile timer_create failing


Hi,

I am using timer_create and timer_settime for for creating seconds timer. The behaviour of this function is random. some times timer_create() failed with error as: out of memory, timer_settime() failing with error: invalied arguments. Please help me in this. I am enclosing sample code which i am using.

Code:
#include <pthread.h>
#include <signal.h>
//#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef void (timer_callback) (union sigval);
/*
 * Timer initialization function 
 */
#define TIME_SET	1000
int timer_initialize(timer_t * timer_id, int period, timer_callback * cbf, void* data) 
{
int status =0;
struct itimerspec ts;
struct sigevent se;
    
	/* register callback func */
	se.sigev_notify = SIGEV_THREAD;
	se.sigev_value.sival_ptr = data;
	se.sigev_notify_function = cbf;
	se.sigev_notify_attributes = NULL;

	if(timer_create(CLOCK_REALTIME, &se, timer_id) != 0)
	{
		printf(" error in creating a timer %s", strerror(errno));
		exit(1);
	}

	/* assign the period */
	ts.it_value.tv_sec = period/1000;
	ts.it_value.tv_nsec = 0;
	ts.it_interval.tv_sec = ts.it_value.tv_sec;
        ts.it_interval.tv_nsec = ts.it_value.tv_nsec;

	if(timer_settime(*timer_id, 0, &ts, 0) != 0)
	{
		printf(" error in setting timer %s", strerror(errno));
		exit(1);
	}

return 0;
}
/*
 * scanTimer callback function 
 */
void scanTimer_callback(union sigval si)
{
    char* evt = (int*) si.sival_ptr;
    printf("\n Iam in [%s] callback \n", evt);
}

/*
 * tileTimer callback function 
 */
void tileTimer_callback(union sigval si)
{
    char* evt = (int*) si.sival_ptr;
    printf("\n Iam in [%s] callback \n", evt);
}
int main(int argc, char ** argv)
{

    timer_t timer_ID;
  while(1){
    timer_initialize(&timer_ID, TIME_SET, scanTimer_callback, (void *) "Scan" );
    getchar();
    timer_delete(timer_ID);
    getchar();
  }
    return 0;
}
 
Old 06-21-2011, 09:57 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Perhaps you need to zero-out the sigevent structure before setting it with values?

The following code works for me:
Code:
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdio.h>

void callback(union sigval si)
{
   const char* data = si.sival_ptr;
   printf("\nInside callback; data is %s", data);
}

int main()
{
   timer_t timerID;

   struct sigevent se;

   memset(&se, 0, sizeof(se));

   se.sigev_notify            = SIGEV_THREAD;
   se.sigev_value.sival_ptr   = "Scan";
   se.sigev_notify_function   = callback;
   se.sigev_notify_attributes = NULL;

   if (timer_create(CLOCK_REALTIME, &se, &timerID) != 0)
   {
      perror("Error creating timer.");
      return -1;
   }

   struct itimerspec ts = { {2, 0}, {2, 0} };

   if (timer_settime(timerID, 0, &ts, NULL) != 0)
   {
      perror("Error setting up timer.");
      return -1;
   }

   printf("Press <enter> to continue...");
   getchar();

   timer_delete(timerID);

   printf("\n");

   return 0;
}
 
Old 06-21-2011, 12:12 PM   #3
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Thanks for the quick response. I am calling timer_initialize, timer_delete in infinite loop, in this case it is failing after some iterations. Will memset(&se, 0, sizeof(se)); gives any improvement.
 
Old 06-21-2011, 12:18 PM   #4
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Thanks for the quick response. I am calling timer_initialize, timer_delete in infinite loop, in this case it is failing after some iterations. Will memset(&se, 0, sizeof(se)); gives any improvement.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C/C++] Using timer_create() and interrupting sleep() dwhitney67 Programming 0 05-19-2009 07:30 AM
Timer Deadlock problem in timer_create and delete_timer nkk4u Programming 0 04-15-2005 02:10 AM
eth1 failing on boot, IEEE firewire card driver failing, help jackuss_169 Linux - Laptop and Netbook 5 03-05-2005 07:34 AM
LILO install failing, Boot failing, but Installation fine. sramelyk Slackware 9 08-23-2003 02:37 PM
X failing to load; AddScreen/InitScreen failing for driver 0 weblion Linux - Software 1 08-01-2002 06:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration