Sponsored Content
Top Forums Programming How to implement SIGKILL and SIGTERM and print a message? Post 302144882 by norelco55 on Sunday 11th of November 2007 10:04:48 AM
Old 11-11-2007
Thanks for all the help. I have everything working except KIll -9, But I am learning in stages thanks to you all Smilie.


questions:
1. I am still confused about using atexit(). Isn't wastefull to resources to call another function that just exits? All I need to do really is call exit(); I havent implemented logging or anything and all filehandles and kids are closed.

2. I still need to work on the printfs..I just got back from AC at 2am. Banged this out when I got back--haha.


3. "you have done sigfillset but done nothing with the sigset_t, you need to call sigprocmask to block or unblock those signals."

I am still working on this one!!

4. There has to be a way to at least know sigkill is coming so I can print before it happens. I say this because some linux distros show the sigkill or are they faking it with sigterm? I did notice sigterm and sigkill are always together.


Here is what I Have now: Feel free to critique(so I don't learn bad Habits) Smilie.

So, is flush in or out? I am thinking, I dont need it because when I exit, that should clean up right?


Code:
static struct sigaction act; 


sigemptyset (&act.sa_mask);
  act.sa_flags = 0;

  /* Register the handler for SIGINT To Ignore. */
  act.sa_handler = SIG_IGN;
  sigaction (SIGINT, &act, NULL);

  /* Register the handler for SIGHUP To Ignore */
  act.sa_handler = SIG_IGN;
  sigaction (SIGHUP, &act, NULL);


       //calls function catcher.

  /* Register the handler for SIGTERM. */
  act.sa_handler =  catcher;
  sigaction (SIGTERM, &act, 0);


  //SIGKILL CANNOT BE CAUGHT!! KILL -9
  /* Register the handler for SIGKILL. */
  act.sa_handler =  catcher;
  sigaction (SIGKILL, &act, 0);




=======================================================
//gracefully quit on sigterm and sigkill.
void catcher(int sig)
{

  printf ("Caught signal %d\n", sig);
  fflush(stdout);
  switch (sig)

   case SIGTERM:

          printf("exiting sigterm");
          exit(1);
          fflush(stdout);
          break;

        case SIGKILL:

          printf("sigkilla");
          exit(1);
          fflush(stdout);
          break;
  }
}   // end function catcher

 

10 More Discussions You Might Find Interesting

1. Programming

signals - SIGTERM

Hi all, I need some urgent help. we are using Dynix/ptx V4.5 on i386, have several processes and instances are running on the box round the clock.we increased the processes recently. We have coded to handle the signals in our programs. Recently, we noticed most of our processes are... (2 Replies)
Discussion started by: reddyb
2 Replies

2. Shell Programming and Scripting

Why SIGKILL will occur?

Hi Gurus, I am executing my Datastage jobs on UNIX operating System. While running the jobs i am getting the following error: main_program: Unexpected termination by Unix signal 9(SIGKILL) Can any one please let me know what are the possible situations where this SIGKILL will arrise? ... (9 Replies)
Discussion started by: choppas
9 Replies

3. Solaris

To print Coloured Prelogin Message on SOLARIS--9................

Hiiii..... Every one...... I am using /etc/issue file to display Pre-login Message on my system, installed with SOLARIS-9. I am getting this Message in White fonts having Black background ( Colour of the Screen)..... So, is there ... (2 Replies)
Discussion started by: prashantshukla
2 Replies

4. Shell Programming and Scripting

Error while trying to print message

Hi all Geting this error while trying to print message as : ./logfunc: print: bad file unit number heres what i m trying to do : log_date="$(date '+%d/%m/%Y %H:%M:%S')" log_type="Message" print "${log_date}: ${log_type}" print -u3 "${log_date}: ${log_type}" this error is due to... (3 Replies)
Discussion started by: Navatha
3 Replies

5. Shell Programming and Scripting

Print a message at specific line on prompt

Hi Friends, I am using HP-UNIX(ksh). I want to print a message at specific line on the prompt screen. For Example: for num in 1 10 3 145 do echo $num // need to print this on the same line for each number sleep 2 done Actual Output: ========== 1 10 3 145 Expected Output:... (5 Replies)
Discussion started by: Niroj
5 Replies

6. Solaris

SIGQUIT and SIGKILL message

Dear All, I have machine with SunOS 5.10 Generic_138888-01 sun4v sparc SUNW,SPARC-Enterprise-T5120. Yesterday there is something at dmesg command : May 25 18:09:02 cacao_launcher: Timeout occured on heartbeat channel, cleanup engaged May 25 18:09:07 cacao_launcher: watchdog : warning,... (0 Replies)
Discussion started by: mbah_jiman
0 Replies

