University of Massachusetts Amherst 2
Homework2
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35
P1 R R R R R R R w w w r r R R R
P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35
P1 r r r r r R R R R R R R w w w r r r r r R R R
P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35
P1 r r r r r R R R R R R R w w w R R R
P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35
P1 R R r r R R r r R R r R w w w R R R
P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm
P1 WT
P2 WT
Avg WT
P1 FT
P1 FT
Schd Len
CPU Ut
FCFS
2
7
4.5
15
27
27
81.48%
SJF
10
0
5
23
20
23
95.65%
PSJF
5
3
4
18
23
23
95.65%
RR 2
5
6
5.5
18
26
26
84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst
will make RR behave like FCFS.
P1: CPU-7, IO-3, CPU-3
P2: CPU-5, IO-8, CPU-7
Kevin Zheng Tongping Liu
ECE 570: System Software Design 2 March 2021
Homework 2 1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3 P2: CPU 5, I/O 8, CPU 7 1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut FCFS 2 7 4.5 15 27 27 81.48% SJF 10 0 5 23 20 23 95.65% PSJF 5 3 4 18 23 23 95.65% RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst will make RR behave like FCFS.
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut
FCFS 2 7 4.5 15 27 27 81.48%
SJF 10 0 5 23 20 23 95.65%
PSJF 5 3 4 18 23 23 95.65%
RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst
will make RR behave like FCFS.
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut
FCFS 2 7 4.5 15 27 27 81.48%
SJF 10 0 5 23 20 23 95.65%
PSJF 5 3 4 18 23 23 95.65%
RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
University of Massachusetts Amherst 3
Homework2
P1: CPU-7, IO-3, CPU-3
P2: CPU-5, IO-8, CPU-7
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35
P1 R R R R R R R w w w r r R R R
P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35
P1 r r r r r R R R R R R R w w w r r r r r R R R
P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35
P1 r r r r r R R R R R R R w w w R R R
P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35
P1 R R r r R R r r R R r R w w w R R R
P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm
P1 WT
P2 WT
Avg WT
P1 FT
P1 FT
Schd Len
CPU Ut
FCFS
2
7
4.5
15
27
27
81.48%
SJF
10
0
5
23
20
23
95.65%
PSJF
5
3
4
18
23
23
95.65%
RR 2
5
6
5.5
18
26
26
84.62%
2. agree/disagree
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut
FCFS 2 7 4.5 15 27 27 81.48%
SJF 10 0 5 23 20 23 95.65%
PSJF 5 3 4 18 23 23 95.65%
RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst
will make RR behave like FCFS.
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R
Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut
FCFS 2 7 4.5 15 27 27 81.48%
SJF 10 0 5 23 20 23 95.65%
PSJF 5 3 4 18 23 23 95.65%
RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst
will make RR behave like FCFS.
Kevin Zheng
Tongping Liu
ECE 570: System Software Design
2 March 2021
Homework 2
1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3
P2: CPU 5, I/O 8, CPU 7
1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut FCFS 2 7 4.5 15 27 27 81.48% SJF 10 0 5 23 20 23 95.65% PSJF 5 3 4 18 23 23 95.65% RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU
burst first, not the shortest total execution time
b. agree – a quantum that is greater than or equal to the longest CPU burst
will make RR behave like FCFS.
Kevin Zheng Tongping Liu
ECE 570: System Software Design 2 March 2021
Homework 2 1. CPU scheduling
P1: CPU 7, I/O 3, CPU 3 P2: CPU 5, I/O 8, CPU 7 1 just before 2
a. First-come/first-served
0 5 10 15 20 25 30 35 P1 R R R R R R R w w w r r R R R P2 r r r r r r r R R R R R w w w w w w w w R R R R R R R
b. Shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w r r r r r R R R P2 R R R R R w w w w w w w w R R R R R R R
c. Preemptive shortest job first
5 10 15 20 25 30 35 P1 r r r r r R R R R R R R w w w R R R P2 R R R R R w w w w w w w w R R r r r R R R R R
d. Round robin with a quantum of 2
5 10 15 20 25 30 35 P1 R R r r R R r r R R r R w w w R R R P2 r r R R r r R R r r R w w w w w w w w R R R R R R R Algorithm P1 WT P2 WT Avg WT P1 FT P1 FT Schd Len CPU Ut FCFS 2 7 4.5 15 27 27 81.48% SJF 10 0 5 23 20 23 95.65% PSJF 5 3 4 18 23 23 95.65% RR 2 5 6 5.5 18 26 26 84.62%
2. agree/disagree
a. disagree – shortest job first scheduling runs the job with the shortest CPU burst first, not the shortest total execution time
University of Massachusetts Amherst 4
Homework2
• Q2:
– Disagree. In SJF scheduling, the task
with the next
shortest cpu burst
,
not total execution time
, will be
scheduled first.
– Agree. There is a quantum value in RR to make it
Homework2
void initLock(bool * lock) {
*lock = false;
}
void lock(bool * lock) {
bool key = true;
while (key == true) {
swap(&key, lock);
}
void unlock(bool * lock) {
*lock = false;
University of Massachusetts Amherst 6
Homework2
• Pros:
– Easy to implement
– No priority inversion.
– Deletion safety
• Cons:
Midterm
• Time:
– March 17: 8pm~10PM (EST)
• Zoom Link (lectures link):
University of Massachusetts Amherst 8
Midterm Exam Format
• 2-hour exam over the zoom (open the video)
• Ask questions in a separate room
• Close notes but with one cheat sheet
(back-and-forth)
Lecture-1: Overview
• Evolution of Computer Systems and OS
Concepts
• Different Types of OS
– Parallel/Distributed/Real-time/Embedded OS
• OS Services
• OS Structures and Basic Components
University of Massachusetts Amherst 10
Important Concepts
Lecture-2: Process
• Programs and Processes
• Process (program image) in memory
• PCB: Process Control Block
University of Massachusetts Amherst 12
Important Concepts
• Difference between a program with a process
• Program states and state transition
Lecture-3: Threads
• Motivation and thread basics
– Resource requirements: thread vs. process
• Thread implementations
– User threads: e.g., Pthreads and Java threads
– Kernel threads: e.g., Linux tasks
– Map user- and kernel-level threads
– Lightweight process and scheduler activation
• Other issues with threads:
– process creation and signals etc.
– Memory sharing model
University of Massachusetts Amherst 14
Important Concepts
• Difference and similarities between threads and
processes
• Pros and cons of thread models: 1-to-1, n-to-1, and
m-to-n
Lecture-5
• Shared data access
– Race condition and critical section
– General structure for enforce critical section
• Synchronization mechanism
– Hardware supported instructions
– Software solution
• Classical Synchronization Problems
University of Massachusetts Amherst 16
Important Concepts
• Critical section
• General structure for ensuring safety of critical
section
• swap() and testAndset()
• Semaphore vs. Mutex
• Monitor
Lecture-6
• Implementing Multithreaded Programs
– Synchronization Errors:
• Deadlock, race condition, order violation, atomicity violation
– Performance Issues:
• Hardware related: false/true sharing
• Software related: lock issues
University of Massachusetts Amherst 18
Important Concepts
• Four common synchronization errors
• False/true sharing performance issues
• Lock performance issues
Question #1
•
What is the difference between a process and a
thread?
University of Massachusetts Amherst 20
20
Question #2
•
What process states for a running process is not
stored in the address space?
Question #3
•
What is the problem with defining large critical
sections in multi-threaded code?
•
Results in a lot of waiting by the threads. You
want to release the lock as much as possible.
University of Massachusetts Amherst 22
22
Question #4
• When might you use signal() instead of
broadcast() with condition variables?
Question #5
• Does the order of the signal() calls matter with
semaphores in terms of causing/preventing
deadlock? Does the order of the wait() calls
matter?
University of Massachusetts Amherst 24
24
Question #6
•
Name three internal events a thread may
execute to return control back to the OS?
Question #7
•
Name the most common external event that
transfers control back to the OS from a
running thread?
University of Massachusetts Amherst 26
26
Question #8
•
What are the basic steps involved with creating
a new thread?
•
Allocate and initialize a new TCB, allocate a new
Question #9
•
Why is it ok for the OS to disable interrupts,
but not application programs?
University of Massachusetts Amherst 28