Bookstore

Recent

Author Topic: [SOLVED] How to check if linux proces is running  (Read 13510 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
[SOLVED] How to check if linux proces is running
« on: February 12, 2011, 01:22:58 pm »
Hi,

I'm writting comunication between two applications (client and server) on linux (using "System V IPC"). I need solution to check if server still running. There is many way to do this when we assume that server is closing normaly (e.g. creating and deleting file in known place). But when user kill server from task manager then client think that server still running because file still exists. On windows, I open TFileStream with "fmCreate or fmShareDenyWrite or fmShareDenyRead" flags and if client can't read this file that means that server running. Even when server is killed, then handle to file is unlocked. But on linux I can open and edit files with this flags (this is a bug or linux feature?)
Solution should be fast too because I want check this before send each command to server.

Regards
« Last Edit: February 14, 2011, 12:52:58 pm by Dibo »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8041
Re: How to check if linux proces is running
« Reply #1 on: February 12, 2011, 04:40:03 pm »
The only trick that I know is to have the client create a file with a known part (e.g. myserver-) and the PID of the process.

The client on startup then checks /tmp (or /var/run which iirc is specially for such files) for files starting with myserver- and finds the PID of the process. Then it tries to find out (e.g. in /proc) if that PID is still running.

a different approach is making it a TCP/IP server listening on some port.

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to check if linux proces is running
« Reply #2 on: February 12, 2011, 05:13:34 pm »
Hm, PID trick looks interesting. How linux increments PID? Once created PID is unique for whole user session? I'm mean, is possible situation:
1. Server starting with some PID and write it to known file
2. Client check this PID and if that proces exists, he mark status as connected
3. Server is killed by user.
4. Linux create some proces with PID which previously had server
5. Client check for PID again and see that server still exists (but that is another application)

Is this scenario possible? Can linux assign this same PID (released) for another application?

In the meantime I found temporary solution with "shared memory". I create 1 byte segment with some key and attach server to this segment. Even if I kill server, linux kernell decrement attached applications to this memory segment. Client can get number of attached apps to this segment.
Quote
a different approach is making it a TCP/IP server listening on some port.
I considered this way, but I must write one of clients in pure C (to much C headers to translate to FPC) and TCP client is to much tricky for me. I am not familiar in C

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8041
Re: How to check if linux proces is running
« Reply #3 on: February 12, 2011, 05:22:04 pm »
Hm, PID trick looks interesting. How linux increments PID? Once created PID is unique for whole user session? I'm mean, is possible situation:

No, PIDs are not guaranteed unique, and in some systems you might actually see wrapping around. Still in such case the number of PIDs must rise very fast, AND at the same time something that keeps running (and not something that starts and directly quits) must occupy the same number after wrapping.

This chance is not very high.

Bart

  • Hero Member
  • *****
  • Posts: 3716
    • Bart en Mariska's Webstek
Re: How to check if linux proces is running
« Reply #4 on: February 13, 2011, 01:35:57 pm »
Maybe I'm missing the point here (I'm relatively unexperienced with *nix). But if you know the PID, can't you use ps (or somethign similar) to see if this PID is still the PID of the process you ecpect it to be?

Bart

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to check if linux proces is running
« Reply #5 on: February 14, 2011, 12:24:51 am »
Sorry for delay. Referring to the PID, if I know server PID and I know that it exists in /proc, can I somehow get some details about this proces, like executable name, up time etc? As said Bart, exists any free pascal function of linux PS command?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1890
Re: How to check if linux proces is running
« Reply #6 on: February 14, 2011, 11:25:18 am »
exists any free pascal function of linux PS command?

Why not simply call PS?
Here is an old example, hope it still compiles.

http://www.lazarusforum.de/viewtopic.php?p=9121#p9121
« Last Edit: February 14, 2011, 11:27:13 am by theo »

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to check if linux proces is running
« Reply #7 on: February 14, 2011, 12:52:36 pm »
Thanks theo!

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8041
Re: How to check if linux proces is running
« Reply #8 on: February 14, 2011, 10:05:55 pm »
Sorry for delay. Referring to the PID, if I know server PID and I know that it exists in /proc, can I somehow get some details about this proces, like executable name, up time etc?

On systems that have /proc, maybe. But even if it is the same binary, do you really know for sure it is the same instance?

Quote
As said Bart, exists any free pascal function of linux PS command?

No. PS is a system command, and thus doesn't necessarily use documented interfaces.

flames

  • New member
  • *
  • Posts: 5
Re: [SOLVED] How to check if linux proces is running
« Reply #9 on: September 11, 2011, 05:01:36 am »
I'm using FileOpen with fmShareExclusive for both locking and checking.
FileOpen will return error -1, if file is already opened with fmShareExclusive.

Works fine on linux.

If you use same method on Windows, FileOpen will succeed regardless, i don't know is this a bug or intended.
But then you can try to read  with FileRead, where it will fail if locked with fmShareExclusive.
« Last Edit: September 11, 2011, 05:07:14 am by flames »