Threads: 1 Nptlib Interfaces
Threads: 1 Nptlib Interfaces
1 NPTlib interfaces
NPTlib provides four interfaces:
2 thread create
An application thread can create more threads using thread create library.
thread create takes the target function (that is going to execute in the new
thread) of type func t and a pointer of type void* as arguments. The pointer
is passed to the target function when it is scheduled. func t is the type of
a function that accepts a pointer of type void* and returns void. A thread
that is created using thread create can also create more threads by calling
thread create. The target function of thread create never returns and always
calls thread exit to terminate itself. struct thread represents a thread in the
scheduler list and also used to save the stack pointer during the context switch.
thread create allocates a struct thread structure for the target thread and
adds to the end of the ready list of the scheduler. ready list is a list
maintained by the scheduler that contains all the threads that need CPU.
thread create also allocates stack for the target thread and saves the stack
pointer in the newly created struct thread. thread create sets up the stack
1
in such a way that after returning from context switch, the new thread jumps
to the start of the target routine, where the first argument on the stack is the
pointer passed to create thread. You can use malloc to allocate stack for the
new thread. All the threads created using thread create have a fixed size stack
of 4096 bytes.
3 thread yield
A thread can voluntarily yield the CPU using thread yield. thread yield
puts the current thread to the end of the ready list and schedules the thread
that is next in the FIFO order.
4 thread exit
The thread exit routine terminates the current thread (by making sure that
it won’t be added to the ready list) and schedules a new thread.
6 schedule1
The schedule1 routine adds the current thread (cur thread) to ready list
and calls schedule. You have to be careful when cur thread is NULL.
7 schedule
schedule implements FIFO scheduling. schedule pops a thread that is next
in the FIFO order from the ready list and calls the context switch routine.
8 context switch
The implementation of context switch is provided in context.s file. You
are not supposed to change this implementation. It takes pointers to struct
thread corresponding to previous and next threads. The switching logic has
already been discussed in the class.
9 push back
push back routine pushes the input thread to the end of the ready list. This
can be used to implement the scheduler logic.
2
10 pop front
pop front routine pops the first element from the ready list and returns it to
the caller. This can be used to implement the scheduler logic.
11 Implementation
You have to implement everything in thread.c. You are not allowed to change
the struct thread. For this assignment, you might not need to add any extra
routines apart from the ones that are provided in the skeleton, but please feel
free to add new routines if you need them. You have to make sure that all
updates to ready list happen in push back and pop front APIs.
12 Environment
For this assignment, you need to clone the assignment repo from https://
github.com/Systems-IIITD/NPTlib.
Install gcc-multilib using, sudo apt-get install gcc-multilib.
NPTlib contains a test case app.c, the library (thread.c), and context switch
logic (context.s). You are to implement all APIs in thread.c as discussed
before. You are not supposed to change the struct thread. struct thread
also serves as a node in the ready list. ‘‘make’’ command builds the test
case and the NPTlib library. To run the test case, run ‘‘make run’’. The
output of ‘‘make run’’ should be:
./app
starting main thread: num_threads: 3
thread: 0 running iteration:0
thread: 1 running iteration:1
thread: 2 running iteration:2
thread: 0 running iteration:1
thread: 1 running iteration:2
thread: 2 running iteration:3
thread: 0 running iteration:2
thread: 1 running iteration:3
thread: 0 running iteration:3
main thread exiting: counter:267
3
• Paste your code corresponding to push back.
• Paste your code corresponding to pop front.
• Paste your code corresponding to create thread. If you are calling func-
tions that are defined by you in create thread, paste the code of them
as well.
• Dump the output of the ‘‘make test’’.
• Suggest a strategy to free struct thread and the stack corresponding
to the thread that has exited (using thread exit API). You don’t need to
implement this logic.