Kernel seminar – part 4

Synchronization

Simultaneous device access

Several user space programs access the same device, data or hardware
Several kernel processes executing the same code on behalf of parallel user processes
The code can be running on another processor
Kernel code can be interrupted at any time
Kernel may be preempted

Linux Locking primitives

Semaphores
Mutexes
Spinlocks
Atomic variable are not locks, but help avoid synchronization issues

Semaphores

Semaphore can go sleep. Meaning cannot be used in tasklets, timers interrupts
It safe to block while holding a semaphore
Semaphores manipulated in two ways:
down – blocking operation
up – release operation

Initialize semaphores

Attempt to acquire a semaphore:
int down_interruptible (stuct semaphore*);
returns 0 on success
only use if event 100% will happen
interrupt can cancel the semphore

int down(struct semaphore*)
same as above, without the interrupt thing

int down_trylock (struct semaphore *sem)
return 0 if success or 1 if semaphore cannot be acquired

release semaphore:
void up(struct semaphore*)

Mutex

Same as semaphore, but more advanced.
Requier #include <linux/mutex.h>
init statically:
DEFINE_MUTEX(name)
init dynamically:
void mutex_init(struct mutex *lock)

Spinlocks

A section a code that executed in a loop, while checking if a lock is free or not. The spinlock occupy the CPU
requires #include <linux/spinlock.h>
static init:
DEFINE_SPINLOCK(my_spinlock)
Dynamic init:
void spin_lock_init(spinlock_t *lock)

API:

Atomic variables
Useful when the shared resource is an integer value
requires <asm/atomic.h>
Set/Read:
atomic_set(atomic_t * int)
int atomic_read(atomic_t *)
Operations without return values:
void atomic_int (atomic_t *)
void atomic_dec (atomic_t *)
void atomic_add (int, atomic_t *)
void atomic_sub (int, atomic_t *)

Later subjects are mostly concern heavy hands on, therefore I will not note them

Leave a Reply

Your email address will not be published. Required fields are marked *