Highlights
1 Introduction
Threads are execution entities like processes but are extremely light weight. They are used extensively for parallelization of large problems. Threads share the same code segments and data segments, hence switching between threads has much lesser overhead than switching between processes. Each Thread has separate stacks and registers. Thread synchronization is used for concurrent execution of two or more threads that share critical resources.
Threads should be synchronized to avoid conflicts while using critical resources. Otherwise, conflicts may arise when parallel- running threads attempt to modify a common variable at the same time, leading to unwanted results.
A mutex is a lockable object that is designed to signal when critical sections of code need exclusive access, preventing other threads with the same protection from executing concurrently and access the same memory locations.
In parallel computing, a barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.
The basic barrier has mainly two variables, one of which records the pass/stop state of the barrier, the other of which keeps the total number of threads that have entered in the barrier. The barrier state was initialized to be ”stop” by the first threads coming into the barrier. Whenever a thread enters, based on the number of threads already in the barrier, only if it is the last one, the thread sets the barrier state to be ”pass” so that all the threads can get out of the barrier. On the other hand, when the incoming thread is not the last one, it is trapped in the barrier and keeps testing if the barrier state has changed from ”stop” to ”pass”, and it gets out only when the barrier statechanges to ”pass”. In this week’s first assignment we will be mainly focusing on the implementation of threads in xv6 and implement switching between user threads. In the second and third assignments we will see some common difficulties that can arise due to improper implementation of threads, and we try to resolve it using mutex and barriers.
2 Problem 1
This assignment is about thread implementation is xv6. Your task is to implement a context-switch mechanism for user-level threads. Before starting the assignment do the following:
$ git clone git://g.csail.mit.edu/xv6-labs-2020
$ cd xv6-labs-2020
$ git fetch
$ git checkout thread
$ make clean
After completing the steps look into the user/uthread.c and user/uthread switch.S files. In uthread.c most of the thread-
ing package has already been implemented and code for three simple test threads has been given.
2.1 Task
You have to complete the thread create() and thread schedule() methods. In uthread switch.S you have to add the code for switching the thread context. This will be called from the thread scheduler() in uthread.c.
Tentative Output :(Might not be in the same order)
$ uthread
thread a started
thread b started
thread c started
thread c 0
thread a 0
thread b 0
..........
thread c 99
thread a 99
thread b 99
thread c: exit after 100
thread a: exit after 100
thread b: exit after 100
thread schedule: no runnable threads $
2.2 Questions
1. While between two threads what is saved as a part of context?
2. Explain the changes that you made in uthread.c and uthread switch.S?
3. Can an user level thread exist after termination of the containing process?
3 Problem 2
In this problem we explore parallel programming with locks in Linux or MacOS(not xv6). To do this assignment you have to go through the different pthread operations in the pthread library.
In the notxv6/ph.c file, threads are used to insert and retrieve keys from a hashtable, when run with single thread($ ./ph number of threads) Fig 1, we see that no key is missing. But when run with 2 threads we get output similar to Fig 2. We see that there is a 2.2x speedup in the puts/second, but there is a lot of key that is missing while retrieving(gets). The puts/sec,gets/sec and time changes from system to system based on number of cores, existing load and speed.
3.1 Tasks
1. Make sure that there is no key missing while using 2 threads
Hint: use pthread mutex t and the init,lock,unlock operations associated with it
2. After completing 1 ensure that the speedup in puts/sec is at least 1.25x the puts/sec rate with single thread.
3.2 Questions
1. What are the different operations you used on mutex for this assignment?
2. Explain briefly why keys were missing initially when running 2 threads?
3. What changes did you make to avoid missing any keys?
4. Position in which you apply lock and unlock on mutex impacts the puts/sec, justify where you used them to maximize the puts/sec.
4 Problem 3
Barrier is a point in the execution of application where each participant threads must wait until all the other threads have reached that point too. The barrier function in notxv6/barrier.c is incomplete. Hence when you execute it thread
it fails the assertion test.
$ make barrier
$ ./barrier 2
barrier: notxv6/barrier.c:42: thread: Assertion ‘i == t’ failed.
4.1 Task
1. Implement the barrier function properly so that no thread leaves before the other. Hint :
pthread cond wait(&cond, &mutex); // go to sleep on cond, releasing lock mutex, acquiring upon wake up pthread cond broadcast(&cond); // wake up every thread sleeping on cond
4.2 Question
1. Give an example of a scenario where you have to use barrier. 2. How did you implement barrier for the current problem?
This IT Assignment has been solved by our IT experts at My Uni Paper. Our Assignment Writing Experts are efficient to provide a fresh solution to this question. We are serving more than 10000+Students in Australia, UK & US by helping them to score HD in their academics. Our Experts are well trained to follow all marking rubrics & referencing style.
Be it a used or new solution, the quality of the work submitted by our assignment experts remains unhampered. You may continue to expect the same or even better quality with the used and new assignment solution files respectively. There’s one thing to be noticed that you could choose one between the two and acquire an HD either way. You could choose a new assignment solution file to get yourself an exclusive, plagiarism (with free Turnitin file), expert quality assignment or order an old solution file that was considered worthy of the highest distinction.
© Copyright 2026 My Uni Papers – Student Hustle Made Hassle Free. All rights reserved.