Last active
November 20, 2023 20:22
-
-
Save punzik/c6f550b2cdec626a13d37bda39669850 to your computer and use it in GitHub Desktop.
Print to terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#define FIFO_FILE "/tmp/veri.log" | |
#define TERM_EMU_CMD "xterm -e 'cat /tmp/veri.log'" | |
volatile int running = 1; | |
void ctrlc_handler(int unused) | |
{ | |
running = 0; | |
} | |
int main(void) | |
{ | |
signal(SIGINT, ctrlc_handler); | |
if (access(FIFO_FILE, W_OK) != 0) { | |
if (mkfifo(FIFO_FILE, 0666) != 0) { | |
printf("Make fifo error\n"); | |
return -1; | |
} | |
} | |
switch(fork()) { | |
case -1: | |
printf("Fork error\n"); | |
remove(FIFO_FILE); | |
return -1; | |
case 0: | |
execl("/bin/sh", "sh", "-c", TERM_EMU_CMD, (char*)NULL); | |
return 0; | |
} | |
FILE *f = fopen(FIFO_FILE, "a"); | |
for (int n = 0; (n < 1000) && running; n ++) { | |
fprintf(f, "Fork: N = %i\n", n); | |
fflush(f); | |
printf("Main: N = %i\n", n); | |
sleep(1); | |
} | |
fclose(f); | |
remove(FIFO_FILE); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment