SlideShare une entreprise Scribd logo
1  sur  48
Télécharger pour lire hors ligne
Lab 4: If you liked it, then you should have put a
“lock” on it
Advanced Operating Systems

Zubair Nabi
zubair.nabi@itu.edu.pk

February 20, 2013
Concurrency within the OS

1

Multiple CPUs share kernel data structures
• Can interfere with each other leading to inconsistencies

2

Even on uniprocessors, interrupt handlers can interfere with
non-interrupt code

3

xv6 uses locks for both situations
Concurrency within the OS

1

Multiple CPUs share kernel data structures
• Can interfere with each other leading to inconsistencies

2

Even on uniprocessors, interrupt handlers can interfere with
non-interrupt code

3

xv6 uses locks for both situations
Concurrency within the OS

1

Multiple CPUs share kernel data structures
• Can interfere with each other leading to inconsistencies

2

Even on uniprocessors, interrupt handlers can interfere with
non-interrupt code

3

xv6 uses locks for both situations
Race conditions: Example

• Several processors share a single disk
• Disk driver has a single linked list idequeue of outstanding disk
requests
• Processors concurrently make a call to iderw which adds a
request to the list
Race conditions: Example

• Several processors share a single disk
• Disk driver has a single linked list idequeue of outstanding disk
requests
• Processors concurrently make a call to iderw which adds a
request to the list
Race conditions: Example

• Several processors share a single disk
• Disk driver has a single linked list idequeue of outstanding disk
requests
• Processors concurrently make a call to iderw which adds a
request to the list
Race conditions (2): Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

struct list {
int data;
struct list ∗ next;
};
struct list ∗ list = 0;
void insert (int data)
{
struct list ∗ l;
l = malloc ( sizeof ∗ l);
l −>data = data;
l −>next = list;
list = l;
}
Race conditions (3): Problem

• Possible race condition on line 13 and 14
• In case of two concurrent insertions, the first one will be lost
Race conditions (3): Problem

• Possible race condition on line 13 and 14
• In case of two concurrent insertions, the first one will be lost
Race conditions (4): Solution
1
2
3
4
5
6
7
8
9
10
11
12
13

struct list ∗ list = 0;
struct lock listlock ;
void insert (int data)
{
struct list ∗ l;
acquire (& listlock );
l = malloc ( sizeof ∗ l);
l −>data = data;
l −>next = list;
list = l;
release (& listlock );
}
Lock representation

1
2
3

struct spinlock {
uint locked ;
};
Acquire lock

1
2
3
4
5
6
7
8
9

void acquire ( struct spinlock ∗ lk)
{
for (;;) {
if(!lk −>locked ) {
lk −>locked = 1;
break ;
}
}
}
Acquiring lock (2): Problem

• Possible race condition on line 4 and 5
• So the solution itself causes a race condition!
Acquiring lock (2): Problem

• Possible race condition on line 4 and 5
• So the solution itself causes a race condition!
Hardware support

1
2
3
4
5
6
7
8
9
10

static inline uint
xchg( volatile uint ∗ addr , uint newval )
{
uint result ;
asm volatile ("lock; xchgl %0, %1" :
"+m" ( ∗ addr), "=a" ( result ) :
"1" ( newval ) :
"cc");
}
Atomic acquire lock

1
2
3
4
5
6
7
8
9
10

void acquire ( struct spinlock ∗ lk)
{
pushcli (); // disable interrupts .
if( holding (lk))
panic (" acquire ");
while (xchg (&lk −>locked , 1) != 0)
;
}
Atomic release lock

1
2
3
4
5
6
7
8
9

void release ( struct spinlock ∗ lk)
{
if(! holding (lk))
panic (" release ");
xchg (&lk −>locked , 0);
popcli ();
}
Recursive

• What happens if a callee tries to acquire a lock held by its caller?
• Solution: recursive locks
• But they do not ensure mutual exclusion between the caller and
the callee
• Pass the buck to the programmer
Recursive

• What happens if a callee tries to acquire a lock held by its caller?
• Solution: recursive locks
• But they do not ensure mutual exclusion between the caller and
the callee
• Pass the buck to the programmer
Recursive

• What happens if a callee tries to acquire a lock held by its caller?
• Solution: recursive locks
• But they do not ensure mutual exclusion between the caller and
the callee
• Pass the buck to the programmer
Recursive

• What happens if a callee tries to acquire a lock held by its caller?
• Solution: recursive locks
• But they do not ensure mutual exclusion between the caller and
the callee
• Pass the buck to the programmer
Lock usage example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void iderw ( struct buf ∗ b)
{
struct buf ∗ ∗ pp;
acquire (& idelock );
b −>qnext = 0;
for(pp =& idequeue ; ∗ pp; pp =&( ∗ pp)−>qnext)
;
∗ pp = b;
// Wait for request to finish .
while ((b −>flags & ( B_VALID | B_DIRTY ))
!= B_VALID ){
sleep (b, & idelock );
}
release (& idelock );
}
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
When to use locks

• To use:
1
2

A variable can concurrently be written to by multiple CPUs
An invariant spans multiple data structures

• Possible to protect the kernel through a “giant kernel lock”
• Problem(s)?
• Reminiscent of the GIL in Python

• Possible to have fine-grained locks
Lock ordering

• If code path x needs locks in the order A and B while y needs
them in order B and A, could there be a problem?
• Need to ensure that all code paths acquire locks in the same
order
Lock ordering

• If code path x needs locks in the order A and B while y needs
them in order B and A, could there be a problem?
• Need to ensure that all code paths acquire locks in the same
order
Interrupt handlers

• Locks are also used to synchronize access between interrupt
handlers and non-interrupt code
Interrupt handlers (2): Example

1
2
3
4
5
6
7
8
9

T_IRQ0 + IRQ_TIMER :
if(cpu −>id == 0){
acquire (& tickslock );
ticks ++;
wakeup (& ticks );
release (& tickslock );
}
lapiceoi ();
break ;
Interrupt handlers (3): Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void sys_sleep (void ){
int n;
uint ticks0 ;
if( argint (0, &n) < 0)
return −1;
acquire (& tickslock );
ticks0 = ticks;
while ( ticks − ticks0 < n){
if(proc −>killed ){
release (& tickslock );
return −1;
}
sleep (& ticks , & tickslock );
}
release (& tickslock );
Interrupt handlers

• Interrupts lead to concurrency problems on uniprocessors too
• Disable interrupts before acquiring a lock: pushcli()
• Re-enable on release: popcli()
• Avoids recursive locks
Interrupt handlers

• Interrupts lead to concurrency problems on uniprocessors too
• Disable interrupts before acquiring a lock: pushcli()
• Re-enable on release: popcli()
• Avoids recursive locks
Interrupt handlers

• Interrupts lead to concurrency problems on uniprocessors too
• Disable interrupts before acquiring a lock: pushcli()
• Re-enable on release: popcli()
• Avoids recursive locks
Interrupt handlers

• Interrupts lead to concurrency problems on uniprocessors too
• Disable interrupts before acquiring a lock: pushcli()
• Re-enable on release: popcli()
• Avoids recursive locks
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task
• xv6 processes only have a single thread so processes do not
share any memory
• But it is definitely possible to add multithreading
• Imagine that you have been provided a library with methods to
create (xv6_thread_create()) and block
(xv6_thread_join()) threads
• xv6 already provides you with a spinlock with methods to
acquire and release that lock.
• In addition, it also has methods to:
1 Put a thread to sleep (sleep(void *chan, struct
spinlock *lk)) on a variable (*chan) (it releases lk before
2

sleeping and then reacquires it after waking up)
Wake up (wakeup(void *chan)) threads sleeping on a
variable (*chan)
Today’s Task (2)

• Design a thread-safe library (in pseudo code) for xv6 that uses
these existing primitives, to implement semaphores (binary and
counting) and conditional variables and their various methods
• Also, add commenting to describe why your solution is
thread-safe
Reading

• Chapter 4 from “xv6: a simple, Unix-like teaching operating
system”
• Timothy L. Harris. 2001. A Pragmatic Implementation of
Non-blocking Linked-Lists. In Proceedings of the 15th
International Conference on Distributed Computing (DISC ’01),
Jennifer L. Welch (Ed.). Springer-Verlag, London, UK, 300-314.
Online: http:

//www.timharris.co.uk/papers/2001-disc.pdf

Contenu connexe

Tendances

Kernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkKernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkAnne Nicolas
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux InterruptsKernel TLV
 
SecureCore RTAS2013
SecureCore RTAS2013SecureCore RTAS2013
SecureCore RTAS2013mkyoon83
 
The Silence of the Canaries
The Silence of the CanariesThe Silence of the Canaries
The Silence of the CanariesKernel TLV
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersKernel TLV
 
Stateless Hypervisors at Scale
Stateless Hypervisors at ScaleStateless Hypervisors at Scale
Stateless Hypervisors at ScaleAntony Messerl
 
High Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux KernelHigh Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux KernelKernel TLV
 
LISA2010 visualizations
LISA2010 visualizationsLISA2010 visualizations
LISA2010 visualizationsBrendan Gregg
 
Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Brendan Gregg
 
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdf
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdfLinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdf
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdfdegarden
 
DTrace Topics: Introduction
DTrace Topics: IntroductionDTrace Topics: Introduction
DTrace Topics: IntroductionBrendan Gregg
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and moreBrendan Gregg
 
What Linux can learn from Solaris performance and vice-versa
What Linux can learn from Solaris performance and vice-versaWhat Linux can learn from Solaris performance and vice-versa
What Linux can learn from Solaris performance and vice-versaBrendan Gregg
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityAndrew Case
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureAnne Nicolas
 
Block I/O Layer Tracing: blktrace
Block I/O Layer Tracing: blktraceBlock I/O Layer Tracing: blktrace
Block I/O Layer Tracing: blktraceBabak Farrokhi
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF SuperpowersBrendan Gregg
 

Tendances (20)

Kernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkKernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver framework
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
SecureCore RTAS2013
SecureCore RTAS2013SecureCore RTAS2013
SecureCore RTAS2013
 
The Silence of the Canaries
The Silence of the CanariesThe Silence of the Canaries
The Silence of the Canaries
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
 
Stateless Hypervisors at Scale
Stateless Hypervisors at ScaleStateless Hypervisors at Scale
Stateless Hypervisors at Scale
 
High Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux KernelHigh Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux Kernel
 
LISA2010 visualizations
LISA2010 visualizationsLISA2010 visualizations
LISA2010 visualizations
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdf
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdfLinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdf
LinuxCon_2013_NA_Eckermann_Filesystems_btrfs.pdf
 
DTrace Topics: Introduction
DTrace Topics: IntroductionDTrace Topics: Introduction
DTrace Topics: Introduction
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and more
 
What Linux can learn from Solaris performance and vice-versa
What Linux can learn from Solaris performance and vice-versaWhat Linux can learn from Solaris performance and vice-versa
What Linux can learn from Solaris performance and vice-versa
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
 
Speeding up ps and top
Speeding up ps and topSpeeding up ps and top
Speeding up ps and top
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architecture
 
Block I/O Layer Tracing: blktrace
Block I/O Layer Tracing: blktraceBlock I/O Layer Tracing: blktrace
Block I/O Layer Tracing: blktrace
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
 

En vedette

AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondZubair Nabi
 
AOS Lab 7: Page tables
AOS Lab 7: Page tablesAOS Lab 7: Page tables
AOS Lab 7: Page tablesZubair Nabi
 
Topic 13: Cloud Stacks
Topic 13: Cloud StacksTopic 13: Cloud Stacks
Topic 13: Cloud StacksZubair Nabi
 
AOS Lab 9: File system -- Of buffers, logs, and blocks
AOS Lab 9: File system -- Of buffers, logs, and blocksAOS Lab 9: File system -- Of buffers, logs, and blocks
AOS Lab 9: File system -- Of buffers, logs, and blocksZubair Nabi
 
AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!Zubair Nabi
 
Topic 14: Operating Systems and Virtualization
Topic 14: Operating Systems and VirtualizationTopic 14: Operating Systems and Virtualization
Topic 14: Operating Systems and VirtualizationZubair Nabi
 
AOS Lab 12: Network Communication
AOS Lab 12: Network CommunicationAOS Lab 12: Network Communication
AOS Lab 12: Network CommunicationZubair Nabi
 
Topic 15: Datacenter Design and Networking
Topic 15: Datacenter Design and NetworkingTopic 15: Datacenter Design and Networking
Topic 15: Datacenter Design and NetworkingZubair Nabi
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!Zubair Nabi
 
MapReduce and DBMS Hybrids
MapReduce and DBMS HybridsMapReduce and DBMS Hybrids
MapReduce and DBMS HybridsZubair Nabi
 
Raabta: Low-cost Video Conferencing for the Developing World
Raabta: Low-cost Video Conferencing for the Developing WorldRaabta: Low-cost Video Conferencing for the Developing World
Raabta: Low-cost Video Conferencing for the Developing WorldZubair Nabi
 
The Anatomy of Web Censorship in Pakistan
The Anatomy of Web Censorship in PakistanThe Anatomy of Web Censorship in Pakistan
The Anatomy of Web Censorship in PakistanZubair Nabi
 
MapReduce Application Scripting
MapReduce Application ScriptingMapReduce Application Scripting
MapReduce Application ScriptingZubair Nabi
 
The Big Data Stack
The Big Data StackThe Big Data Stack
The Big Data StackZubair Nabi
 

En vedette (14)

AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyond
 
AOS Lab 7: Page tables
AOS Lab 7: Page tablesAOS Lab 7: Page tables
AOS Lab 7: Page tables
 
Topic 13: Cloud Stacks
Topic 13: Cloud StacksTopic 13: Cloud Stacks
Topic 13: Cloud Stacks
 
AOS Lab 9: File system -- Of buffers, logs, and blocks
AOS Lab 9: File system -- Of buffers, logs, and blocksAOS Lab 9: File system -- Of buffers, logs, and blocks
AOS Lab 9: File system -- Of buffers, logs, and blocks
 
AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!
 
Topic 14: Operating Systems and Virtualization
Topic 14: Operating Systems and VirtualizationTopic 14: Operating Systems and Virtualization
Topic 14: Operating Systems and Virtualization
 
AOS Lab 12: Network Communication
AOS Lab 12: Network CommunicationAOS Lab 12: Network Communication
AOS Lab 12: Network Communication
 
Topic 15: Datacenter Design and Networking
Topic 15: Datacenter Design and NetworkingTopic 15: Datacenter Design and Networking
Topic 15: Datacenter Design and Networking
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
 
MapReduce and DBMS Hybrids
MapReduce and DBMS HybridsMapReduce and DBMS Hybrids
MapReduce and DBMS Hybrids
 
Raabta: Low-cost Video Conferencing for the Developing World
Raabta: Low-cost Video Conferencing for the Developing WorldRaabta: Low-cost Video Conferencing for the Developing World
Raabta: Low-cost Video Conferencing for the Developing World
 
The Anatomy of Web Censorship in Pakistan
The Anatomy of Web Censorship in PakistanThe Anatomy of Web Censorship in Pakistan
The Anatomy of Web Censorship in Pakistan
 
MapReduce Application Scripting
MapReduce Application ScriptingMapReduce Application Scripting
MapReduce Application Scripting
 
The Big Data Stack
The Big Data StackThe Big Data Stack
The Big Data Stack
 

Similaire à AOS Lab 4: If you liked it, then you should have put a “lock” on it

Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threadsSyed Zaid Irshad
 
Verification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAVerification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAUniversität Rostock
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCDrsebbe
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterLucidworks
 
LXC Containers and AUFs
LXC Containers and AUFsLXC Containers and AUFs
LXC Containers and AUFsDocker, Inc.
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waitingRoman Elizarov
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Jared Roesch
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Roman Elizarov
 
APEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLAPEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLConnor McDonald
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 
CNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection MechanismsCNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection MechanismsSam Bowne
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesCharles Nutter
 
Towards a Scalable Non-Blocking Coding Style
Towards a Scalable Non-Blocking Coding StyleTowards a Scalable Non-Blocking Coding Style
Towards a Scalable Non-Blocking Coding StyleAzul Systems Inc.
 
CNIT 127: 3: Shellcode
CNIT 127: 3: ShellcodeCNIT 127: 3: Shellcode
CNIT 127: 3: ShellcodeSam Bowne
 

Similaire à AOS Lab 4: If you liked it, then you should have put a “lock” on it (20)

无锁编程
无锁编程无锁编程
无锁编程
 
Jvm memory model
Jvm memory modelJvm memory model
Jvm memory model
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threads
 
Hoard_2022AIM1001.pptx.pdf
Hoard_2022AIM1001.pptx.pdfHoard_2022AIM1001.pptx.pdf
Hoard_2022AIM1001.pptx.pdf
 
Verification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAVerification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLA
 
Kernel
KernelKernel
Kernel
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, Twitter
 
LXC Containers and AUFs
LXC Containers and AUFsLXC Containers and AUFs
LXC Containers and AUFs
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waiting
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!
 
APEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQLAPEX Connect 2019 - array/bulk processing in PLSQL
APEX Connect 2019 - array/bulk processing in PLSQL
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
spinlock.pdf
spinlock.pdfspinlock.pdf
spinlock.pdf
 
CNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection MechanismsCNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection Mechanisms
 
Memory model
Memory modelMemory model
Memory model
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
Towards a Scalable Non-Blocking Coding Style
Towards a Scalable Non-Blocking Coding StyleTowards a Scalable Non-Blocking Coding Style
Towards a Scalable Non-Blocking Coding Style
 
CNIT 127: 3: Shellcode
CNIT 127: 3: ShellcodeCNIT 127: 3: Shellcode
CNIT 127: 3: Shellcode
 

Plus de Zubair Nabi

Lab 5: Interconnecting a Datacenter using Mininet
Lab 5: Interconnecting a Datacenter using MininetLab 5: Interconnecting a Datacenter using Mininet
Lab 5: Interconnecting a Datacenter using MininetZubair Nabi
 
Topic 12: NoSQL in Action
Topic 12: NoSQL in ActionTopic 12: NoSQL in Action
Topic 12: NoSQL in ActionZubair Nabi
 
Lab 4: Interfacing with Cassandra
Lab 4: Interfacing with CassandraLab 4: Interfacing with Cassandra
Lab 4: Interfacing with CassandraZubair Nabi
 
Topic 10: Taxonomy of Data and Storage
Topic 10: Taxonomy of Data and StorageTopic 10: Taxonomy of Data and Storage
Topic 10: Taxonomy of Data and StorageZubair Nabi
 
Topic 11: Google Filesystem
Topic 11: Google FilesystemTopic 11: Google Filesystem
Topic 11: Google FilesystemZubair Nabi
 
Lab 3: Writing a Naiad Application
Lab 3: Writing a Naiad ApplicationLab 3: Writing a Naiad Application
Lab 3: Writing a Naiad ApplicationZubair Nabi
 
Topic 8: Enhancements and Alternative Architectures
Topic 8: Enhancements and Alternative ArchitecturesTopic 8: Enhancements and Alternative Architectures
Topic 8: Enhancements and Alternative ArchitecturesZubair Nabi
 
Topic 7: Shortcomings in the MapReduce Paradigm
Topic 7: Shortcomings in the MapReduce ParadigmTopic 7: Shortcomings in the MapReduce Paradigm
Topic 7: Shortcomings in the MapReduce ParadigmZubair Nabi
 
Lab 1: Introduction to Amazon EC2 and MPI
Lab 1: Introduction to Amazon EC2 and MPILab 1: Introduction to Amazon EC2 and MPI
Lab 1: Introduction to Amazon EC2 and MPIZubair Nabi
 
Topic 6: MapReduce Applications
Topic 6: MapReduce ApplicationsTopic 6: MapReduce Applications
Topic 6: MapReduce ApplicationsZubair Nabi
 

Plus de Zubair Nabi (11)

Lab 5: Interconnecting a Datacenter using Mininet
Lab 5: Interconnecting a Datacenter using MininetLab 5: Interconnecting a Datacenter using Mininet
Lab 5: Interconnecting a Datacenter using Mininet
 
Topic 12: NoSQL in Action
Topic 12: NoSQL in ActionTopic 12: NoSQL in Action
Topic 12: NoSQL in Action
 
Lab 4: Interfacing with Cassandra
Lab 4: Interfacing with CassandraLab 4: Interfacing with Cassandra
Lab 4: Interfacing with Cassandra
 
Topic 10: Taxonomy of Data and Storage
Topic 10: Taxonomy of Data and StorageTopic 10: Taxonomy of Data and Storage
Topic 10: Taxonomy of Data and Storage
 
Topic 11: Google Filesystem
Topic 11: Google FilesystemTopic 11: Google Filesystem
Topic 11: Google Filesystem
 
Lab 3: Writing a Naiad Application
Lab 3: Writing a Naiad ApplicationLab 3: Writing a Naiad Application
Lab 3: Writing a Naiad Application
 
Topic 9: MR+
Topic 9: MR+Topic 9: MR+
Topic 9: MR+
 
Topic 8: Enhancements and Alternative Architectures
Topic 8: Enhancements and Alternative ArchitecturesTopic 8: Enhancements and Alternative Architectures
Topic 8: Enhancements and Alternative Architectures
 
Topic 7: Shortcomings in the MapReduce Paradigm
Topic 7: Shortcomings in the MapReduce ParadigmTopic 7: Shortcomings in the MapReduce Paradigm
Topic 7: Shortcomings in the MapReduce Paradigm
 
Lab 1: Introduction to Amazon EC2 and MPI
Lab 1: Introduction to Amazon EC2 and MPILab 1: Introduction to Amazon EC2 and MPI
Lab 1: Introduction to Amazon EC2 and MPI
 
Topic 6: MapReduce Applications
Topic 6: MapReduce ApplicationsTopic 6: MapReduce Applications
Topic 6: MapReduce Applications
 

Dernier

All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 

Dernier (20)

All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 

AOS Lab 4: If you liked it, then you should have put a “lock” on it

  • 1. Lab 4: If you liked it, then you should have put a “lock” on it Advanced Operating Systems Zubair Nabi zubair.nabi@itu.edu.pk February 20, 2013
  • 2. Concurrency within the OS 1 Multiple CPUs share kernel data structures • Can interfere with each other leading to inconsistencies 2 Even on uniprocessors, interrupt handlers can interfere with non-interrupt code 3 xv6 uses locks for both situations
  • 3. Concurrency within the OS 1 Multiple CPUs share kernel data structures • Can interfere with each other leading to inconsistencies 2 Even on uniprocessors, interrupt handlers can interfere with non-interrupt code 3 xv6 uses locks for both situations
  • 4. Concurrency within the OS 1 Multiple CPUs share kernel data structures • Can interfere with each other leading to inconsistencies 2 Even on uniprocessors, interrupt handlers can interfere with non-interrupt code 3 xv6 uses locks for both situations
  • 5. Race conditions: Example • Several processors share a single disk • Disk driver has a single linked list idequeue of outstanding disk requests • Processors concurrently make a call to iderw which adds a request to the list
  • 6. Race conditions: Example • Several processors share a single disk • Disk driver has a single linked list idequeue of outstanding disk requests • Processors concurrently make a call to iderw which adds a request to the list
  • 7. Race conditions: Example • Several processors share a single disk • Disk driver has a single linked list idequeue of outstanding disk requests • Processors concurrently make a call to iderw which adds a request to the list
  • 8. Race conditions (2): Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 struct list { int data; struct list ∗ next; }; struct list ∗ list = 0; void insert (int data) { struct list ∗ l; l = malloc ( sizeof ∗ l); l −>data = data; l −>next = list; list = l; }
  • 9. Race conditions (3): Problem • Possible race condition on line 13 and 14 • In case of two concurrent insertions, the first one will be lost
  • 10. Race conditions (3): Problem • Possible race condition on line 13 and 14 • In case of two concurrent insertions, the first one will be lost
  • 11. Race conditions (4): Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 struct list ∗ list = 0; struct lock listlock ; void insert (int data) { struct list ∗ l; acquire (& listlock ); l = malloc ( sizeof ∗ l); l −>data = data; l −>next = list; list = l; release (& listlock ); }
  • 13. Acquire lock 1 2 3 4 5 6 7 8 9 void acquire ( struct spinlock ∗ lk) { for (;;) { if(!lk −>locked ) { lk −>locked = 1; break ; } } }
  • 14. Acquiring lock (2): Problem • Possible race condition on line 4 and 5 • So the solution itself causes a race condition!
  • 15. Acquiring lock (2): Problem • Possible race condition on line 4 and 5 • So the solution itself causes a race condition!
  • 16. Hardware support 1 2 3 4 5 6 7 8 9 10 static inline uint xchg( volatile uint ∗ addr , uint newval ) { uint result ; asm volatile ("lock; xchgl %0, %1" : "+m" ( ∗ addr), "=a" ( result ) : "1" ( newval ) : "cc"); }
  • 17. Atomic acquire lock 1 2 3 4 5 6 7 8 9 10 void acquire ( struct spinlock ∗ lk) { pushcli (); // disable interrupts . if( holding (lk)) panic (" acquire "); while (xchg (&lk −>locked , 1) != 0) ; }
  • 18. Atomic release lock 1 2 3 4 5 6 7 8 9 void release ( struct spinlock ∗ lk) { if(! holding (lk)) panic (" release "); xchg (&lk −>locked , 0); popcli (); }
  • 19. Recursive • What happens if a callee tries to acquire a lock held by its caller? • Solution: recursive locks • But they do not ensure mutual exclusion between the caller and the callee • Pass the buck to the programmer
  • 20. Recursive • What happens if a callee tries to acquire a lock held by its caller? • Solution: recursive locks • But they do not ensure mutual exclusion between the caller and the callee • Pass the buck to the programmer
  • 21. Recursive • What happens if a callee tries to acquire a lock held by its caller? • Solution: recursive locks • But they do not ensure mutual exclusion between the caller and the callee • Pass the buck to the programmer
  • 22. Recursive • What happens if a callee tries to acquire a lock held by its caller? • Solution: recursive locks • But they do not ensure mutual exclusion between the caller and the callee • Pass the buck to the programmer
  • 23. Lock usage example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void iderw ( struct buf ∗ b) { struct buf ∗ ∗ pp; acquire (& idelock ); b −>qnext = 0; for(pp =& idequeue ; ∗ pp; pp =&( ∗ pp)−>qnext) ; ∗ pp = b; // Wait for request to finish . while ((b −>flags & ( B_VALID | B_DIRTY )) != B_VALID ){ sleep (b, & idelock ); } release (& idelock ); }
  • 24. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 25. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 26. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 27. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 28. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 29. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 30. When to use locks • To use: 1 2 A variable can concurrently be written to by multiple CPUs An invariant spans multiple data structures • Possible to protect the kernel through a “giant kernel lock” • Problem(s)? • Reminiscent of the GIL in Python • Possible to have fine-grained locks
  • 31. Lock ordering • If code path x needs locks in the order A and B while y needs them in order B and A, could there be a problem? • Need to ensure that all code paths acquire locks in the same order
  • 32. Lock ordering • If code path x needs locks in the order A and B while y needs them in order B and A, could there be a problem? • Need to ensure that all code paths acquire locks in the same order
  • 33. Interrupt handlers • Locks are also used to synchronize access between interrupt handlers and non-interrupt code
  • 34. Interrupt handlers (2): Example 1 2 3 4 5 6 7 8 9 T_IRQ0 + IRQ_TIMER : if(cpu −>id == 0){ acquire (& tickslock ); ticks ++; wakeup (& ticks ); release (& tickslock ); } lapiceoi (); break ;
  • 35. Interrupt handlers (3): Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void sys_sleep (void ){ int n; uint ticks0 ; if( argint (0, &n) < 0) return −1; acquire (& tickslock ); ticks0 = ticks; while ( ticks − ticks0 < n){ if(proc −>killed ){ release (& tickslock ); return −1; } sleep (& ticks , & tickslock ); } release (& tickslock );
  • 36. Interrupt handlers • Interrupts lead to concurrency problems on uniprocessors too • Disable interrupts before acquiring a lock: pushcli() • Re-enable on release: popcli() • Avoids recursive locks
  • 37. Interrupt handlers • Interrupts lead to concurrency problems on uniprocessors too • Disable interrupts before acquiring a lock: pushcli() • Re-enable on release: popcli() • Avoids recursive locks
  • 38. Interrupt handlers • Interrupts lead to concurrency problems on uniprocessors too • Disable interrupts before acquiring a lock: pushcli() • Re-enable on release: popcli() • Avoids recursive locks
  • 39. Interrupt handlers • Interrupts lead to concurrency problems on uniprocessors too • Disable interrupts before acquiring a lock: pushcli() • Re-enable on release: popcli() • Avoids recursive locks
  • 40. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 41. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 42. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 43. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 44. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 45. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 46. Today’s Task • xv6 processes only have a single thread so processes do not share any memory • But it is definitely possible to add multithreading • Imagine that you have been provided a library with methods to create (xv6_thread_create()) and block (xv6_thread_join()) threads • xv6 already provides you with a spinlock with methods to acquire and release that lock. • In addition, it also has methods to: 1 Put a thread to sleep (sleep(void *chan, struct spinlock *lk)) on a variable (*chan) (it releases lk before 2 sleeping and then reacquires it after waking up) Wake up (wakeup(void *chan)) threads sleeping on a variable (*chan)
  • 47. Today’s Task (2) • Design a thread-safe library (in pseudo code) for xv6 that uses these existing primitives, to implement semaphores (binary and counting) and conditional variables and their various methods • Also, add commenting to describe why your solution is thread-safe
  • 48. Reading • Chapter 4 from “xv6: a simple, Unix-like teaching operating system” • Timothy L. Harris. 2001. A Pragmatic Implementation of Non-blocking Linked-Lists. In Proceedings of the 15th International Conference on Distributed Computing (DISC ’01), Jennifer L. Welch (Ed.). Springer-Verlag, London, UK, 300-314. Online: http: //www.timharris.co.uk/papers/2001-disc.pdf