7. Shell Programming and Scripting

How to detect SIGTERM,SIGKILL signal in UNIX

Dear All We have JBOSS server running on Linux we need to track Graceful Shutdown(SIGTERM) and Forceful Shutdown(SIGKILL) timestamp and write it into one file, I am new to UNIX Signal processing if is it possible how to detect it? We generally do $kill PID For Graceful... (5 Replies)
Discussion started by: mnmonu
5 Replies

8. Shell Programming and Scripting

SIGSTOP and SIGKILL

Which is sent to a terminal when it closes? SIGKILL? Reason I ask is I have a script I want to run in the background, but want it to run even if the terminal window is closed. Or, I'd like it to background itself if the terminal is closed but not if its running in an open window. I will learn how... (5 Replies)
Discussion started by: DC Slick
5 Replies

9. UNIX for Advanced & Expert Users

Print message while using sqlplus

I want to connect to oracle database from solaris... After that i will drop and create a no.of tables.One of the table example is as below. sqlplus -s usrname/password@dbname << SQL >> $logfile 2>&1 echo " dropping the table1" | tee logfile DROP TABLE Table1 echo "creating the table1" |... (1 Reply)
Discussion started by: millan
1 Replies

10. Shell Programming and Scripting

Script to check the string in a file and print an attribute in the message

Hi, I am new to shell scripting and got a task to complete. Task is : we have a log file where in i need to traverse through the whole file to check the string "Person Type missing in message" and after that i need to get EMPLID=xxxxxx from the file and print details in a different file. ... (1 Reply)
Discussion started by: suren424
1 Replies
thr_sigsetmask(3C)														thr_sigsetmask(3C)

