I am using C to write a number to a named pipe. I want to make a write every 1 second, thus I am using code similar to this, to set a timer:
need programs that illustrate use of settimer and alarm functions in GNU C
And in the DoStuff() routine which is called every 1 second from the timer above, I am opening a named pipe with mkfifo() and writing to it using write().
Then I am calling it with read from a separate program.
Now, the thing is, the other program may only call 'read' every 3 seconds, for example. However. I still want the 'write' to occur every 1 second.
Basically, I want the first program to do this:
- Set up a timer for 1 second interval.
- Every 1 second, write a number to pipe.
- Double the number (up to a certain max).
Then the second program to do this:
- Every 3 seconds (or whenever, this is variable), read from the pipe.
- Read the number that was MOST RECENTLY written to the pipe.
I.e., if both programs are started, this might happen:
- Second 1: First program writes "1" to pipe.
- Second 2: First program writes "2" to pipe.
- Second 3: Second program reads "2" from pipe AND First program writes "4" to pipe.
- Second 4: First program writes "8" to pipe.
- Second 5: First program writes "16" to pipe.
- Second 6: Second program reads "16" from pipe AND First program writes "32" to pipe.
But what ends up happening is this:
- Second 1: First program writes "1" to pipe.
- Second 2: Nothing
- Second 3: Second program reads "1" from pipe AND First program writes "2" to pipe.
- Second 4: Nothing
- Second 5: Nothing
- Second 6: Second program reads "2" from pipe AND First program writes "4" to pipe.
I get that pipe write is blocking until it gets read, so it seems that it prevents the timer in the first program from continuing until the second program reads it. What would be the best way to handle making it non blocking, or are there any other solutions? Sorry, I am very new to piping and forking, and while I think I understand the general direction to go in, I'm fuzzy on the specifics and would appreciate any veteran advice.
Thank you all.
Edit: According to the first responder below, the method I'm using may not be the right approach at all, so I'm going to describe the problem in general to ask for further suggestions.
Program 1 needs to update a variable every 1 second, while it's simultaneously doing other things.
Program 2 needs to be able to access the most recent value of that variable.
My initial thought was to have a timer in program 1 to update the variable every 1 second, then to have program 2 read the variable via a pipe. I was trying to use setitimer() and signal() to do the update, but then every time I write to pipe, it becomes blocked until read, which means that the every-second update may not happen if it's not being read fast enough.
General suggestions for how to approach this most simply? I have little experience with forked processes and threading. Thank you.
I am considering that the simplest solution may be to use the timer that I am already using, but simply have it write the value to an actual file, which the other program can then read. That way there's no piping, so no blocking. Does that seem reasonable?
Or what if, in the second program, I had a second timer loop going to read from the pipe every 1 second?