Skip to content

Semaphores

May 31, 2009

Whilst modifying the memory allocation routines to include the Pid of the task that owns the memory it occurred to me that I had made the most elementary oversight by not accounting for the possibility of multiple tasks trying to allocate memory at the same time. In the wrong circumstances this could cause havoc by completely wrecking the linked lists of memory allocation. The answer is trivial – I have now implemented a system of semaphors so that only one task can access the memory allocation/deallocation routines at a time.

This is almost trivial on the x86_64 processors by using the CMPXCHG instruction. This tests and, if required, sets a memory location as an atomic instruction; in other words, it is guaranteed that the instruction will complete without being interrupted. If the semaphore can’t be set (i.e. if another task has already set it) a task switch is done and the operation will be retried next time the task runs.

At the start of the memory allocation/deallocation routines a semaphore is now set, and it is cleared at the end of these routines. This should guard against the possibility of some particulary obscure bugs at a future time.

From → IanOS

One Comment
  1. I was just looking for this info for a while. After 6 hours of continuous Googleing, finally I got it in your site. I wonder what’s the lack of Google strategy that do not rank this type of informative web sites in top of the list. Normally the top sites are full of garbage.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: