Home > Articles, Operating Systems > Protection of critical regions of a process in GNU/Linux.

Protection of critical regions of a process in GNU/Linux.

Below, we quote a function for managing a transaction in a hypothetical transaction management system. It is very likely multiple instances of the function to be executed in parallel on a system of symmetric multiprocessing (SMP), on multi-processor platforms or even a multithreaded single processor systems.

To avoid possible race conditions and deadlocks, we should protect the critical region of the function (code that acts on shared resources). If this protection is not done, it is very likely that the behavior of the transaction management system will be unstable and the transaction insecure.

Below, we use the function pthread_setcancelstate() which protects the critical region of the function so as to prevent both its interruption (atomic execution) and the parallel execution of multiple instances (within the critical region).

The secure function of the hypothetical Transaction Management System follows:

#include <pthread.h>

int
process_transaction (int from_account, int to_account, float euro) {
  int old_cancel_state;

  ... // init inline stuff.

  /* begin critical section. */
  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancel_state);

  ... // handle the resources.

  /* end critical section. */
  pthread_setcancelstate (old_cancel_state, NULL);

  ... // return something.
}
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: