C - WEXITSTATUS() question


 
Thread Tools Search this Thread
Top Forums Programming C - WEXITSTATUS() question
# 1  
Old 10-14-2009
[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 10:40 AM..
# 2  
Old 10-14-2009
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  
Old 10-14-2009
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  
Old 10-14-2009
if your just checking to see if PID exists. use kill(pid,0); see man page for details.
# 5  
Old 10-16-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

wc question

I am new to unix. how to get only the no. of lines output from the wc -l command. Input: wc -l filename it gives <no.of lines> filename i need only the interger value. as <no.of lines> Thanks in advance (2 Replies)
Discussion started by: sanvel
2 Replies

2. Programming

help with WEXITSTATUS in C

when a execute this line printf("%d\n",WEXITSTATUS(status)); i get no output, whats could be the possible causes? ---------- Post updated at 09:29 PM ---------- Previous update was at 09:27 PM ---------- nvm i figured it out, u can delete this thread (0 Replies)
Discussion started by: omega666
0 Replies

3. Programming

C++ little question

Hi, I am doing a C++ self-study and I got stuck with this problem. I want to have a code that asks the suer to enter two numbers and then it lists the numbers between these two numbers. It has also to print a message if these two numbers are equal. Here is what I wrote: #include <iostream>... (11 Replies)
Discussion started by: faizlo
11 Replies

4. Programming

Question on order of headers and WEXITSTATUS

In one of the Unix Programming FAQ's they have the following headers in the program to catch SIGCHLD #include <sys/types.h> /* include this before any other sys headers */ #include <sys/wait.h> /* header for waitpid() and various macros */ #include <signal.h> /* header for signal... (5 Replies)
Discussion started by: frequency8
5 Replies

5. UNIX for Dummies Questions & Answers

question?

Is there any way to use sed and count the number of alphabetic characters in a sentence? (4 Replies)
Discussion started by: brentdeback
4 Replies

6. UNIX for Dummies Questions & Answers

another question?

what happens if the script doesn't get Y,y,N, or n? Will it just loop back up and ask the question again? I tried to get mine to give me another response to tell me my input was invalid, but ran into problems with it. (8 Replies)
Discussion started by: wmosley2
8 Replies

7. UNIX for Dummies Questions & Answers

Next Question:

what is the function of swap in linux why i have to create apsolutely a particion for the swap when i install (i installed lnx4win mandrake and made an automat. disk particion and the install program one of my disk partitions that was 3gb devidet in 4 one native 700mb swap 600mb and the others i... (1 Reply)
Discussion started by: user666
1 Replies

8. UNIX for Dummies Questions & Answers

Well, im getting it, but i have ONE question

Hay everyone, i would like to take this opportunity to thank all of you who helped me make the decision to get a linux distro. As a newbie, Im defininately considering buying Mandrake Linux... I went to the site and phew..... 2300 applications, i think ill have a good time. But i do have a... (5 Replies)
Discussion started by: LolapaloL
5 Replies
Login or Register to Ask a Question