jewee
asked on
Using system function to run xview
I am writing C++ code (Linux) to display an image file using xview. After the image is displayed for 10 seconds, how do I go about displaying the next file considering that I cannot launch xview again unless I close out the original display?
I tried this:
system("xview imageFile.pbm");
sleep(10);
system("xview imageFile2.pbm");
The only way I can shut down the application if I do a CTRL-C. How can I capture those keystrokes and use it within the system function? The only other way, which I was hoping I wouldn't have to do is use the system function to write out the process ids to a file then read that file and get the process id that corresponds to the xview.
I tried this:
system("xview imageFile.pbm");
sleep(10);
system("xview imageFile2.pbm");
The only way I can shut down the application if I do a CTRL-C. How can I capture those keystrokes and use it within the system function? The only other way, which I was hoping I wouldn't have to do is use the system function to write out the process ids to a file then read that file and get the process id that corresponds to the xview.
ASKER
What header file do I need to include?
You'll need
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
BTW, see also http://www.ecst.csuchico.edu/~beej/guide/ipc/fork.html ("A fork() Primer")
ASKER
Thank you for your help! I tried your code and noticed that the execl command doesn't work. XView does not execute.
ASKER
I tried entering the path of xview but it said "image not found". For the image, I included the path but it still didn't work.
I noticed that if I use the same code you provided me, but instead of using execl, I used system, then incremented the pid after, it worked but would only launch xview once.
I noticed that if I use the same code you provided me, but instead of using execl, I used system, then incremented the pid after, it worked but would only launch xview once.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
while ( true) {
char* image = "imageFile.pbm";
pid_t pid;
pid = fork ();
if ( 0 == pid) { // child process, spawn xview
execl ( "xview", "xview", image);
} else { // main process, wait 10s, then kill xview and restart it again
sleep ( 10);
kill ( pid, SIGKILL);
}
}