LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-30-2003, 05:30 AM   #1
stoney79
LQ Newbie
 
Registered: Jul 2003
Posts: 12

Rep: Reputation: 0
Unhappy 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

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,

 
Old 07-30-2003, 06:49 AM   #2
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
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.
 
Old 07-30-2003, 07:22 AM   #3
stoney79
LQ Newbie
 
Registered: Jul 2003
Posts: 12

Original Poster
Rep: Reputation: 0
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...
 
Old 07-30-2003, 08:46 AM   #4
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
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.
 
Old 07-30-2003, 09:34 AM   #5
stoney79
LQ Newbie
 
Registered: Jul 2003
Posts: 12

Original Poster
Rep: Reputation: 0
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...!)
 
Old 07-31-2003, 05:26 AM   #6
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
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.
 
Old 08-01-2003, 03:21 AM   #7
stoney79
LQ Newbie
 
Registered: Jul 2003
Posts: 12

Original Poster
Rep: Reputation: 0
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?
 
Old 08-01-2003, 06:20 AM   #8
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Linux - Newbie 10 02-07-2014 12:53 AM
Closing applications to the system tray ThePenguin Linux - Newbie 1 09-06-2004 01:11 AM
Who is Knowing about daemon applications and how to develop these applications? ms_890 Linux - Software 0 04-14-2004 02:04 AM
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Programming 1 03-02-2004 08:22 AM
Xlib help what to do? micxz Linux - General 4 03-10-2003 01:39 PM


All times are GMT -5. The time now is 08:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration