LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-21-2015, 03:18 AM   #1
redhavoc
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Rep: Reputation: 0
Detecting which X displays are free


Hello,
I am trying to run some distributed jobs, and unfortunately what I am running may try to display some unimportant graphics which I dont care, and thus crash if no display is present. For this reason I dispatch a job which first creates an Xvfb virtual framebuffer, and outputs everything on this display.

My problem seems to be in detecting which display is free, so that I can start an Xvfb on that display. I have come up with a bash script, which get a start display of 10000, and checks if there is a lock file for this display. If not it increments the display or else starts an Xvfb there. There is the chance that because of a race condition another process may try the same display, so I check the exit status and if it is not 0, I increment the display to use and try again.

Unfortunately in some occasions (about 5-8% of the jobs), something may go wrong, and I get the following errors
Cannot establish any listening sockets - Make sure an X server isn't already run
ning
_XSERVTransSocketINETCreateListener: ...SocketCreateListener() failed
_XSERVTransMakeAllCOTSServerListeners: server already running

Fatal server error:


The script I use to detect the display is the following
Code:
set disp=10000

while ( 1 )
   if ( -f "/tmp/.X$disp-lock" ) then
      set disp=`echo $disp + 1 | bc`
   else
      Xvfb :$disp -screen 0 800x600x24 &
      set xvfb_pid=`echo $!`
      sleep 2
      ps -p $xvfb_pid > /dev/null
      if ( $? == 0 ) then
         break
      endif
   endif
end

setenv DISPLAY :$disp
Any recommendations on my approach to detect free displays, or improvements on the script? Also any insight into what may be going wrong could help me work around the issue
 
Old 09-21-2015, 06:48 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Why don't you check if $disp + 1 lock file exists as well. You can't assume that if /tmp/.X1000-lock file exists that /tmp/.X1001-lock file does not.

Quote:
while ( 1 )
if [ -f "/tmp/.${disp}-lock ] then
disp=$(($disp + 1))
fi
Xvfb :${disp} -screen 0 800x600x24 &
.....
end
 
Old 09-21-2015, 09:28 AM   #3
redhavoc
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
I am not sure how your code checks that exactly.
What the current code I pasted does, is checking for lock files, if one exists increment the display number. If the lock file does not exist, then attempt to start Xvfb, and stil check if Xvfb started successfully.

So in theory I check all displays, until I find a display that has no lock file. And once I consider Xvfb to have started successfully I break the infinite while loop.

Unfortunately this method does not always work, so my criteria for successful Xvfb run is wrong. Also a completely different approach to finding free displays may exist and be far superior than mine.
 
Old 09-22-2015, 02:20 AM   #4
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
My bad. I misunderstood you script. It does what mine was aiming at. Just increase the display number till a free lock file is found.
I checked some man page and the net to find a way to get a free display number but did not find anything usefull within the X family programs. What I wonder if you can start xvfb without a display number and have it create some itself. You could then get the DISPLAY variable out of the enviroment file under /proc/$pid/.

Maybe you could create a lock file after finding a free display number. Then short to starting xvfb delete it again. Or maybe just bump up the increment from 1 to say 10. Or some other alternating numbers.
Nother approach would be to monitor the /tmp directory with say inotifywait.
 
1 members found this post helpful.
Old 09-22-2015, 04:56 AM   #5
redhavoc
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Unfortunately Xvfb will try display :0 if no argument is given, and will crash since :0 is taken. Also since Xvfb creates its own lock files I dont see the need. The jobs are dispatched by a central machine trhough the sun grid engine. So there is no backwards communicatiton, or control of which machine each job will go to so that I can control the display. Also there is a chance that two users may start submitting jobs, thus creating another potential conflict in displays.
So most suggestions will not work unfortunately, except maybe this inotifywait. I dont even know what it is so let me take a good look and see if I can use it on this case

Thanks for the suggestions
 
Old 09-22-2015, 08:00 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,220

Rep: Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952Reputation: 7952
you can use netcat to check if the port is in use: http://stackoverflow.com/questions/4...p-port-is-open. http://www.freesoft.org/CIE/Topics/113.htm

also you can use instead of:
set xvfb_pid=`echo $!`
just:
set xvfb_pid=$!
 
1 members found this post helpful.
  


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
Debian incorrectly displays free space Pantherman Linux - Hardware 2 05-27-2012 08:18 PM
Xorg or Gnome not properly detecting displays at startup. PeniWize Linux - Desktop 1 07-16-2010 12:37 PM
Help- Detecting free memory slots on motherboard kkoene Linux - Hardware 6 01-12-2009 08:40 AM
free -m command displays less RAM size babu198649 Linux - Newbie 6 05-08-2008 12:51 PM
FC5 installer not detecting disk's free space LloydM Fedora - Installation 7 03-28-2006 07:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration