LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-28-2014, 03:23 AM   #1
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Rep: Reputation: Disabled
How to run the timer in the background ?


static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;

tidp = si->si_value.sival_ptr;

if ( *tidp == firstTimerID )

TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}

/*
The function takes a pointer to a timer_t variable that will be filled with the
timer ID created by timer_create(). This pointer is also saved in the sival_ptr
variable right before calling timer_create(). In this function notice that we
always use the SIGRTMIN signal, so expiration of any timer causes this signal to
be raised. The signal handler I've written for that signal is timerHandler.
*/

static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
//sigset_t mask;
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;


/* Set up signal handler. */
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}

/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);

its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 1000000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 1000000;
timer_settime(*timerID, 0, &its, NULL);


return 1;

}

int callBackTimers()
{
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
while(1)
;;

}

int CreateSocket()
{


socklen_t len = sizeof(client);
// Socket creation for UDP

acceptSocket=socket(AF_INET,SOCK_DGRAM,0);

if(acceptSocket==-1)

{

printf("Failure: socket creation is failed, failure code\n");

return 1;

}

else

{

printf("Socket started!\n");

}

//non blocking mode
/* rc = ioctl(acceptSocket, FIONBIO, (char *)&flag);
if (rc < 0)
{
printf("\n ioctl() failed \n");
return 0;
}*/

//Bind the socket
memset(&addr, 0, sizeof(addr));

addr.sin_family=AF_INET;

addr.sin_port=htons(port);

addr.sin_addr.s_addr=htonl(INADDR_ANY);

rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));

if(rc== -1)

{

printf("Failure: listen, failure code:\n");

return 1;

}

else

{

printf("Socket an port %d \n",port);

}


if(acceptSocket == -1)
{
printf("Fehler: accept, fehler code:\n");

return 1;
}
else
{

while(rc!=-1)
{


rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("something went wrong with data %s", strerror(errno));
break;
}


XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
callBackTimers();

}


}

close(acceptSocket);



return 0;

}






int main()
{

Xcp_Initialize();
CreateSocket();
return 2;

}

I am working on a client and server architecture. Server code is shown above and I created a socket to recieve the request from the client via the ip address and port number. Server is waiting for a request from the client and send a response back to the client. when ever it recieves data from the client, it should call the timer task (i.e callBackTimers in my code), For that I also created timer to call the task for every 2ms, 10ms and 100ms.
My QUESTION : How to run the callBackTimers in the background or where exactly should I call that ?? (my requirement is whenever it recieves a data from the client , I should call the callBackTimers). Someone help me.
 
Old 02-28-2014, 03:37 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,236

Rep: Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957Reputation: 7957
please use [code]here comes your script[/code] to keep formatting.
see how to create background (daemon) process: http://www.thegeekstuff.com/2012/02/c-daemon-process/
 
Old 02-28-2014, 03:44 AM   #3
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Original Poster
Rep: Reputation: Disabled
what is the problem in that?? could you please help me ??

Last edited by hemanth1989; 02-28-2014 at 03:46 AM.
 
  


Reply

Tags
c programming, linux client, linux module, threads, timer-countdown


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I want to run php file in background on fedora terminal in background. gauravwcities Linux - Newbie 8 01-18-2012 12:23 AM
Run SSH in the Background ? maas187 Linux - Networking 4 09-16-2009 11:27 PM
How can I get LAPIC timer to run instead of the PIT timer? sixerjman Linux - Kernel 1 10-16-2007 09:59 PM
how to run a process in the X background sh4d0w13 Linux - General 6 09-12-2005 02:54 PM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 12:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:29 AM.

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