LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-15-2022, 11:04 PM   #1
lnx8a
LQ Newbie
 
Registered: Aug 2022
Posts: 1

Rep: Reputation: Disabled
Question c timer in linux ubuntu


i am new to linux programming in C. Was looking for a periodic timer that repeats very fast like each 10 msec with overlap calling protection. Is the posix timer reliable to run on linux versus the linux specific timers?.
 
Old 08-16-2022, 12:51 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Hi, and welcome here, at LQ!

without context hard to say anything. Why do you think the posix timer is not reliable?
 
Old 08-16-2022, 04:02 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Mind you,Linux is not a Real-Time system i.e. there are no guarranted time-limits or intervals.
 
Old 08-16-2022, 05:43 AM   #4
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 998

Rep: Reputation: 470Reputation: 470Reputation: 470Reputation: 470Reputation: 470
You seem to be referring to nanosleep.

I would not rely on time delays much below 100ms. A desktop OS is not built to do that. It runs too many processes.

You need to design your program so that it can tolerate timing errors of hundreds of milliseconds. Or, you need to use an OS intended for real-time.
Ed
 
Old 08-16-2022, 07:06 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
A while back, for fun, I wrote a simple animation program that ran on the linux console (writing directly to the VCS deviceš). I used a clock_nanosleep() to govern the display loop, adjusting the sleep duration each time to compensate for drift˛. Typically the latency on the wake was sub 1msec. Setting scheduling class and priority might help with reliability, but as Nev' points out, Linux is not a true RTOS and there are no guarantees.

I'd expect the results using a timer and SIGALRM to be similar.

___
1) I eventually shelved the project as the VCS device doesn't support mmap and writing out a full frame each time using write(2) was just too inefficient/slow.

2) using TIMER_ABSTIME on the nanosleep is also a good way to avoid latency induced drift.
 
Old 08-18-2022, 04:31 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Here's the best way to consider "timers." A timer that is set to go off every 10 msec will ensure that your process will wake up at least that often. But, having woken up, your process then needs to "look at the clock to find out what time it is." It should process its entire time-ordered work list, re-checking the clock every time, until it either runs out of work to do or finds that the next unit of work is due "more than 10 msec from now." At which time it can yield, knowing that the next timer-pop will happen "soon."

It is entirely possible that the timer might go off again while the process is doing its work, so it's entirely possible that it might "wait for the next alarm" and actually wind up "not waiting at all." The timer simply serves as a mechanism to allow the process to avoid monopolizing system resources ("busy waiting ...") when it has nothing to do. The process must constantly refer to the immediate time-of-day value as it processes its to-do list. The unit-of-work might be processed slightly later than the specified time – since "Linux is not an RTOS" – but it will not be processed sooner.

Last edited by sundialsvcs; 08-18-2022 at 04:36 PM.
 
Old 09-07-2022, 07:44 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Here is one way to do it:

Code:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>


void doit(int n)
{
        time_t tp = time(0);
        printf("%s\r\n\r\n", ctime(&tp));
}

int main(int args, char **arv)
{
        const struct sigaction sa = { doit, 0, 0 };
        sigaction(SIGALRM, &sa, 0);

        while(1){
                alarm(1);
                sleep(10);
        }
}

Last edited by bigearsbilly; 09-07-2022 at 07:48 AM.
 
Old 09-07-2022, 07:58 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
more flexible

I just knocked this up quick it just echos what you type with the timer being annoying


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <signal.h>
#include <time.h>


static char * chomp(char * line)
{
        char *p = line;
        strsep( &p, "\r\n");
        return p;
}


void squawk(union sigval unused)
{
        static int n;
        fprintf(stderr, "\nhello %d\n", ++n);
        return;
}

int start_ticker()
{
        timer_t timid ;
        // union sigval sv = {0,};
        struct sigevent sev = { 0, };
        sev.sigev_notify = SIGEV_THREAD;
        sev.sigev_notify_function = squawk;

        if (timer_create( CLOCK_REALTIME,  &sev, &timid) == 0) {

                // struct timespec tick = { 1, 0 };
                struct itimerspec ticks = { { 2, 0}, {2, 0} };
                timer_settime( timid, 0, &ticks, NULL);
        }  else {
                perror("timer_create");
                return 0;
        }
        return 1;
}
 
Old 09-12-2022, 12:28 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Always remember that: in a "non-real time OS," such as Linux, the occurrence of a timer interrupt only causes the affected process(es) to "become runnable." It does not cause the OS to "move Hell and High Water To ..." cause said process to "immediately run." (i.e. with to strive to fulfill some promised maximum contractual degree of "latency," of which Linux by its design has none.)

And this, of course, is the fundamental difference between "real-time" and "non-real-time" OSes . . .

A "non-real-time" OS promises that "you will not wake up sooner." But it does not promise that "you will not wake up later," nor that you might not occasionally "wake up twice, finding nothing to do the second time."

Last edited by sundialsvcs; 09-12-2022 at 08:29 PM.
 
  


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
local timer&&global timer yellownew8707 Linux - Kernel 0 10-29-2012 01:44 AM
Internet Based "Tea Timer" Reminder System for Ubuntu Jaffa8 Linux - Newbie 2 02-08-2008 11:03 AM
apic timer error while installing ubuntu tusharwalaskar Linux - Newbie 1 12-05-2007 07:55 AM
How can I get LAPIC timer to run instead of the PIT timer? sixerjman Linux - Kernel 1 10-16-2007 09:59 PM
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM

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

All times are GMT -5. The time now is 06:31 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