ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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!
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.
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...
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.
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.
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...!)
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.