Processes and Threads. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. Processes The Process Model. Multiprogramming of four programs Conceptual model of 4 independent, sequential processes
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
Processes and Threads 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Chapter 2
ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant
Process Creation Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process • Initiation of a batch job
Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)
Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of process hierarchy • all processes are created equal
Process States (1) • Possible process states • running • blocked • ready • Transitions between states shown
Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes
Scheduler Activations • Goal – mimic functionality of kernel threads • gain performance of user space threads • Avoids unnecessary user/kernel transitions • Kernel assigns virtual processors to each process • lets runtime system allocate threads to processors • Problem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)
Pop-Up Threads • Creation of a new thread when message arrives (a) before message arrives (b) after message arrives
Critical Regions (1) Four conditions to provide mutual exclusion • No two processes simultaneously in critical region • No assumptions made about speeds or numbers of CPUs • No process running outside its critical region may block another process • No process must wait forever to enter its critical region
Algorithm 1 • Shared variables: • int turn;initially turn = 0 • turn - i Pi can enter its critical section • Process Pi do { while (turn != i) /*infinite loop */; critical section turn = j; reminder section } while (1);
Synchronization Hardware • Test and modify the content of a word atomically. boolean TestAndSet(boolean &target) { boolean rv = target; tqrget = true; return rv; }
Mutual Exclusion with Test-and-Set • Shared data: boolean lock = false; • Process Pi do { while (TestAndSet(lock)) ; critical section lock = false; remainder section }
Synchronization Hardware • Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }
Mutual Exclusion with Swap • Shared data (initialized to false): boolean lock; • Process Pi do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section }
Semaphores • Synchronization tool that does not require busy waiting. • Semaphore S – integer variable • can only be accessed via two indivisible (atomic) operations wait/down (S): while S 0 do no-op;S--; signal/up (S): S++;
Critical Section of n Processes • Shared data: semaphore mutex; //initially mutex = 1 • Process Pi: do { wait(mutex);critical section signal(mutex); remainder section} while (1);
Semaphore Implementation • Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; • Assume two simple operations: • block suspends the process that invokes it. • wakeup(P) resumes the execution of a blocked process P.
Implementation • Semaphore operations now defined as wait/down(S): S.value--; if (S.value < 0) { add this process to S.L; block; } signal/up(S): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); }
Mutexes Implementation of mutex_lock and mutex_unlock
Semaphore as a General Synchronization Tool • Execute B in Pj only after A executed in Pi • Use semaphore flag initialized to 0 • Code: Pi Pj Await/down(flag) signal/up(flag) B
Deadlock and Starvation • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. • Let S and Q be two semaphores initialized to 1 P0P1 wait(S); wait(Q); wait(Q); wait(S); signal(S); signal(Q); signal(Q) signal(S); • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.
Two Types of Semaphores • Counting semaphore – integer value can range over an unrestricted domain. • Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. • Can implement a counting semaphore S as a binary semaphore.
Implementing S as a Binary Semaphore • Data structures: binary-semaphore S1, S2; int C: • Initialization: S1 = 1 S2 = 0 C = initial value of semaphore S
Implementing S • wait operation wait(S1); C--; if (C < 0) { signal(S1); wait(S2); } signal(S1); • signal operation wait(S1); C ++; if (C <= 0) signal(S2); else signal(S1);
Sleep and Wakeup Producer-consumer problem with fatal race condition
Semaphores The producer-consumer problem using semaphores
Semaphores The producer-consumer problem using semaphores