Is there anyway to interrupt one program with another program? One program writes to a file, and the other one reads and prints a certain statement depending on what was read. I want the reading program to be interrupted when the writing program writes.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
You mean Program A has a file open for reading, then Program B wants to write to that file? What I think you want is to synchronize two processes, see these google links
wilifred.shayo 0 Newbie Poster
I like to know this program what will i do
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
If you are under a Unix/Linux environment, this is exactly what pipelines are for, and it is exactly what they do. In Windows, there is this "equivalent" (not really equivalent because it is an extremely convoluted and annoyingly verbose alternative, but it technically does the same thing).
Otherwise, a good alternative is to use sockets on a loopback connection (IP address: 127.0.0.1). It's a little bit more trouble than just using piping, but it is a bit more convenient (there are more features / options when creating a socket).
In both cases, with "blocking" behavior (which is the default, unless you specify that you want a non-blocking socket or pipe), you will have the "interruption" behaviour that you want. By the way, in computer science, the word "interrupt" means something quite a bit different, the term you ought to be using is "blocking" (versus "non-blocking").
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
If you are running Linux, there is the nice inotify api's that will let you know when a file/directory has been changed.
dalawh 0 Newbie Poster
@mike_2000_17
These two programs are connected to hardware. Program A writes something onto the file. Program B reads what is in the file and sends the appropriate commands to the hardware, which lights up certain lights. So this overall project includes two programs and one hardware. Will running it on a Linux server be a good idea since I have hardware in my plans?
I guess I would want blocking since I don't want program B to tell the hardware to do anything when program A is writing into the file. Would I still use piping/sockets for this?
Can you give me the defintions to interrupt, so I can get a better understanding of this?
@rubberman
Will inotify run in parallel with my program?
These do seem pretty helpful.
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
Will running it on a Linux server be a good idea since I have hardware in my plans?
Yes, hands down. That is, if you have a choice in the matter.
I guess I would want blocking since I don't want program B to tell the hardware to do anything when program A is writing into the file. Would I still use piping/sockets for this?
Yes, exactly. With blocking operations, what happens is that the program that reads (usually called the "consumer") will block (wait) until there is something new on its input stream before returning from the reading operation, and subsequently processing the data received. Setting things up correctly on the writer side (usually called the "producer") will make it so that the consumer won't start processing input until the producer wrote all that is needed.
Here is a simple example of writing two programs (producer-consumer) and piping them with each other:
producer.cpp:
#include <iostream>
#include <cmath> // needed for sin() function.
#include <unistd.h> // needed for Linux usleep() function.
using namespace std;
int main() {
// output the result of a sine-wave in time:
double t = 0.0;
while(t < 10.0) { // output for 10 seconds.
cout << (1.0 + sin(10.0 * t)) << endl; // output to standard-output (cout)
usleep(10000);
t += 0.01;
};
return 0;
};
consumer.cpp:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// calculate and print out a running average of the input values:
double avg = 0.0;
double current_value = 0.0;
while(cin >> current_value) { // read from input stream (as long as valid)
avg = 0.99 * avg + 0.01 * current_value; // compute a running average.
cout << "\r" << setw(10) << setprecision(6) << avg; cout.flush(); // output the running average.
};
cout << "\rInput stream has ended. Final running average is " << avg << endl;
return 0;
};
Now, you can compile both programs and run them individually (just to test). If you run the producer program, you will get an avalanche of numbers calculated from the sine-wave function. If you run the consumer program, it will not do anything until you enter some numbers (standard input), which it will then slowly take a running average of. As you see, the consumer program will only execute something when it receives a value to process, and will be asleep the rest of the time. But then, you can create a pipe between them. In Linux command line, the piping of two programs is done by using the |
separator between calling the two programs, as so:
$ ./producer | ./consumer
(assuming producer.cpp was compiled into the program "producer", and consumer.cpp was compiled into the program "consumer")
The output of the above program is a running display of the running average (around 1.0), followed by the output of the phrase "Input stream has ended. Final running average is 0.997201". By the way, this approach also allows you the flexibility of running both programs in a batch run, with a file output as intermediate. In other words, you could run your producer using $ ./producer > data.txt
, which will put all the produced data into a file (the symbol >
redirects the standard output to a file), and then you can run the consumer on that file using the command $ cat data.txt | ./consumer
(where cat
is a standard Linux program to output a file to the standard output, which is then piped to your consumer program). These are all tricks that you will appreciate a lot when running / testing / debugging your programs in your project.
Of course, piping can also be done programmatically (with the pipe() function for instance) if you need the additional control over the whole process. And, by the way, hardware communications in Linux (and in Windows for that matter) is also implemented through a piping mechanism. In Linux, hardware peripherals (like serial ports or USB ports) essentially look like files on the system, which you can open for read or write operations just as if you were dealing with a file or standard input-output. In other words, to transmit data to a device, you could just redirect / pipe your program's input-output to the proper stream (although in practice you will probably prefer opening the file the program).
Can you give me the defintions to interrupt, so I can get a better understanding of this?
Just refer to the wiki page.
Edited by mike_2000_17 because: little error
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.