Download - Tutorial03 Solution
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 1/21
Technische Universität München
Chip Multicore ProcessorsTutorial 3
Institute for Integrated Systems
Theresienstr. 90
Building N1www.lis.ei.tum.de
S. Wallentowitz
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 2/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 2
S. Wallentowitz
Task 3.1: 3-Thread Lock
You have a processor at hand that does not contain any
hardware support for locking or atomar operations.Extend Peterson's Lock to support three threads.
How can this be generalized?
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 3/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 3
S. Wallentowitz
Remember: Peterson‘s Lock
Add shared variable victim
Interleaved execution:
the last write to victim gives access
to the other thread
Works for two threads
Extend for 3 threads
volatile bool flag[2];
volatile int victim;
void lock() {
int me=get_thread_id();
int other=1-me;
flag[me]=true;victim=me;
while(flag[other] &&
victim==me){ do nothing }
}
void unlock() {
int me=get_thread_id();flag[me]=false;
}
I am
interested
let the othergo first
Thread 0 Thread 1
flag[0]=true
Initially: flag={false,false}
while (..)
t
Critical
Section
flag[1]=true
while (..)
victim=0 victim=1
memory
ordervictim=0
victim=1
while (..)
while (..)
Critical
Section
memory
ordervictim=1
victim=0
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 4/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 4
S. Wallentowitz
volatile bool flag[3];
volatile int victim;
void lock() {
int me=get_thread_id();
int other=1-me;
flag[me]=true;
victim=me;
while(flag[other] &&
victim==me){ do nothing }
}
void unlock() {
int me=get_thread_id();
flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 5/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 5
S. Wallentowitz
volatile bool flag[3];
volatile int victim;
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;
victim=me;
while(flag[other] &&
victim==me){ do nothing }
}
void unlock() {
int me=get_thread_id();flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 6/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 6
S. Wallentowitz
volatile bool flag[3];
volatile int victim;
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;
victim=me;
while((flag[left] || flag[right])
&& victim==me){ do nothing }
}
void unlock() {
int me=get_thread_id();flag[me]=false;
}
T0 T1 T2
tMemory
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 7/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 7
S. Wallentowitz
volatile bool flag[3];
volatile int victim;
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;
victim=me;
while((flag[left] || flag[right])
&& victim==me){ do nothing }
}
void unlock() {
int me=get_thread_id();flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 8/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 8S. Wallentowitz
volatile bool flag[3];
volatile int victim[2];
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;
victim[0]=me;
victim[1]=me;
while((flag[left] || flag[right])
&& victim[0]==me
&& victim[1]==me) { do nothing }
}
void unlock() {
int me=get_thread_id();
flag[me]=false;
}
T0 T1 T2
tMemory
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 9/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 9S. Wallentowitz
volatile bool flag[3];
volatile int victim[2];
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;
victim[0]=me;
victim[1]=me;
while((flag[left] || flag[right])
&& victim[0]==me
&& victim[1]==me) { do nothing }
}
void unlock() {
int me=get_thread_id();
flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 10/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 10S. Wallentowitz
...
void lock() {...
flag[me]=true;
victim[0]=me;
while((flag[left] || flag[right])
&& victim[0]==me) { do nothing }
victim[1]=me;
while((flag[left] || flag[right])
&& victim[1]==me) { do nothing }
}
void unlock() {
int me=get_thread_id();
flag[me]=false;}
T0 T1 T2 Memory
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 11/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 11S. Wallentowitz
volatile bool flag[3];
volatile int victim[2];
int lefts[3]={2,0,1};int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
flag[me]=true;victim[0]=me;
while((flag[left] || flag[right])
&& victim[0]==me) { do nothing }
victim[1]=me;
while((flag[left] || flag[right])
&& victim[1]==me) { do nothing }}
void unlock() {
int me=get_thread_id();
flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 12/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 12S. Wallentowitz
volatile int level[3];
volatile int victim[2];
int lefts[3]={2,0,1};
int rights[3]={1,2,0};
void lock() {
int me=get_thread_id();
int left=lefts[me];
int right=rights[me];
level[me]=1;
victim[0]=me;while((level[left]>=1 ||
level[right]>=1)
&& victim[0]==me) { do nothing }
level[me]=2;
victim[1]=me;
while((level[left]==2 ||
level[right]==2)&& victim[1]==me) { do nothing }
}
void unlock() {
int me=get_thread_id();
flag[me]=false;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 13/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 13S. Wallentowitz
Task 3.2: Locking
On the right you find a piece of
source code to implement a
stack. In a stack, the last written
element is read as first (LIFO).
Use the functionsmutex_lock(mutex*) and
mutex_unlock(mutex*) to
make the code thread-safe.
#define N 100
int buf[N];
int c=0;
mutex lock;
void write(int i) {
int tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;
}
int read() {
int r, tmp=c;
while(tmp==0) {tmp=c;
}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 14/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 14S. Wallentowitz
void write(int i) {
int tmp
tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 15/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 15S. Wallentowitz
int read() {
int r, tmp
tmp = c;
while(tmp==0) {
tmp=c;}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 16/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 16S. Wallentowitz
void write(int i) {
int tmp
tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;
}
int read() {
int r, tmp
tmp = c;
while(tmp==0) {
tmp=c;
}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 17/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 17S. Wallentowitz
void write(int i) {
int tmp
tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;
}
int read() {
int r, tmp
tmp = c;
while(tmp==0) {
tmp=c;
}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 18/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 18S. Wallentowitz
void write(int i) {
int tmp
tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;
}
int read() {
int r, tmp
tmp = c;
while(tmp==N) {
tmp=c;
}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 19/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 19S. Wallentowitz
void write(int i) {
int tmp
tmp=c;
while(tmp==N) {
tmp=c;
}
buf[c]=i;
c=c+1;
}
int read() {
int r, tmp
tmp = c;
while(tmp==N) {
tmp=c;
}
r=buf[c];
c=c-1;
return r;
}
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 20/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 20S. Wallentowitz
Task 3.3: Spinlocks vs. Blocking locks
In the context of mutexes, explain what the difference betweenspinlocks and blocking locks is.
8/12/2019 Tutorial03 Solution
http://slidepdf.com/reader/full/tutorial03-solution 21/21
Technische Universität München
© Institute for Integrated SystemsChip Multicore Processors – Tutorial 3 – 21S Wallentowitz