UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2009-11-11 08:59 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: kill function returned value

When using kill function(kill(pid,0)) to check whether the process is exist on Linux, the kill returned value is zero, but with the ps command to check the pid, it is not exist, anyone can give some suggestions?

Offline

#2 2009-11-11 01:25 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: kill function returned value

Can you post your code? You never know.

Other than that, the most obvious this is that you either used ps with
options that don't show the process with the pid you used, or that the
process exited between the kill() and the ps execution.

Offline

#3 2009-11-12 02:14 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: kill function returned value

The code looks like the followings:

int ret;     
  if(proc_pid > 0)
        {
               ret = kill(proc_pid, 0);
                if(ret == 0)
                {
                        cout<<"process is already exist"<<endl;
                 }
                else
                   {
                       cout<<"process is not exist"<<endl;
                  }
        }


The above code output "process is already exist" message, but using the command " ps -ef " to check the pid, it does not exist.
Right after use the above program to check,we soon use the command to check, it not exist. after that, retry to check using the program again, it still exist.
At the same time, we check the /proc directory, pid directory of the process is not existed.

Offline

#4 2009-11-12 05:57 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: kill function returned value

The code part where you declare and assign proc_pid is missing, it's rather
important. Add a printf before the kill(2) to make sure it has the correct
value (or add the proc_pid to the current 'cout').

Or test it with bogus pids like 999999.

Offline

#5 2009-11-13 02:41 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: kill function returned value

Well, because the above code is a part of program,  the proc_pid variable has been defined as pid_t,  its value has been assigned by the other call function while the process starting up, so it  could been sure correct. Moreover, the proc_pid value must be within the system limit instead of the exceptional one while we test.

Offline

#6 2009-11-13 03:00 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: kill function returned value

I'm with i3839: "proc_pid" likely isn't set to the same value you're looking for with "ps"...
Just print out the value of "proc_pid" in your messages there, and you'll see...

Alternatively, it's a short-lived process and exits very soon after your kill(), but before
the "ps" check...  How about you surround your kill() call with two system()'d calls
to "ps", and let IT do the ps checking both before and after its own kill()...  Then, you
can be pretty much sure of what's going on: if the PID really isn't there in the ps
output immediately before or after the kill(), yet the kill() still succeeds, then you've
probably found some weird kernel bug or something...  But, I really tend to doubt it...
More likely, you'll find the process IS there immediately before and/or after the successful
kill()...

Offline

Board footer

Powered by FluxBB