NAME
thr_sigsetmask - change or examine calling thread's signal mask SYNOPSIS
cc -mt [ flag... ] file... [ library... ] #include <thread.h> #include <signal.h> int thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset); The thr_sigsetmask() function changes or examines a calling thread's signal mask. Each thread has its own signal mask. A new thread inher- its the calling thread's signal mask and priority; however, pending signals are not inherited. Signals pending for a new thread will be empty. If the value of the argument set is not NULL, set points to a set of signals that can modify the currently blocked set. If the value of set is NULL, the value of how is insignificant and the thread's signal mask is unmodified; thus, thr_sigsetmask() can be used to inquire about the currently blocked signals. The value of the argument how specifies the method in which the set is changed and takes one of the following values: SIG_BLOCK set corresponds to a set of signals to block. They are added to the current signal mask. SIG_UNBLOCK set corresponds to a set of signals to unblock. These signals are deleted from the current signal mask. SIG_SETMASK set corresponds to the new signal mask. The current signal mask is replaced by set. If the value of oset is not NULL, it points to the location where the previous signal mask is stored. Upon successful completion, the thr_sigsetmask() function returns 0. Otherwise, it returns a non-zero value. The thr_sigsetmask() function will fail if: EINVAL The value of how is not defined and oset is NULL. Example 1: Create a default thread that can serve as a signal catcher/handler with its own signal mask. The following example shows how to create a default thread that can serve as a signal catcher/handler with its own signal mask. new will have a different value from the creator's signal mask. As POSIX threads and Solaris threads are fully compatible even within the same process, this example uses pthread_create(3C) if you exe- cute a.out 0, or thr_create(3C) if you execute a.out 1. In this example: o The sigemptyset(3C) function initializes a null signal set, new. The sigaddset(3C) function packs the signal, SIGINT, into that new set. o Either pthread_sigmask() or thr_sigsetmask() is used to mask the signal, SIGINT (CTRL-C), from the calling thread, which is main(). The signal is masked to guarantee that only the new thread will receive this signal. o pthread_create() or thr_create() creates the signal-handling thread. o Using pthread_join(3C) or thr_join(3C), main() then waits for the termination of that signal-handling thread, whose ID number is user_threadID. Then main() will sleep(3C) for 2 seconds, after which the program terminates. o The signal-handling thread, handler: o Assigns the handler interrupt() to handle the signal SIGINT by the call to sigaction(2). o Resets its own signal set to not block the signal, SIGINT. o Sleeps for 8 seconds to allow time for the user to deliver the signal SIGINT by pressing the CTRL-C. /* cc thisfile.c -lthread -lpthread */ #define _REENTRANT /* basic first 3-lines for threads */ #include <pthread.h> #include <thread.h> thread_t user_threadID; sigset_t new; void *handler(), interrupt(); int main( int argc, char *argv[] ){ test_argv(argv[1]); sigemptyset(&new); sigaddset(&new, SIGINT); switch(*argv[1]) { case '0': /* POSIX */ pthread_sigmask(SIG_BLOCK, &new, NULL); pthread_create(&user_threadID, NULL, handler, argv[1]); pthread_join(user_threadID, NULL); break; case '1': /* Solaris */ thr_sigsetmask(SIG_BLOCK, &new, NULL); thr_create(NULL, 0, handler, argv[1], 0, &user_threadID); thr_join(user_threadID, NULL, NULL); break; } /* switch */ printf("thread handler, # %d, has exited ",user_threadID); sleep(2); printf("main thread, # %d is done ", thr_self()); return(0) } /* end main */ struct sigaction act; void * handler(char *argv1) { act.sa_handler = interrupt; sigaction(SIGINT, &act, NULL); switch(*argv1){ case '0': /* POSIX */ pthread_sigmask(SIG_UNBLOCK, &new, NULL); break; case '1': /* Solaris */ thr_sigsetmask(SIG_UNBLOCK, &new, NULL); break; } printf(" Press CTRL-C to deliver SIGINT signal to the process "); sleep(8); /* give user time to hit CTRL-C */ return (NULL) } void interrupt(int sig) { printf("thread %d caught signal %d ", thr_self(), sig); } void test_argv(char argv1[]) { if(argv1 == NULL) { printf("use 0 as arg1 to use thr_create(); or use 1 as arg1 to use pthread_create() "); exit(NULL); } } In the last example, the handler thread served as a signal-handler while also taking care of activity of its own (in this case, sleeping, although it could have been some other activity). A thread could be completely dedicated to signal-handling simply by waiting for the delivery of a selected signal by blocking with sigwait(2). The two subroutines in the previous example, handler() and interrupt(), could have been replaced with the following routine: void * handler(void *ignore) { int signal; printf("thread %d waiting for you to press the CTRL-C keys ", thr_self()); sigwait(&new, &signal); printf("thread %d has received the signal %d ", thr_self(), signal); } /*pthread_create() and thr_create() would use NULL instead of argv[1] for the arg passed to handler() */ In this routine, one thread is dedicated to catching and handling the signal specified by the set new, which allows main() and all of its other sub-threads, created after pthread_sigmask() or thr_sigsetmask() masked that signal, to continue uninterrupted. Any use of sig- wait(2) should be such that all threads block the signals passed to sigwait(2) at all times. Only the thread that calls sigwait() will get the signals. The call to sigwait(2) takes two arguments. For this type of background dedicated signal-handling routine, a Solaris daemon thread can be used by passing the argument THR_DAEMON to thr_create(). See attributes(5) for descriptions of the following attributes: +-----------------------------+------------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+------------------------------+ |MT-Level |MT-Safe and Async-Signal-Safe | +-----------------------------+------------------------------+ sigaction(2), sigprocmask(2), sigwait(2), cond_wait(3C), pthread_cancel(3C), pthread_create(3C), pthread_join(3C), pthread_self(3C), sigaddset(3C), sigemptyset(3C), sigsetops(3C), sleep(3C), attributes(5), cancellation(5), standards(5) It is not possible to block signals that cannot be caught or ignored (see sigaction(2)). It is also not possible to block or unblock SIG- CANCEL, as SIGCANCEL is reserved for the implementation of POSIX thread cancellation (see pthread_cancel(3C) and cancellation(5)). This restriction is quietly enforced by the standard C library. Using sigwait(2) in a dedicated thread allows asynchronously generated signals to be managed synchronously; however, sigwait(2) should never be used to manage synchronously generated signals. Synchronously generated signals are exceptions that are generated by a thread and are directed at the thread causing the exception. Since sigwait() blocks waiting for signals, the blocking thread cannot receive a synchronously generated signal. Calling thesigprocmask(2) function will be the same as if thr_sigsetmask() or pthread_sigmask() has been called. POSIX leaves the seman- tics of the call to sigprocmask(2) unspecified in a multi-threaded process, so programs that care about POSIX portability should not depend on this semantic. If a signal is delivered while a thread is waiting on a condition variable, the cond_wait(3C) function will be interrupted and the handler will be executed. The state of the lock protecting the condition variable is undefined while the thread is executing the signal handler. Signals that are generated synchronously should not be masked. If such a signal is blocked and delivered, the receiving process is killed. 23 Mar 2005 thr_sigsetmask(3C)
All times are GMT -4. The time now is 11:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy