diff --git a/Processes/cpu_freq b/Processes/cpu_freq index 69d08e6decf81aeefd3bc03d3d19fab205cde805..9b1a03b74468ef8dd78a4c76a83a75ea1ba86c17 100755 Binary files a/Processes/cpu_freq and b/Processes/cpu_freq differ diff --git a/Processes/cpu_freq.c b/Processes/cpu_freq.c index b66a8169df44f97312c8406cebb9302bcb1b3edf..6efd32cf57cc46751d33ee1414854fc46015fb39 100644 --- a/Processes/cpu_freq.c +++ b/Processes/cpu_freq.c @@ -1,50 +1,58 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <sys/stat.h> //for pipes +#include <sys/stat.h> #include <fcntl.h> +#include <string.h> -void read_cpu_frequency() +static const char *PIPE_TWO = "/tmp/pipeTwo"; + +static void read_cpu_freq_and_write(int fd) { FILE *file; char buffer[1024]; char *freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq"; file = fopen(freq_path, "r"); - if (file == NULL) - { - printf("Error opening file\n"); + if (!file) { + perror("Failed to open CPU freq file"); return; } - if (fgets(buffer, sizeof(buffer), file) != NULL) - { + if (fgets(buffer, sizeof(buffer), file) != NULL) { + // CPU frequency in kHz -> in GHz umrechnen float freq = atol(buffer) / 1000000.0f; - char message[1024]; - snprintf(message, sizeof(message), "%f", freq); - printf("CPU frequency: %f GHz\n", freq); - - //write pipe - const char *pTwo = "/tmp/pipeTwo"; - int vTwo = open(pTwo, O_WRONLY); - if (vTwo == -1) - { - perror("Failed to open pipe two in read_cpu_frequency"); - return; + char message[128]; + snprintf(message, sizeof(message), "%.3f", freq); + + ssize_t written = write(fd, message, strlen(message) + 1); + if (written < 0) { + perror("write to pipeTwo"); + } else { + printf("Wrote CPU freq: %s GHz\n", message); } - write(vTwo, message, sizeof(message)); - close(vTwo); - } - else - { - printf("Error reading file\n"); + } else { + fprintf(stderr, "Error reading CPU freq\n"); } + fclose(file); } -int main(void) { +int main(void) +{ + // Named Pipe einmal öffnen + mkfifo(PIPE_TWO, 0666); + int pipeFd = open(PIPE_TWO, O_WRONLY); + if (pipeFd == -1) { + perror("Failed to open pipeTwo for writing"); + return 1; + } + while (1) { - read_cpu_frequency(); + read_cpu_freq_and_write(pipeFd); + sleep(1); } + + close(pipeFd); return 0; } diff --git a/Processes/cpu_temp b/Processes/cpu_temp index fa369854b5434e2ac46e93909ca2ec7f492f2e4e..b7e02c500a2f8157b5ee3a05791e4829aada9488 100755 Binary files a/Processes/cpu_temp and b/Processes/cpu_temp differ diff --git a/Processes/cpu_temp.c b/Processes/cpu_temp.c index 801a433e2059f17ce26afd8116deb1fcbf8d5d3e..9ff26c602c66c144bd5636a123e18c194261f691 100644 --- a/Processes/cpu_temp.c +++ b/Processes/cpu_temp.c @@ -1,50 +1,63 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <sys/stat.h> //for pipes +#include <sys/stat.h> #include <fcntl.h> +#include <string.h> -void read_cpu_temp() +static const char *PIPE_ONE = "/tmp/pipeOne"; + +static void read_cpu_temp_and_write(int fd) { FILE *file; char buffer[1024]; char *temp_path = "/sys/class/thermal/thermal_zone0/temp"; file = fopen(temp_path, "r"); - if (file == NULL) - { - printf("Error opening file\n"); + if (!file) { + perror("Failed to open CPU temp file"); return; } - if (fgets(buffer, sizeof(buffer), file) != NULL) - { + if (fgets(buffer, sizeof(buffer), file) != NULL) { long temp = atol(buffer) / 1000; - char message[1024]; + // z.B. "42\n" => 42° C + char message[128]; snprintf(message, sizeof(message), "%ld", temp); - printf("CPU temperature: %ld C\n", temp); // todo: print message instead of temp? - - // write pipe - const char *pOne = "/tmp/pipeOne"; - int vOne = open(pOne, O_WRONLY); - if (vOne == -1) - { - perror("Failed to open pipe one in read_cpu_temp"); - return; + + // Schreiben, aber NICHT den FD schließen. + // Nur so viele Bytes schreiben, wie tatsächlich gebraucht werden. + ssize_t written = write(fd, message, strlen(message) + 1); + if (written < 0) { + perror("write to pipeOne"); + } else { + printf("Wrote CPU temp: %s C\n", message); } - write(vOne, message, sizeof(message)); - close(vOne); - } - else - { - printf("Error reading file\n"); + } else { + fprintf(stderr, "Error reading CPU temp\n"); } + fclose(file); } -int main(void) { +int main(void) +{ + // Named Pipe einmal öffnen (Blockierend) + // Der mkfifo-Aufruf sollte idealerweise vorher im Setup passieren + // Falls sie nicht existiert, wird sie erzeugt + mkfifo(PIPE_ONE, 0666); + int pipeFd = open(PIPE_ONE, O_WRONLY); + if (pipeFd == -1) { + perror("Failed to open pipeOne for writing"); + return 1; + } + while (1) { - read_cpu_temp(); + read_cpu_temp_and_write(pipeFd); + sleep(1); } + + // Nie erreicht im Beispiel, weil while(1) + close(pipeFd); return 0; } diff --git a/main b/main index d620ed2b993c96b62e0c8411c7c773dd8a1ad47e..98261929dd7adb1a4010b1d036332b20244b822a 100755 Binary files a/main and b/main differ diff --git a/main.c b/main.c index fb29f7df2bf22cb562e98be8e7865ad8e6717c83..8c6a45d8a3cd3f800f08111c561560203bdf4de7 100644 --- a/main.c +++ b/main.c @@ -1,39 +1,104 @@ #include <stdio.h> #include <stdlib.h> -#include <sys/stat.h> //for pipes -#include <fcntl.h> #include <unistd.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> -int main(void) { - // create and open pipes - const char *pOne = "/tmp/pipeOne"; - mkfifo(pOne, 0666); - int vOne = open(pOne, O_RDONLY | O_NONBLOCK); - if (vOne == -1) - { - perror("Failed to open pipe one in main"); +static const char *PIPE_ONE = "/tmp/pipeOne"; +static const char *PIPE_TWO = "/tmp/pipeTwo"; + +int main(void) +{ + // mkfifo() nur ausführen, wenn die Pipes noch nicht existieren, + // bzw. Fehler ignorieren, falls schon existiert: + if (mkfifo(PIPE_ONE, 0666) < 0 && errno != EEXIST) { + perror("mkfifo pipeOne"); + return 1; + } + if (mkfifo(PIPE_TWO, 0666) < 0 && errno != EEXIST) { + perror("mkfifo pipeTwo"); return 1; } - const char *pTwo = "/tmp/pipeTwo"; - mkfifo(pTwo, 0666); - int vTwo = open(pTwo, O_RDONLY | O_NONBLOCK); - if (vTwo == -1) - { - perror("Failed to open pipe two in main"); + // Beide Pipes blockierend öffnen + int fdOne = open(PIPE_ONE, O_RDONLY); + if (fdOne == -1) { + perror("open pipeOne for reading"); return 1; } - // read pipes - char mOne[1024] = {}; // for now only sending char with the pipe - read(vOne, mOne, sizeof(mOne)); - printf("value one received: %s\n", mOne); - close(vOne); + int fdTwo = open(PIPE_TWO, O_RDONLY); + if (fdTwo == -1) { + perror("open pipeTwo for reading"); + close(fdOne); + return 1; + } + + // Dauerhafte Lese-Schleife + while (1) { + char bufOne[128] = {0}; + char bufTwo[128] = {0}; + + // =========== Lesevorgang PipeOne =========== + // read() kann blockieren, wenn noch keine Daten da sind, + // da wir blockierendes I/O verwenden. + // Wenn die Schreibseite offen bleibt, kommen periodisch neue Daten. + ssize_t bytesReadOne = read(fdOne, bufOne, sizeof(bufOne)); + if (bytesReadOne > 0) { + // Gültige Daten -> ausgeben + printf("PipeOne CPU Temp: %s °C\n", bufOne); + } else if (bytesReadOne == 0) { + // EOF -> Schreibseite hat geschlossen + // -> ggf. Pipe erneut öffnen + printf("PipeOne closed by writer. Reopening...\n"); + close(fdOne); + + // Neu öffnen + fdOne = open(PIPE_ONE, O_RDONLY); + if (fdOne == -1) { + perror("Reopen pipeOne"); + break; // oder return 1; + } + } else { + // bytesReadOne < 0 => Fehler + if (errno == EINTR) { + // Signal unterbrochen, einfach weiter + continue; + } + perror("read pipeOne"); + break; // oder return 1; + } + + // =========== Lesevorgang PipeTwo =========== + ssize_t bytesReadTwo = read(fdTwo, bufTwo, sizeof(bufTwo)); + if (bytesReadTwo > 0) { + printf("PipeTwo CPU Freq: %s GHz\n", bufTwo); + } else if (bytesReadTwo == 0) { + printf("PipeTwo closed by writer. Reopening...\n"); + close(fdTwo); + fdTwo = open(PIPE_TWO, O_RDONLY); + if (fdTwo == -1) { + perror("Reopen pipeTwo"); + break; + } + } else { + if (errno == EINTR) { + continue; + } + perror("read pipeTwo"); + break; + } + + // Kurze Pause, damit CPU-Last nicht durch + // Dauerschleife hochgetrieben wird + usleep(200000); // 200 ms + } - char mTwo[1024] = {}; - read(vTwo, mTwo, sizeof(mTwo)); - printf("value two received: %s\n", mTwo); - close(vTwo); + // Sollte man jemals aus der while(1)-Schleife ausbrechen: + close(fdOne); + close(fdTwo); return 0; -} \ No newline at end of file +}