Results 1 to 2 of 2
Hi everybody!
I've got to write a "background clock": current time must be printed in a certain place of the terminal window. I'm new to linux and dealing with processes. ...
- 12-22-2010 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 1
Background process
Hi everybody!
I've got to write a "background clock": current time must be printed in a certain place of the terminal window. I'm new to linux and dealing with processes. But as far as I understand I have to create a daemon. After looking through a number articles on that topic I found out that such kind of things are realized by forking and exiting "the parent-process".
I've tried something like:
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
And after that: saving cursor position, choosing the necessary position, writing down the number (let's say variable "time" has the correct value) and returning back:
while (1) {
printf("\033[s");
printf("\x1b[12;39H");
printf(time);
printf("\033[u");
}
The value is printed in the correct place, but the process is not working at the background: I can't deal with other commands, can't use the terminal.
Would anybody tell me, how to realize this background process correctly?
- 12-22-2010 #2
Well, there's a basic problem with this approach, which is that there's nothing in place to share access to the terminal in a robust way. For instance, when you save the cursor position and then move the cursor - there's no guarantee that another program wouldn't choose that particular moment to write to the screen, possibly moving the cursor again or reverting it to where it was, etc...
But let's assume that's not the problem here. How frequently are you updating this clock? You aren't showing your full program here - but if there's not a call to sleep() or something, then your clock will be updating on a constant basis, which is going to make it really tough to do anything else. (It will flood the TTY with a constant stream of data, it will increase the amount of time that the cursor is at the clock's position to almost 100%, etc.)
I messed around with your code and came up with this:
It seems to work. There are some annoyances, though: Terminal scrollback is effectively useless (Since the clock updates every second, the terminal keeps jumping back to the end of the scrollback buffer to show you the updated display) - and the clock clobbers anything else that happens to be displayed at that point on the screen - and if you scroll down a line, the clock winds up in a different place than the one that's going to be drawn to replace it...Code:#include <stdio.h> #include <time.h> #include <unistd.h> int main() { pid_t pid; time_t now; pid = fork(); if (pid < 0) { return -1; } else if (pid > 0) { return 0; } while (1) { now = time(0); printf("\033[s"); printf("\033[12;39H"); printf("%lu", now); printf("\033[u"); fflush(stdout); sleep(1); } return 0; }
If you want a robust solution, you're probably better off just running GNU screen. It's a program that's designed to allow multiple programs to share the terminal (imagine "xterm" implemented as a console program), and it has a built-in clock.


Reply With Quote
