0% found this document useful (0 votes)
18 views

WA10 Answers

Uploaded by

5qf59ptg2s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

WA10 Answers

Uploaded by

5qf59ptg2s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

15-213: Introduction to Computer Systems

Written Assignment 10
This written homework covers synchronization

Directions
Complete the question(s) on the following pages with single paragraph answers. These questions
are not meant to be particularly long! Once you are done, submit this assignment on Canvas.

Below is an example question and answer.

Q: Please describe the benefit of 2s-complement signed integers versus other approaches (such
as 1s-complement or signed-magnitude).

A: For both 1s-complement and signed-magnitude representations of signed integers, we end up


representing both -0 and +0, which gets inconvenient when the computer wants to test for a
zero result. Additionally, in both of these representations, implementing addition/subtraction is
complicated. With 2s-complement, the hardware for addition / subtraction is the same for both
signed and unsigned inputs.

Grading
Each assignment will be graded in two parts:

1. Does this work indicate any effort? (e.g. it’s not copied from a homework for another
class or from the book)
2. Three peers will provide short, constructive feedback.

Due Date
This assignment is due on Aug 9, 11:59 PM EST. Remember to convert this time to the
timezone you currently reside in.
Question 1
Compare and contrast synchronization in a multi-threaded program versus between parent and
child processes.

The purpose of synchronization is different for both these schemes and this is reflected in how
they synchronize.

Processes are often use to run different programs and so their synchronization involves
determining when programs start and stop. Because of this processes rely on signals to and
signal handlers to synchronize.

Multi-threaded programs often deal with multiple of the same program and if they need to
synchronize it’s because they’re accumulating data or reading and writing to the same piece of
memory. Because of this their synchronization relies largely on mutexes and semaphores to
lock and unlock data. In the case of accumulation multi-threading uses something like
`pthread_join` to wait on each thread.
Question 2
Here is a thread routine that uses two global variables, count1 and count2.

void* thread(void* argp) {


long niters = ((long)argp);
for (long i = 0; i < niters; i++) {
if (i % 2 == 0) count1++;
else count2++;
}
return NULL;
}

How many critical sections are in this code? Justify your answer. How would a programmer
synchronize the critical section(s)?

There are two critical sections in this code, one for each of the global variables. While writing
to the globals is in an if-else this does not mean there is only one critical section. There is no
guarantees in this code about synchronization in the for loop between threads meaning
threads can be writing to count1 or count2. Since we’re only writing to these variables and not
reading we should use a mutex as it’s less costly than a semaphore.
Question 3
Josh Z makes a hearty stew for 10 TAs over for dinner at his house. The TAs are seated around a
table, and decide that if a TA wants more stew, they state their desire and the stew pot is passed
around the table to them. Rashmi and Abi K are seated across from one another. Rashmi asks
for more stew, but while the stew pot is making its way towards her, Abi K requests more stew,
and the stew pot begins to make its way back towards Abi K. Rashmi once again requests more
stew, followed by Abi K, and the stew pot is passed back and forth, never reaching Abi K or
Rashmi. Is this an instance of deadlock, livelock, or starvation (as it relates to concurrency)?

Obviously this is starvation because Abi is starving! In starvation processes keep asking for
resources but whatever schedules the shared resource gives the process what it needs (it’s
starving). In this specific cause the starvation is caused by livelock. Both Abi and Rashmi are
not blocked they’re still running but by both being greedy no one gets stew.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy