Homework 2 Solution - System Programming - Fall 2008 | CS 241, Assignments of Computer Science

Material Type: Assignment; Professor: Gupta; Class: System Programming; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/10/2009

koofers-user-8r5-1
koofers-user-8r5-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solutions. CS241 Homework 2. Fall 2008. netid: _ _ _ _ _ _ _ _
Page 1 of 2
Question 1: (Game, Set and) Match it!
Part I. [7 points] 1-b,e (any one answer is ok too). 2-b,d,e. 3-a. 4-none. 5-d,e. 6-c,d,e.
Part II. [3 points] 1-i. 2-e. 3-a. 4-g. 5-h.
Question 2: Nobel Process Hierarchy [10 points]
One possible solution is as follows.
create_binary_tree_with_depth(int N){
int i;
for (i = 1; i < N; i++)
if (fork()) /* 1st child created – child goes to next for iteration */
if (fork()) /* 2nd child created – child goes to next for iteration */
break; /* parent done and exits */
}
Question 3: Does it Sync? [10 points]
Guarantees... Case a b c d e
Mutual
Exclusion(Yes/No)
No Yes Yes No Yes
Progress (Yes/No)
No No No Yes Yes
Question 4: Ping-Pong 1 [10 points]
i. [2 points] If pongthread is the fast one, it will execute mythread_start(pingthread)
before pingthread has executed mythread_stop(). Subsequently, both threads will
deadlock.
ii. [8 points] One possible solution is as follows.
Sempahores sping=0, spong=0; /* shared by both threads */
void ping() {
while(true) {
sping.wait();
printf("ping is here\n");
spong.signal();
}
}
void pong() {
while(true) {
printf("pong is here\n");
sping.signal();
spong.wait();
}
}
pf2

Partial preview of the text

Download Homework 2 Solution - System Programming - Fall 2008 | CS 241 and more Assignments Computer Science in PDF only on Docsity!

Solutions. CS241 Homework 2. Fall 2008. netid: _ _ _ _ _ _ _ _

Page 1 of 2

Question 1: (Game, Set and) Match it! Part I. [7 points] 1-b,e (any one answer is ok too). 2-b,d,e. 3-a. 4-none. 5-d,e. 6-c,d,e.

Part II. [3 points] 1-i. 2-e. 3-a. 4-g. 5-h.

Question 2: Nobel Process Hierarchy [10 points] One possible solution is as follows.

create_binary_tree_with_depth(int N){ int i; for (i = 1; i < N; i++) if (fork()) /* 1st^ child created – child goes to next for iteration / if (fork()) / 2nd^ child created – child goes to next for iteration / break; / parent done and exits */ }

Question 3: Does it Sync? [10 points]

Guarantees... Case a b c d e Mutual Exclusion(Yes/No)

No Yes Yes No Yes

Progress (Yes/No) No No No Yes Yes

Question 4: Ping-Pong 1 [10 points] i. [2 points] If pongthread is the fast one, it will execute mythread_start(pingthread) before pingthread has executed mythread_stop(). Subsequently, both threads will deadlock.

ii. [8 points] One possible solution is as follows.

Sempahores sping=0, spong=0; /* shared by both threads */ void ping() { while(true) { sping.wait(); printf("ping is here\n"); spong.signal(); } } void pong() { while(true) { printf("pong is here\n"); sping.signal(); spong.wait(); } }

Solutions. CS241 Homework 2. Fall 2008. netid: _ _ _ _ _ _ _ _

Page 2 of 2

Question 5: Goohoo!! Inc. 2 [10 points] i. [3 points] The code has three problems: it can deadlock, it fails to restore s1, and it has unmatched downs and ups.

ii. [7 points] One possible solution is as follows.

void atomic_swap(Stack *s1, Stack *s2) { Item *item1; Item *item2; // items being transferred Stack *tmp; if(s1->id > s2->id) { // impose ordering on down operations tmp = s1; s1 = s2; s2 = tmp; } down(s1->mutex); down(s2->mutex); item1 = pop(s1); if(item1 != NULL) { item2 = pop(s2); if(item2 != NULL) { push(s2, item1); push(s1, item2); } else push (s1, item1); } up(s2->mutex); up(s1->mutex); }

Question 6: Check your Concepts [10 points]

i) F. ii) T. iii) F. iv) T. v) F. vi) F. vii) T. viii) F. ix) T. x) F.