Vorab: Ich bin neu in der Linux-Welt und kenne mich im Grunde nur mit
PICs und AVRs aus.
Der Code ist cross-compiliert für einen Raspberry Pi und sollte
eigentlich gehen.
1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include <time.h>
|
4 | #include <unistd.h>
|
5 | #include "c_gpio.h"
|
6 |
|
7 | int main(void) {
|
8 | printf("\nTest_1 Timing\n");
|
9 | printf ("=============\n");
|
10 | int i;
|
11 | for (i = 0; i<10; i++){
|
12 | sleep(1);
|
13 | //DelayMicrosecondsNoSleep (1000000); <- funktioniert nicht
|
14 | printf ("%i, ", i);
|
15 | }
|
16 | printf ("\n");
|
17 | return EXIT_SUCCESS;
|
18 |
|
19 | }
|
Eigentlich erwarte ich, daß nach jeder Sekunde i schön artig um 1 erhöht
und printf aufgerufen wird. Das ist aber nicht so, stattdessen passiert
10 sek lang gar nichts, danach zeigt printf alle Zahlen von 0 bis 9.
Wenn ich zusätzlich aber das hier in die for-Schleife einfüge,
funktioniert's.
1 | s = sleep(1);
|
2 | printf ("sleep(1): %i\n", s);
|
pi@raspberrypi ~/c-stuff $ ./test_1
Test_1 Timing
=============
sleep(1): 0
0, sleep(1): 0
1, sleep(1): 0
2, sleep(1): 0
3, sleep(1): 0
4, sleep(1): 0
5, sleep(1): 0
6, sleep(1): 0
7, sleep(1): 0
8, sleep(1): 0
9,
Warum ist das so?
Das hab ich in unistd.h gefunden:
1 | /* Make the process sleep for SECONDS seconds, or until a signal arrives
|
2 | and is not ignored. The function returns the number of seconds less
|
3 | than SECONDS which it actually slept (thus zero if it slept the full time).
|
4 | If a signal handler does a `longjmp' or modifies the handling of the
|
5 | SIGALRM signal while inside `sleep' call, the handling of the SIGALRM
|
6 | signal afterwards is undefined. There is no return value to indicate
|
7 | error, but if `sleep' returns SECONDS, it probably didn't work.
|
8 |
|
9 | This function is a cancellation point and therefore not marked with
|
10 | __THROW. */
|
Steht das damit im Zusammenhang?