How does the Linux kernel realize reentrancy? -


all unix kernels reentrant: several processes may executing in kernel mode @ same time. how can realize effect in code? how should handle situation whereby many processes invoke system calls, pending in kernel mode?

[edit - term "reentrant" gets used in couple of different senses. answer uses basic "multiple contexts can executing same code @ same time." applies single routine, can extended apply set of cooperating routines, routines share data. extreme case of when applied complete program - web server, or operating system. web-server might considered non-reentrant if deal 1 client @ time. (ugh!) operating system kernel might called non-reentrant if 1 process/thread/processor executing kernel code @ time.

operating systems occurred during transition multi-processor systems. many went through slow transition written-for-uniprocessors one-single-lock-protects-everything (i.e. non-reentrant) through various stages of finer , finer grained locking. iirc, linux got rid of "big kernel lock" @ approx. version 2.6.37 - gone long before that, protecting remnants not yet converted multiprocessing implementation.

the rest of answer written in terms of individual routines, rather complete programs.]

if in user space, don't need anything. call whatever system calls want, , right thing happens.

so i'm going presume asking code in kernel.

conceptually, it's simple. it's pretty identical happens in multi-threaded program in user space, when multiple threads call same subroutine. (let's assume it's c program - other languages may have differently named mechanisms.)

when system call implementation using automatic (stack) variables, has own copy - no problem re-entrancy. when needs use global data, needs use kind of locking - specific locking required depends on specific data it's using, , it's doing data.

this pretty generic, perhaps example might help.

let's system call want modify attribute of process. process represented struct task_struct member of various linked lists. linked lists protected tasklist_lock. system call gets tasklist_lock, finds right process, possibly gets per-process lock controlling field cares about, modifies field, , drops both locks.

one more detail, case of processes executing different system calls, don't share data each other. reasonable implementation, there no conflicts @ all. 1 process can kernel handle system call without affecting other processes. don't remember looking @ linux implementation, imagine it's "reasonable". trap exception handler, looks in table find subroutine handle specific system call requested. table const, no locks required.