Solved

Using system function to run xview

Posted on 2004-04-24
7
408 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:jewee
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10908893
You'd use 'fork()' and 'exec()' for that, e.g.

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);
    }
}
0
 

Author Comment

by:jewee
ID: 10909059
What header file do I need to include?
0
 
LVL 86

Expert Comment

by:jkr
ID: 10909088
You'll need

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 10909092
BTW, see also http://www.ecst.csuchico.edu/~beej/guide/ipc/fork.html ("A fork() Primer")
0
 

Author Comment

by:jewee
ID: 10909752
Thank you for your help!  I tried your code and noticed that the execl command doesn't work.  XView does not execute.
0
 

Author Comment

by:jewee
ID: 10910006
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10910150
>>and noticed that the execl command doesn't work.

My fault - if should be

       execl ( "xview", "xview", image, NULL);

:-(


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now