AIX/UNIX Command to determine the staus of a Unix process

I've a UNIX Process running that is waiting for an input. Once it gets the input it processes some information and comes back to waiting for the input.
Sometimes, I've to kill UNIX Processes for System Administration. Before killing this Unix Process, I've to make sure it's waiting at the input (it's ok to kill it then, otherwise it could be updating some files).
How can I determine that?
I've looked at the !ps -s command, but not sure if those stats can be used or how to manipulate them.
If you need more clarification, please let me know.

Thanks
rashidaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mliberiCommented:
the common way to do such a thing is to allow the process do filter received signals.

In such a programming schema the process disables interrupts during computing and enables them just after.

example:

#include <signal.h>
int  signal_received=0;

/* kill event handler */
void siglater(int i)
{
  signal(i,siglater);
  signal_received=i;
}

int main()
{
  while(!signal_received){  /* data input-processing cycle */
    read_data();
    signal(SIGTERM,siglater); /* install signal handler */
    process_data();
    signal(SIGTERM,SIG_DFL);
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ufolk123Commented:
This code may have some race conditions so a better solution is

            
               int  signal_received=0;

                   /* kill event handler */
                   void siglater(int i)
                   {
                     signal_received=i;
                   }

                   int main()
                   {
                 struct sigaction sMaskDfl,sMaskHnd;
                 sMaskDfl.sa_handler=SIG_IGN;
                 sMaskHnd.sa_handler=siglater;
                     sigaction( SIGTERM,  &sMaskDfl,0);

                     while(!signal_received){  /* data input-processing cycle */                        
                       read_data();
                       sigaction( SIGTERM,  &sMaskHnd,0); /* install signal handler */
                       process_data();
                       sigaction( SIGTERM,  &sMaskDfl,0);
                     }
                   }

But only of SIGTERM is used one need to add sigaction code for other signals also
if they are used to terminate the process.


Regards,
ufolk123
0
mliberiCommented:
ufolk123: can you please explain why using 'sigaction' instead of 'signal' is a better solution?

rashida:
please note that while 'process_data' function is executing your process continues receiving signals. After "signal(SIGTERM,siglater)" receiving a signal do not cause your application to exit, BUT any system call will exit with EINTR return code. That means that you must carefully test system call return codes.

To prevent your process to receive the signal you would like using 'sigsetmask' library function.

Example:



#include <signal.h>
int main()
{
  int forever=1, old_mask, new_mask;

  new_mask=(1<<(SIGINT-1));
  while(forever){  /* data input processing cycle */
    read_data();
    old_mask=sigsetmask(new_mask); /* prevent process from receiving SIGINT signal */
    process_data();
    sigsetmask(old_mask);
  }
}
0
ufolk123Commented:
mliberi:

The signal() based code is suffering from the classical race condition.
Suppose asignal SIGTERM comes ,so handler will enter .As Unix will reset the signal
handler to default before entering in the signal handler code.
So if another SIGTERM comes before  signal(i,siglater) inside signal handler code

gets executed , the default action will be done by OS and prog will terminate.

void siglater(int i)
                   {
                     /* If signal comes just here before call to signal happens*/
                     signal(i,siglater);
                     signal_received=i;
                   }


rashida:

As I written earlier it is better to block a set of signals instead of only SIGTERM
if you can more than one signal to terminate the application.

So a working solution may be...
main()
{
long newmask=sigmask (SIGUSR1) | sigmask (SIGUSR2) | ...          //List here all signals you can use for killing
long oldmask = sigblock (newmask)  

while()
{

/*** Non -critical Code ( Say read()) *****/
sigsetmask(oldmask);
process_read();
sigblock(newmask);
/****End of non critical block*****/

other processing
.......
}
......
Rest of code
}


So your signals will be processed only  in the Non-Criotical section.
Also there is not much use of getting knowledge of process state before
posting the signal as within the time you check and send the signal the state may change.
So ultimately either sigcation() or sigmask() based solution is good.
Actually as mliberi said you can have problems with sigaction() I feel that is not a
difficult as with sigaction also you can do something like
sigaction( SIGTERM,  &sMaskDfl,SA_RESTART);
which will automatically restart an interrupted system call if any

ufolk123
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.