Sei sulla pagina 1di 3

CoE 135 1st Sem 2018-2019 Lab 8 (10/16/2018)

Kernel Threads and Locks


Objectives:
- Make use of KThread API and relate them to PThreads
- Make use of Kernel Spinlock API to lock shared resources
- Make use of Kernel Mutex API to lock shared resources
- Implement a Concurrent Counter Data Structure

General Specifications:
You are to create a Concurrent Counter Data Structure in Kernel Space. Without any locking
mechanism, the base implementation will be the following:

typedef struct __counter_t {


int value;
} counter_t;

void init (counter_t *c) {


c->value = 0;
}

void increment (counter_t *c) {


c->value++;
}

void decrement (counter_t *c) {


c->value--;
}

int get (counter_t *c) {


return c->value;
}

Your first task is to make use of Kernel Space Spinlock API to implement a simple locking
mechanism similar to the concept of a Monitor Data Structure to turn the unsafe Counter Data
Structure into a working Concurrent Counter Data Structure. A very important note for this part
of the exercise is to use the spin_trylock() (or equivalent) version for locking to prevent
your Operating System from freezing. Save your code as lab8_spinlock_lastname.c (e.g.
lab8_spinlock_alpano.c).

Once the concurrent version of the counter is implemented, you are to create two Kernel-level
Threads using KThread API. Each kernel thread must increment the counter five hundred
thousand (500,000) times. Make sure that the global counter’s value is correct after each thread
is finished incrementing it. Take note of the runtime of this kernel module. Afterwards, rerun the
module with the exact same functionality and increase the number of kernel threads by one (so,

1
you have a modified kernel module with three kernel threads). Note the runtime again. Repeat
the process for four threads and five threads. Save your code as lab8_threads_lastname.c (e.g.
lab8_threads_alpano.c).

As you will notice, the performance of the first Concurrent Counter Data Structure poorly scales
as you increase the number of threads. Your next task is to implement a Sloppy Counter Data
Structure using Kernel Space Mutex API. Create another kernel module that implements the
same counter data structure with Mutex API, and modify your implementation by adding an
array of local counters to the counter structure, and by adding a global counter member in the
structure. The number of local counters should be the same as the number of kernel threads. The
idea behind the sloppy counter you are going to make is the following:

1) The global counter and all the local counters will be initialized to zero
2) Each kernel thread will increment their local counters first (Note: This step will not
require locking since the local counters are local to each thread)
3) After a thread increments their local counter S times (S is an integer greater than 0),
the thread will acquire the mutex lock and update the global counter
4) The local counter is then reset to zero.

Upon proper implementation of the Concurrent Sloppy Counter Data Structure, you will need to
understand the effect of modifying the value of S and scaling the number of threads to the
performance of the sloppy counter. Save your code as lab8_mutex_lastname.c (e.g.
lab8_mutex_alpano.c).

All analysis will be discussed directly with the Lab Instructor or Student Assistants during the
checking period.

Deadline: Friday 10/26/18 lab hours


 Grades are given DURING the laboratory class hours. Students who failed to let the
lecturer/student assistant check their work during class hours will be given a grade of 0.
 Codes are expected to run properly and will be checked on Ubuntu 18.04 LTS. You may
also use your own Arch Linux installed from Lab 1 exercise for checking, given that you
bring it on your checking day.
 Name your files as lab8_module_lastname (e.g. lab8_spinlock_alpano). Also, put your
name at the start of your code. Incorrect name syntax will be given a deduction of 10%.
 Submit your c files to psalpano@upd.edu.ph with the subject
CoE135_section_lab8_Lastname (e.g. CoE135_HLMTRU_lab8_Alpano) AFTER the
lecturer/student assistant checks your work. Files must be attached to the email and not as
zipped/tar.
 Compile your codes with –W –Wall. Codes with warnings (unused parameters,
incompatible data types, etc) will be given a deduction of 10%.
 You will still be given a grade of 0 if you only send via email and failed to let the
lecturer/student assistant check your work during class hours on the week of 10/16/18 to
10/26/18.

2
 I will be running your c codes through a code checker so make sure you did not copy
your code from the internet or from other students.
 Students who are caught cheating will be given a grade of 0 and will be filed a case in the
Student Disciplinary Council.

Grading System:
30% Primitive Concurrent Counter Data Structure with Spinlock API
20% Discussion and Implementation of Five-level Threads with Primitive Concurrent Counter
Data Structure with Spinlock API
30% Sloppy Concurrent Data Structure with Mutex API
20% Discussion and Implementation of Counter Performance in Sloppy Concurrent Data
Structure with Mutex API

Potrebbero piacerti anche