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 <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;
}
No preview for this file type
#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;
}
write(vOne, message, sizeof(message));
close(vOne);
// 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);
}
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;
}
No preview for this file type
#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>
static const char *PIPE_ONE = "/tmp/pipeOne";
static const char *PIPE_TWO = "/tmp/pipeTwo";
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)
int main(void)
{
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;
}
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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment