Thursday, July 29, 2010

Task synchronization in Linux

[1] http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:programming
[2] Understanding the Linux Kernel, 3rd Edition By Daniel P. Bovet, Marco Cesati Publisher: O'Reilly Pub Date: November 2005 ISBN: 0-596-00565-2 Pages: 942
[3] http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/apis/apiexusmem.htm


KERNEL THREAD
--------------------
The only difference between a kernel thread and a user thread is the fact that the kernel thread has no user task memory mapping. Like other user processes it shares the kernel address space but the thread operates only in kernel space with kernel privilages.

A thread is normally designed as a continous loop that calls some wait or scheduling function to stop it consuming 100% of the CPU.

Process creat thread


SPIN LOCK
----------------------------
As a general rule, kernel preemption is disabled in every critical region protected by spin locks. In the case of a uniprocessor system, the locks themselves are useless, and the spin lock primitives just disable or enable the kernel preemption. Please notice that kernel preemption is still enabled during the busy wait phase, thus a process waiting for a spin lock to be released could be replaced by a higher priority process. [2] 5.2.4


SEMAPHORES
-------------------------
Why need semaphore?

In general, safe access to a global variable is ensured by using atomic operations . In the previous example, data corruption is not possible if the two control paths read and decrease V with a single, noninterruptible operation. However, kernels contain many data structures that cannot be accessed with a single operation.

The down( ) method decreases the value of the semaphore. If the new value is less than 0, the method adds the running process to the semaphore list and then blocks (i.e., invokes the scheduler). The up( ) method increases the value of the semaphore and, if its new value is greater than or equal to 0, reactivates one or more processes in the semaphore list. [2] 1.6.5

Each data structure to be protected has its own semaphore, which is initialized to 1. When a kernel control path wishes to access the data structure, it executes the down( ) method on the proper semaphore. If the value of the new semaphore isn't negative, access to the data structure is granted. Otherwise, the process that is executing the kernel control path is added to the semaphore list and blocked.

[2] 1.6.5.3

Linux offers two kinds of semaphores:
Kernel semaphores, which are used by kernel control paths
System V IPC semaphores, which are used by User Mode processes [2] 5.2.8

The semaphore value is positive if the protected resource is available, and 0 if the protected resource is currently not available. A process that wants to access the resource tries to decrease the semaphore value; the kernel, however, blocks the process until the operation on the semaphore yields a positive value. When a process relinquishes a protected resource, it increases its semaphore value; in doing so, any other process waiting for the semaphore is woken up. [2] 19.3.3

int semget(key_t key, int nsems, int semflg);
int semop(int semid, struct sembuf *sops, unsigned nsops);
int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
struct timespec *timeout);


type struct sembuf containing the following members:
unsigned short sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */

sem_flg are IPC_NOWAIT and SEM_UNDO

SAMPLES [3]

/* Oprations on semaphore. */

union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO
(Linux-specific) */
};

No comments: