Kernel Threads in the xv6 Operating System are implemented in this project. This is implemented on top of this repository. Support for threads (lightweight processes) at the kernel level is provided, and semaphore implementation serves as a primitive synchronisation mechanism. One-to-one mapping and ticket-lock synchronisation are also added to the userland threading library.
int clone(void(*function)(void *, void *), void *stack, int flags, void *arg1, void *arg2);
- Userland threading library wrapper function calls the clone system call to construct a thread of a process.
- function - Function pointer which runs in the created thread.
- flags - Flags for the clone system call.
- stack - Child stack to pass the function(fcn) arguments.
- arg1 & arg2 - Arguments to function fcn.
int join(int threadId);
- Join system call to block other threads while it waits for the current thread to finish, and then release thread resources once it has.
- threadId - thread Id of the executing thread.
int gettid();
- gettid system call to get the thread Id of currently executing thread
int tgkill(int tgid, int tid, int sig);
- tgkill system call to terminate the thread from the thread group with the thread group id and thread id as the tgid and tid, respectively.
- tgid - thread group id (parent id)
- tid - thread id to be killed
- sig - signal number to be sent
- Setting up tgid for the process.
- Setting additional fields that were added to the proc structure to implement threads
- If it's a thread and the CLONE FILE flag is set, changing one condition will result in shutting all file descriptors.
- Adding a clause that says a parent process can only be killed if all of its child threads have been killed using tgkill.
Functions implemented for userland threading library are:
- thread_create - Malloc the stack for thread and then calls clone system call.
- thread_join - Free the stack for the thread and call join system call.
- tlock_acquire - Acquires a ticket lock
- tlock_release - Reelease the ticket lock
- tlock_init - Initialize the ticket lock
Added testing code to evaluate Kernel thread functionality across a variety of test cases.