Starting from:

$30

CS5600-Homework 1 User-space threads Solved

In this homework you will write a user-space thread system (named “qthreads”, vs. the standard “pthreads” library), which creates threads, performs context switches, and includes implementations of mutexes and condition variables. You will use your code in two applications – your own test procedures, and a simple threaded webserver provided as part of the example.

 
Programming Assignment materials and resources
You will download the skeleton code for the assignment from the CCIS repository server, github.ccs.neu.edu, using the 'git clone' command:

 

git clone https://github.ccs.neu.edu/cs5600-03-sp2018/hw1

You should copy this code to a subdirectory in your github team repository under hw1. For example if you are on team 1

     git clone git@github.ccs.neu.edu/cs5600-03-sp2018/team-1      cd team-1      mkdir hw1      cp -a /path/to/hw1/* hw1 So that your team repository looks like

     ls team-1

     hw1

  

Homework 1 – User-space threads                                                                 Page 2 of 3

You should be able to complete this homework on any x86 Linux system however

Repository Contents
The repository you clone contains the following files:

qthread.c, qthread.h – these are the files you will be implementing. They have some comments in them describing the functions and types you have to implement.

switch.s, stack.c – these contains the context switch function (switch_from_to) and the stack initialization function (init_stack). You should not change these files. The functions are more complex than the the ones discussed in class because they contain protection code designed to abort into the debugger if you try to switch to an invalid stack.

test1.c – this is where your test code should go. (well, you can add additional files and update the makefile if you do) server.c – a simple multi-threaded webserver using qthreads.

Makefile – this is set up to build executables for ‘test1’ and ‘server’. Compile your code by typing ‘make’.

Deliverables
The following files from your repository will be examined, tested, and graded:

qthread.c, qthread.h test1.c

                                                                                                                         Page 3 of 3

Qthreads interface
First, two function pointer typedefs – one for a function taking two arguments (with no return value) and the other taking one argument and returning ‘void*’. These will make the following definitions easier to read.

typedef void (*f_2arg_t)(void*, void*); typedef void * (*f_1arg_t)(void*);

You will need to implement the following functions – first, the basic functions to start and exit a thread, run the thread scheduler, and wait for a thread to finish (“join”).

qthread_t qthread_start(f_2arg_t f, void *arg1, void *arg2); qthread_t qthread_create(f_1arg_t f, void *arg1);

void qthread_run(void); void qthread_exit(void *arg); void *qthread_join(qthread_t thread);

Mutex and condition variable functions:

void qthread_mutex_init(qthread_mutex_t *m); void qthread_mutex_lock(qthread_mutex_t *m); void qthread_mutex_unlock(qthread_mutex_t *m); void qthread_cond_init(qthread_cond_t *c);

void qthread_cond_wait(qthread_cond_t *c, qthread_mutex_t *m); void qthread_cond_signal(qthread_cond_t *c); qthread_cond_broadcast(qthread_cond_t *c);

And finally functions to replace some of the blocking sleep and I/O calls in the Unix interface:

void qthread_usleep(long int microsecs); ssize_t qthread_read(int fd, void *buf, size_t len); ssize_t qthread_write(int fd, void *buf, size_t len);

int qthread_accept(int sock, struct sockaddr *addr, socklen_t *len);

More products