Skip to content
Snippets Groups Projects
Commit f2f3582b authored by Felix Moser's avatar Felix Moser
Browse files

fixedStructure

parent 6b9f2623
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> //for pipes #include <sys/stat.h>
#include <fcntl.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; FILE *file;
char buffer[1024]; char buffer[1024];
char *freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq"; char *freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq";
file = fopen(freq_path, "r"); file = fopen(freq_path, "r");
if (file == NULL) if (!file) {
{ perror("Failed to open CPU freq file");
printf("Error opening file\n");
return; 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; float freq = atol(buffer) / 1000000.0f;
char message[1024]; char message[128];
snprintf(message, sizeof(message), "%f", freq); snprintf(message, sizeof(message), "%.3f", freq);
printf("CPU frequency: %f GHz\n", freq);
ssize_t written = write(fd, message, strlen(message) + 1);
//write pipe if (written < 0) {
const char *pTwo = "/tmp/pipeTwo"; perror("write to pipeTwo");
int vTwo = open(pTwo, O_WRONLY); } else {
if (vTwo == -1) printf("Wrote CPU freq: %s GHz\n", message);
{
perror("Failed to open pipe two in read_cpu_frequency");
return;
} }
write(vTwo, message, sizeof(message)); } else {
close(vTwo); fprintf(stderr, "Error reading CPU freq\n");
}
else
{
printf("Error reading file\n");
} }
fclose(file); 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) { while (1) {
read_cpu_frequency(); read_cpu_freq_and_write(pipeFd);
sleep(1);
} }
close(pipeFd);
return 0; return 0;
} }
No preview for this file type
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> //for pipes #include <sys/stat.h>
#include <fcntl.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; FILE *file;
char buffer[1024]; char buffer[1024];
char *temp_path = "/sys/class/thermal/thermal_zone0/temp"; char *temp_path = "/sys/class/thermal/thermal_zone0/temp";
file = fopen(temp_path, "r"); file = fopen(temp_path, "r");
if (file == NULL) if (!file) {
{ perror("Failed to open CPU temp file");
printf("Error opening file\n");
return; return;
} }
if (fgets(buffer, sizeof(buffer), file) != NULL) if (fgets(buffer, sizeof(buffer), file) != NULL) {
{
long temp = atol(buffer) / 1000; long temp = atol(buffer) / 1000;
char message[1024]; // z.B. "42\n" => 42° C
char message[128];
snprintf(message, sizeof(message), "%ld", temp); snprintf(message, sizeof(message), "%ld", temp);
printf("CPU temperature: %ld C\n", temp); // todo: print message instead of temp?
// write pipe // Schreiben, aber NICHT den FD schließen.
const char *pOne = "/tmp/pipeOne"; // Nur so viele Bytes schreiben, wie tatsächlich gebraucht werden.
int vOne = open(pOne, O_WRONLY); ssize_t written = write(fd, message, strlen(message) + 1);
if (vOne == -1) if (written < 0) {
{ perror("write to pipeOne");
perror("Failed to open pipe one in read_cpu_temp"); } else {
return; printf("Wrote CPU temp: %s C\n", message);
}
write(vOne, message, sizeof(message));
close(vOne);
} }
else } else {
{ fprintf(stderr, "Error reading CPU temp\n");
printf("Error reading file\n");
} }
fclose(file); 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) { while (1) {
read_cpu_temp(); read_cpu_temp_and_write(pipeFd);
sleep(1);
} }
// Nie erreicht im Beispiel, weil while(1)
close(pipeFd);
return 0; return 0;
} }
No preview for this file type
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> //for pipes
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
static const char *PIPE_ONE = "/tmp/pipeOne";
static const char *PIPE_TWO = "/tmp/pipeTwo";
int main(void) { 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"); // 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; return 1;
} }
const char *pTwo = "/tmp/pipeTwo"; // Beide Pipes blockierend öffnen
mkfifo(pTwo, 0666); int fdOne = open(PIPE_ONE, O_RDONLY);
int vTwo = open(pTwo, O_RDONLY | O_NONBLOCK); if (fdOne == -1) {
if (vTwo == -1) perror("open pipeOne for reading");
{
perror("Failed to open pipe two in main");
return 1; return 1;
} }
// read pipes int fdTwo = open(PIPE_TWO, O_RDONLY);
char mOne[1024] = {}; // for now only sending char with the pipe if (fdTwo == -1) {
read(vOne, mOne, sizeof(mOne)); perror("open pipeTwo for reading");
printf("value one received: %s\n", mOne); close(fdOne);
close(vOne); 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] = {}; // Sollte man jemals aus der while(1)-Schleife ausbrechen:
read(vTwo, mTwo, sizeof(mTwo)); close(fdOne);
printf("value two received: %s\n", mTwo); close(fdTwo);
close(vTwo);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment