Tip 14 Calculate Elapsed Time 1 n To















- Slides: 15
Tip #14: Calculate Elapsed Time 1 n To calculate the time passed between two events without taking extra execution time than expected: #include <time. h> #include <stdlib. h> clock_t startm, stopm; #define BEGIN if ((startm = clock()) == -1) { exit(1); } #define CLOSE if ((stopm = clock()) == -1) { exit(2); } #define SHOWTIME printf("%6. 3 f seconds elapsed. ", ((double)stopm-startm)/CLOCKS_PER_SEC); int main() { BEGIN; // Instructions to time. Sleep(10); CLOSE; SHOWTIME; }
Delta Clock 3 n n Problem: How to efficiently monitor multiple timed events? Examples of timed events: n n n scheduling real-time sequencing timers timeouts Lists require each event timer to be decremented (O(n)) to determine if time has expired. Using a Delta Clock only requires decrementing the top entry (O(1)).
4 DC Implementation Suppose: 20 Event 1 occurs in 20 tics Event 2 occurs in 5 tics Event 3 occurs in 35 tics Event 4 occurs in 27 tics Event 5 occurs in 27 tics Event 6 occurs in 22 tics 5 Event 2 5 35 Event 3 27 Event 4 27 Event 5 And that Event 6 occurs 2 tics after Event 1 What if Event 7 occurs in 17 tics? Event 8 in 31 tics? Notice that Event 1 occurs 15 tics after Event 2 22 Event 6 Linked List Event 2 12 Event 5 7 2 3 12 5 Event 1 7 2 2 3 15 Event 6 1 5 2 Event 4 6 0 Event 5 5 Event 4 4 Event 8 0 Event 5 4 8 Event 3 Delta Clock
Project 3 Assignment Step 1: Delta Clock 5 n Implement delta clock. n n Design data structure to hold delta times/events. dc[4] 10 / sem 1 Program an insert delta clock function dc[3] 5 / sem 2 n n n insert. Delta. Clock(int time, Semaphore* sem); High priority, mutex protected Add 1/10 second function to decrement top event and sem. Signal semaphore when 0 n n n dc[5] dc[2] 0 / sem 3 dc[1] 2 / sem 4 dc[0] 4 pollinterrupts or High priority, mutex protected. Thoroughly test the operation of your delta clock before proceeding. n n n os 345 p 3. c Print Delta Clock (dc): int P 3_dc(int argc, char* argv[]); Test Delta Clock (tdc): int P 3_tdc(int argc, char* argv[]); n n int dc. Monitor. Task(int argc, char* argv[]); int time. Task(int argc, char* argv[]);
Chapter 6 Concurrency: Deadlock and Starvation
7 CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview 4 P 1: Shell 3: Process Description and Control 4: Threads 4 P 2: Tasking 5: Concurrency: ME and Synchronization 6: Concurrency: Deadlock and Starvation 6 P 3: Jurassic Park 7: Memory Management 8: Virtual memory 6 P 4: Virtual Memory 9: Uniprocessor Scheduling 10: Multiprocessor and Real-Time Scheduling 6 P 5: Scheduling 11: I/O Management and Disk Scheduling 12: File Management 8 P 6: FAT Student Presentations 6
8 Learning Objectives… Learning Outcomes After completing this section, you should be able to n n n Topics n n List and explain the conditions of deadlock. n Define deadlock prevention and describe deadlock prevention strategies related to each of the conditions for deadlock. n n n Explain the difference between deadlock prevention and deadlock avoidance. n Understand how an integrated deadlock strategy can be designed. n Analyze the dining philosophers problem. Explain the concurrency and synchronization methods used in UNIX, Linux, Solaris, and Windows 7. n n Resources Deadlock Joint Process Diagrams Deadlock Conditions Circular Wait Resource Allocation Graph Handling Deadlock Avoidance Detection Recovery
Deadlock Quiz 6. 1 9 How could deadlock occur when n 200 K bytes of memory is available for allocation by the system Process 1 needs 140 K in 80 K, 60 K blocks Process 2 needs 150 k in 70 K, 80 K blocks Process 1 Process 2 Request 80 K bytes … Request 60 K bytes … … Request 70 K bytes … Request 80 K bytes Process 1 Process 2 Receive(P 2) … Send(P 2) … … Receive(P 1) … Send(P 1) How could deadlock occur when n Two processes need to communicate via send/receive messages Process 1 waits to hear from process 2 before sending data Process 2 proceeds after hearing from process 1
Resources Types of Resources 10 n Reusable Resources n n n Used by one process at a time and not depleted by that use Processes obtain resources that they later release for reuse by other processes Processor time, I/O channels, main and secondary memory, files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other Consumable Resources n n Created (produced) and destroyed (consumed) by a process Interrupts, signals, messages, and information in I/O buffers Deadlock may occur if a Receive message is blocking May take a rare combination of events to cause deadlock
Deadlock Follow the Rules… 11 n System Model (Rules) n n Process must request (and be granted) a resource before using it. Process must release the resource when done. Why? ? Deadlock n A set of processes is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set. P 1: holds R 1, needs R 2 P 2: holds R 2, needs R 3 P 3: holds R 3, needs R 1 P 4: needs R 1
Deadlock 12 Quiz 6. 2 1. What are the resources? 2. Where is mutual exclusion needed? 3. What is required for deadlock to occur?
Deadlock 13 Quiz 6. 2 (solution) 1. What are the resources? 2. Where is mutual exclusion needed? 3. What is required for deadlock to occur? ME
Diagrams 14 Joint Process Diagram Progress of Q Impossible joint conditions are grayed out. 2 1 Release A A Required Both P and Q have A Release B Get A B Required 5 ? P has B Both P and Q Q has A have B Get B Deadlock is only inevitable if the joint progress of the two processes creates a path that enters the fatal region. 6 Get A Get B 4 3 Release A Release B A Required B Required Progress of P
Diagrams 15 Joint Process Diagram Progress of Q 1 2 Release A A Required Release B 3 6 Both P and Q have A Get A Both P and Q have B B Required 5 Get B 4 No fatal region, as there are “exit” paths available. Get A Release A Get B A Required Release B B Required Progress of P
16