LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Closing applications using Xlib (https://www.linuxquestions.org/questions/programming-9/closing-applications-using-xlib-77239/)

stoney79 07-30-2003 05:30 AM

Closing applications using Xlib
 
Hi there,

I'm pretty much a newbie to Linux, and especially to Xlib so sorry, but I'm going to ask a pretty basic question that I haven't found a solution to yet :confused:

I'm coding a system that has a number of open web browsers and I want to be able to select a particular window and resize and close it.

I've figured out how to get a handle to the window, but I haven't figured out how to close the application. All I can find is XKillClient, but this seems to be tidying up resources after a process has exited. I actually want to kill that process. Is this possible, or is there a better way of doing this? I originally looked at pids, but I didn't think that was a very good way of doing it.

All help would be gratefully received, as I'm finding this Linux thing quite hard going at the moment!

Many thanks,

:newbie:

DIYLinux 07-30-2003 06:49 AM

Have a look at the manpage kill(2), http://man.linuxquestions.org/index....ction=2&type=2

You should send the victim a SIGTERM if you are polite. If it REALLY has to go NOW, send SIGKILL. In the latter case, the victim cant handle the signal, so it will not clean up any temporary files or other housekeeping work.

And remember: Processes are controlled by the kernel, not the windowing system. You can run Linux without the X Windows.

stoney79 07-30-2003 07:22 AM

Thanks very much for your help!

Hmm, is there anyway to map the processes onto the windows?

Basically, I couldn't figure out a neat way in which to return the process from a system() call, e.g.

system("galeon -openURL(/"www.google.com/")&");

or something like that. The only way I figured to do this was by using what is returned from "galeon -openURL(etc...)", which is the PID - and then piping that into a file which I could read and store in the app. Rather messy really!

This lead me to looking at the window handle, using XQueryTree(I need to do this anyway for the resizing part). I thought XKillClient called on the window would kill the process, but having re-read this I'm not so sure.

So I think using the kill command is the best way to do it, but then my next question would be how to I get the process ID from an application into my program?

P.S. - If there is a way to kill an application from its window, then that would be ideal, as I've already got the window handles. Clearly it must be possible, since each window has a close button...

DIYLinux 07-30-2003 08:46 AM

To get the PID of the child process you created, you will have to create your own version of system.

Here the steps
1. fork of a child
2. in the child, where fork returns 0, call one of the exec functions to start say galeon. The system function execs /bin/sh.
3. in the parent, where fork also returns, with the PID as result, record this PID.
4. Use the PID to control the child, kill it or wait for termination. See kill, wait, wait3 and waitpid.

The man page for system has an example. See http://man.linuxquestions.org/index....ction=3&type=2

Nice idea: you can also create an anonymous pipe using the pipe command before forking. Connect it to the childs standard input, using dup2, before calling exec, and open galeon on /dev/stdin. Now you have your own webserver with integrated client.

stoney79 07-30-2003 09:34 AM

Doesn't the fork duplicate the same process (and its resources)? I've got quite limited resources and a lot going on in this application, so don't really want to do this (particularly if I have say 10 of them...).

My colleague has done something similar before I went down the window management route, but decided against it due to the resource hog. At least we're thinking on the same lines though! Thanks so much for the help, but I was kinda hoping there would be a simpler way of doing things?

(An answer of 'no, there isn't' would also be useful...!)

DIYLinux 07-31-2003 05:26 AM

System also forks, so there is no real choice.

Note that the child process has copy-on-write access on the parent, except for the stack segment which is private. If you call exec, new memory will be allocated for the program and data you load, if it is loaded for the 1st time. Linux will let programs and libraries share memory for their code segments with other instances of the same program.

So you only pay for:
- stack segment and writable segments
- kernel structures allocated for each program, like the open file.

stoney79 08-01-2003 03:21 AM

Erm, ok - I'm getting a bit lost here on what all these terms mean... I'm not a complete novice to computers, but just haven't dealt much with Linux in the past...

I've tried looking at XDestroyWindow and XDestroySubWindows, but this only kills the window and not the process. I then tried calling XKillClient(display, AllTemporary), but this didn't seem to work either.

So you think that forking is the only way to go?

DIYLinux 08-01-2003 06:20 AM

You will need to create a new process if you want to run a browser. No choice here im afraid.

If you dont fork, exec will replace the current process, and you will lose all state information. When the browser exits, your old process wont come back either. Not even if its called arnie:cool:


All times are GMT -5. The time now is 06:14 AM.