Академический Документы
Профессиональный Документы
Культура Документы
Topics
Processes and Threads
Posix threads (Pthreads) interface
Shared variables
The need for synchronization
Synchronizing with semaphores
Synchronizing with mutex and condition
variables
Thread safety and reentrancy
Traditional view of a UNIX process
shared libraries
stack
SP brk
run-time heap
Thread 1 (main thread) Shared code and data Thread 2 (peer thread)
shared libraries
stack 1 stack 2
run-time heap
T2 P0
T4
T1
P1
shared code, data
and kernel context
sh sh sh
T5 T3
foo
bar
The idle thread is created at compile time for the first CPU;
Kernel threads are created using kernel_thread(). Kernel
threads usually have no user address space, i.e. p->mm =
NULL
Kernel threads can always access kernel address space
directly. They are allocated PID numbers in the low range.
They cannot be pre-empted by the scheduler.
User processes are created by means of clone(2) or fork(2)
system calls, both of which internally invoke
kernel/fork.c:do_fork().
Betriebssysteme SoSe 2002 7 Harald Kosch
Concurrent thread execution
Two threads run concurrently (are concurrent) if their
logical flows overlap in time.
Otherwise, they are sequential.
Time
main thread
call Pthread_create()
Pthread_create() returns peer thread
call Pthread_join()
printf()
main thread waits for return NULL;
peer thread to terminate (peer thread
terminates)
Pthread_join() returns
exit()
terminates
main thread and
any peer threads
sem_init(&sem, 0, 1);
exit(0);
}
Unlocks mutex.
like V(mutex)
ssize_t
Fix: Rewrite function so that my_read_r(Rline *rptr, char *ptr)
caller passes in all necessary {
state. ...
}
All functions
Thread-safe
functions
Thread-unsafe
functions
Reentrant
functions
Since linux libc version 6.0 all C Libraries are thread safe