Hi!
- What to do if multiple processes want to read from the same disk at once?
Have a queue of sequential disk accesses. Each process spins until it's request is satisfied (out of cache or from the disk).
- What are spinlocks, when do I use them, and how?
Spinlocks are an implementation of mutual exclusion. I can't write an x86 one off the top of my head right now but they usually use the 'compare and exchange' instruction (in SMP, with the LOCK prefix)
It usually goes something like:
Code: Select all
a: is_the_lock_free()?
if yes:
return;
else goto a;
is_the_lock_free() would be a function (all of this is usually in ASM) or instruction that can tell if the lock is free
and then lock it itself for our use all before any other process can lock it. This requirement is called 'atomicity', and usually means that the function must comprise of exactly one CPU instruction.
Example, if the function is not atomic (note this is in C, you'd have to translate it to asm):
Code: Select all
bool is_lock_free()
{
if (lock_is_free)
get_lock(); return true;
else
return false;
}
Now, imagine that the lock is free. We do the 'if' test and come back true. Then we get preempted and another process gets the lock. We get executed again and execute the get_lock() function. This would overwrite whatever is in the lock already (can sometimes be more than just a boolean value) and we would be under the assumption that we have the lock, where we don't.
For that reason the 'if' and 'get' statements must be done together as an atomic set of operations. The x86 provides a cmpxchng instruction for this.
Spinlocks are used whenever mutual exclusion is required.
- Same with deadlocks?
Deadlock is different. Let's take an example.
Process P has resource A. Process Q has resource B.
Process P requires resource B to complete execution, however process Q requires resource A to complete execution (they both require both resources but have locked one each).
What we have there is called 'deadlock'. Neither process can progress. There are four things needed to avoid deadlock, and for the life of me I can't remember what they are. I know one is to avoid starvation, can't remember the rest. Combuster is an academic type, maybe he'll know
(at work now so haven't got time to grep the internet).
- What other steps do I need look into to thread-safe my kernel?
Are you using multiple cores/processors or not?