title: Linux: Can a signal handler excution be preempted?
body: <p>I came across the following signal handler code that stores the errno variable so that it wont affect main thread's errno handling.</p>
  
  <pre><code>void myhandler(int signo)
  {
      int esaved;
      esaved = errno;
      write(STDOUT_FILENO, "Got a signal\n", 13);
      errno = esaved;
  }
  </code></pre>
  
  <p>But this really serves the purpose ? what happens if another thread check for the shared errno varible just after write() and before restoring errno ? Will that thread get wrong errno value due to race condition?</p>
  
  <p>Or a signal handler executes <em>atomically</em> with respect to a thread/process, so that once the signal handler executes , kernel wont schedule the thread back until the signal handler finishes?</p>
  
  <p>Putting in other words -Once started, do a signal handler executes without being interrupted by:</p>
  
  <pre><code> - 1) Scheduler (process/threads),  or  
   - 2) Other signals,  or
   - 3) Hardware interrupt handlers  ?
  </code></pre>