timer interrupt

That's sort-of why I mentioned using poll(), which is often convenient in cases where you want a program to sleep most of the time between doing periodic checks, etc but still to service external events immediately.

Case in point was a system, written in C, I designed that accepted very large quantities of data in the form of device activity logs, transformed them into a common format and loaded them into a data warehouse after manipulating and/or compressing the content of some fields. In the interest of speed, logs were converted into a common format as they arrived. Output from this process was left in memory under the ownership of a log manager process, which owned all logs currently in memory and scheduled their processing. Each log stayed in memory until all field- level processing was complete - the last process in the series loaded to log into the data warehouse and then told the log manager to delete it.

The aims of this approach were to:

- minimise time taken by reading, writing or moving activity logs around

- keep individual processes simple by avoiding multi threading

- allow reuse of some of the field transforming processes by parameterising them

- allow bottlenecks to be eliminated by running multiple copies of any slow process.

As a result, all these processes were based around poll() with the timeout value set non-zero: on timeout they'd look for more work and sleep if there was none: i.e the initial log load&transform process looked for files on the input disk space while everything else asked the log manager for a new logs to work on. That all worked as intended and, last but not least, all processes also included stdin as a file to be monitored by poll() so overall system control and monitoring could be handled by sending messages to stdin on any or all processes.

Reply to
Martin Gregorie
Loading thread data ...

Yes. Leave as much direct hardware interaction as possible where it belongs - being handled by the kernel.

Reply to
Martin Gregorie

That's why the fired-up code sets a global flag when it begins, and resets it when it ends. But before setting the flag it checks whether the flag is already set and exits immediately if so (and optionally sets a notification that the system is just too damn slow).

Reply to
Charlie Gibbs

Alexandre,

Looking at the rather terse question the OP posted I'm not willing to make such a guess myself.

Too many possibilities all culminating into the OPs post. Some of them focussed on just toggeling that pin, others on the actual usage of timer interrupts.

The real question is : why can't *you* ?

Besides, how does me being able to give an example or not change anything to the info I provided to the OP ?

Also, it looks like Knute Johnson already posted an example.

Regards, Rudy Wieser

Reply to
R.Wieser

Ahem,

Yeah, I saw you mentioning that. Another reason why sleep() might not be the best solution.

Regards, Rudy Wieser

Reply to
R.Wieser

Charlie,

:-) Such a basic solution is ofcourse easy enough. But the OP would first need to know about the possibility of the problem - which is all I wanted to do.

Regards, Rudy Wieser

Reply to
R.Wieser

After enough experience you become pessimistic enough to spend your time thinking up ways that things can go wrong, and workarounds for them. :-)

Reply to
Charlie Gibbs

[Snip]

Exactly, for a noddy Python routine which reads the CPU once per second, and isn't particularly bothered its bang on the second, but would like it not to drift as would happen just by using time.sleep(1), I use:-

While True: """Read CPU temp"""" """Store in circular buffer""" # sleep for the remainder of the current second time.sleep(1 - (time.time() % 1))

The circular buffer is used to provide the average CPU temperature over periods from 5 to 15 minutes. So even if the worst happened, and sleep took more than a second on an occasion, it wouldn't make much difference to the average.

---druck

Reply to
druck

Le 04/11/2021 à 10:03, zeneca a écrit :

This is how I did it: Many thanks to all

#include <stdio.h>

#include <stdio.h>

#include <sys/time.h>

#include <unistd.h>

#include <pthread.h>

#include <bcm2835.h>

#include <wiringPi.h>

#include <signal.h>

#include <time.h>

// Which GPIO pin we're using #define PIN_LED 0 #define PIN_LED1 2 #define PIN_BUTTON 3

#define CLOCKID CLOCK_REALTIME #define SIG SIGRTMIN

// How much time a change must be since the last in order to count as a change #define IGNORE_CHANGE_BELOW_USEC 10000

// Current state of the pin static volatile int state; // Time of last change int debug; int fd; int ret; static int led; /** timer handler **/ static void handlerT(int sig, siginfo_t *si, void *uc) { digitalWrite(PIN_LED1,led); led ^= 1; }

************************************/ // Handler for interrupt void handle(void) {

usleep(1000);

state = digitalRead(PIN_BUTTON);

if (!state) { if(debug) printf("Falling\n"); digitalWrite(PIN_LED, HIGH); } else { if(debug) printf("Rising\n"); digitalWrite(PIN_LED, LOW); }

}

int main(void) {

fd = 0x00; debug = isatty(fd);

wiringPiSetup();

// Set pin to output in case it's not pinMode(PIN_BUTTON, INPUT); pinMode(PIN_LED, OUTPUT); pinMode(PIN_LED1, OUTPUT);

digitalWrite(PIN_LED, LOW); digitalWrite(PIN_LED1, LOW);

// Bind to interrupt wiringPiISR(PIN_BUTTON, INT_EDGE_BOTH, &handle);

if(debug)printf("Creating timer\n"); timer_t timerid; struct sigevent sev; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIG; sev.sigev_value.sival_ptr = &timerid; ret = timer_create(CLOCKID, &sev, &timerid);

if(ret) perror ("Timer create\n");

struct sigaction sa; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = handlerT; sigemptyset(&sa.sa_mask); if (sigaction(SIG, &sa, NULL) == -1) perror("sigaction");

struct itimerspec its; its.it_value.tv_sec = 1; its.it_interval.tv_sec = 1;

if(debug)printf("settime\n"); if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime");

int sleeptime = 5000000; // Waste time but not CPU for (;;) sleep(sleeptime); }

Reply to
zeneca

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.