170 likes | 327 Views
Lecture 12. Processes and Processes Synchronization . Process Time-sharing system Critical section problem Semaphore Process communication . Process . A process is a program in execution. A computer can run several programs simultaneously.
E N D
Lecture 12. Processes and Processes Synchronization • Process • Time-sharing system • Critical section problem • Semaphore • Process communication CS3369 Realtime Control Computer Software/WANG Lusheng
Process • A process is a program in execution. • A computer can run several programs simultaneously. • A program can be executed several times by the same computer simultaneously. CS3369 Realtime Control Computer Software/WANG Lusheng
Time-sharing System • Multiple jobs (processes) are executed by the CPU switching between them, but the switches occur so frequently that the users may interact with each program while it is running. • The switch among processes is done by OS. CS3369 Realtime Control Computer Software/WANG Lusheng
Data communication in embedded systems. • Channels: directed transfer of information. • Pools: shared information. • Process Symbol: input output processing CS3369 Realtime Control Computer Software/WANG Lusheng
Channels • A channel provides the medium for items of information to be passed between one process and another. • More than one item of information can pass through a channel at any one time and they are usually ordered according to the rule of FIFO (first-in-first-out). • Interrupt #0x09 stores keys typed by users in a buffer which is accessible by other interrupts. put ch._1 get g p CS3369 Realtime Control Computer Software/WANG Lusheng
Pools • Items of information in a pool are available for reading and/or writing by a number of processes in the system. • Information does not flow within a pool. A pool acts as a repository of information. • Any item in a pool will be available to processes using the pool. CS3369 Realtime Control Computer Software/WANG Lusheng
Pools are as important as processes: • processes are software models of the activities that an embedded system must perform; • pools form the software models of items in the system. These includes physical devices and mechanisms in the control system, and conceptual items such as logical files. • In many applications, the pool has information written into it endlessly. CS3369 Realtime Control Computer Software/WANG Lusheng
Process Synchronization • Producer-consumer Problem • Producer keeps producing items and putting them in the buffer • Consumer keeps taking items away from the buffer. CS3369 Realtime Control Computer Software/WANG Lusheng
Process Synchronization Producer’s code Repeat … produce an item in nextp … while counter = n do no-op; buffer[in]:=nextp; in:= in + 1 mod n; counter:= counter + 1; Until false; CS3369 Realtime Control Computer Software/WANG Lusheng
Process Synchronization Consumer’s codes Repeat while counter =0 do no-op; nextc:= buffer[out]; out:=out + 1 mod n; counter:=counter –1; … consume the item in nextc … Untile false; CS3369 Realtime Control Computer Software/WANG Lusheng
counter := counter +1 and counter: =counter -1 Each high level language statement corresponds to several lower level language statements. register1:=counter; register2:=counter; register1:=register1+1; register2=register2-1; counter:=register1; counter=register2 counter=counter+1; counter=counter-1; CS3369 Realtime Control Computer Software/WANG Lusheng
The results could be wrong If counter =5 then after counter=counter+1; counter=counter-1; The result should be counter =5. However, it could be wrong: T0: producer register1:=counter; {register1=5} T1:producer register1:=register1+1; {register1=6} T2:consumer register2:=counter; {register2=5} T3:consumer register2=register2-1; {register2=4} T4:producer counter:=register1; {counter =6} T5:consumer counter=register2 {counter=4} CS3369 Realtime Control Computer Software/WANG Lusheng
Cirtical section Why the result is wrong? Two processes access the same variable counter at about the same time. A critical section is a segment of codes that the process may be changing common resources, e.g., variables. CS3369 Realtime Control Computer Software/WANG Lusheng
Semaphore A synchronization tool that can be used to solve some synchronization problem, e.g., the critical section problem. CS3369 Realtime Control Computer Software/WANG Lusheng
Semaphore A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait and signal. Wait(s): while S<=0 do no-op; s=s-1; Signal(S): s=s+1; CS3369 Realtime Control Computer Software/WANG Lusheng
Solve the Critical Section Problem Producer’s code Repeat … produce an item in nextp … while counter = n do no-op; wait(S); buffer[in]:=nextp; in:= in + 1 mod n; counter:= counter + 1; signal(S): Until false; CS3369 Realtime Control Computer Software/WANG Lusheng
Solve the Critical Section Problem Consumer’s codes Repeat wait(S); while counter =0 do no-op; nextc:= buffer[out]; out:=out + 1 mod n; counter:=counter –1; signal(S); … consume the item in nextc … Untile false; CS3369 Realtime Control Computer Software/WANG Lusheng