The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Question on order of headers and WEXITSTATUS frequency8 High Level Programming 5 06-05-2007 10:28 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-14-2009
james2432 james2432 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 15
[SOLVED] C - WEXITSTATUS() question

I want to check if an application is still running under C. So far I've been using
Code:
int rc;
rc=WEXITSTATUS( [PID here] )

if(rc > 0)
{
   //then I exited
}
I was wondering seeing as WEXITSTATUS in man pages is close to the WAIT() if it was thread blocking and if it was how would I go about making my code non-blocking without threads?

Last edited by james2432; 10-19-2009 at 09:40 AM..
  #2 (permalink)  
Old 10-14-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
Code:
pid_t waitpid(pid_t pid, int *status, int options);
// use WNOHANG as the option
This returns the pid when if the process ended, or it checks and returns right away if the child process is still active.

---------- Post updated at 11:48 ---------- Previous update was at 11:00 ----------

If it is not a child process -

try
Code:
errno=0;
if(kill(pid, 0) == 0 )
{
    printf("process running\n");
}
else

{
    if(errno==ESRCH)
        printf("process stopped\n");
    else
    {
        perror("cannot signal the process");
        exit(1);
     }
}
  #3 (permalink)  
Old 10-14-2009
james2432 james2432 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 15
The thing is I'm creating a daemon to check that certain applications are running(via PID) I do not initially launch them(so they are not my children) and I don't really feel like trying to kill something that I'm to prevent from stopping. would waitpid still work even though it's not my child?
  #4 (permalink)  
Old 10-14-2009
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 427
if your just checking to see if PID exists. use kill(pid,0); see man page for details.
  #5 (permalink)  
Old 10-16-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
When you're writing a daemon consider using a lockfile to prevent two instacnes of the daemon from running. Otherwise the kill(pid,0) approach works fine.
Reply

Bookmarks

Tags
c language, wexitstatus()

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0