Main Content

CERT C: Rule POS44-C

Do not use signals to terminate threads

Description

Rule Definition

Do not use signals to terminate threads.1

Polyspace Implementation

The rule checker checks for Use of signal to kill thread.

Examples

expand all

Issue

Use of signal to kill thread occurs when you use an uncaught signal to kill a thread. For instance, you use the POSIX® function pthread_kill and send the signal SIGTERM to kill a thread.

Risk

Sending a signal kills the entire process instead of just the thread that you intend to kill.

For instance, the pthread_kill specifications state that if the disposition of a signal is to terminate, this action affects the entire process.

Fix

Use other mechanisms that are intended to kill specific threads.

For instance, use the POSIX function pthread_cancel to terminate a specific thread.

Example - Use of pthread_kill to Terminate Threads
#include <signal.h>
#include <pthread.h>

void* func(void *foo) {
  /* Execution of thread */
}
 
int main(void) {
  int result;
  pthread_t thread;
 
  if ((result = pthread_create(&thread, NULL, func, 0)) != 0) {
  }
  if ((result = pthread_kill(thread, SIGTERM)) != 0) { //Noncompliant
  }
 
  /* This point is not reached because the process terminates in pthread_kill() */
 
  return 0;
}

In this example, the pthread_kill function sends the signal SIGTERM to kill a thread. The signal kills the entire process instead of the thread previously created with pthread_create.

Correction — Use pthread_cancel to Terminate Threads

One possible correction is to use the pthread_cancel function. The pthread_cancel terminates a thread specified by its first argument at a specific cancellation point or immediately, depending on the thread's cancellation type.

#include <signal.h>
#include <pthread.h>

void* func(void *foo) {
  /* Execution of thread */
}
 
int main(void) {
  int result;
  pthread_t thread;
 
  if ((result = pthread_create(&thread, NULL, func, 0)) != 0) {
    /* Handle Error */
  }
  if ((result = pthread_cancel(thread)) != 0) {
    /* Handle Error */
  }
 
  /* Continue executing */
 
  return 0;
}

See also:

  • pthread_cancel for more information on cancellation types.

  • Pthreads for functions that are allowed to be cancellation points.

Check Information

Group: Rule 50. POSIX (POS)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.