/Mailbox

Primary LanguageC

Mailboxes

Goal of this project is to design a simple version of mailbox system and demonstrate core OS concepts.

* Concurrency primitives e.g. mutexes and condition variables
* Using the pthreads library
* Resource management in a multithreaded environment
* Implementing a message passing system on top of a shared memory model

The code creates few threads, each of which receives a pointer to their own mailbox struct that can be used to send and receive messages along with pointing to another thread’s mailbox. They each have a pseudo randomly assigned id that ranges between 0 and 5 ∗ NUM THREADS. At first, these threads will be arranged in a ring and this ring is transformed into a sorted list of threads.

* Compatible with POSIX operating system. Requires pthreads and gcc.
* Graphs generated by 'tring' need to be viewed using GraphViz package.


In case of several messages, some short while after it is created each thread will receive two messages, ID and MAILBOX, in that order. The first contains the id of that thread, while the second contains the mailbox of the next thread in the ring.

This code also handles the PING, PRINT, and SHUTDOWN messages. In response to a PING message a thread  calls the pong function, passing its id as the argument. When a PRINT message is received the thread, in some way, cause a line to be printed to the tring.dot file (using the tring print function) that describes the edge between it and the thread pointed too by next mb. Upon the arrival of a SHUTDOWN message the thread frees all of its resources and exit.

At several points the main thread of execution pauses, pthread cond wait()ing, giving the threads time to finish certain tasks. The first of these pauses is after the call to tring protocol start, at which point it is waiting for the threads to re-arrange themselves. It pauses again after it sends a PING and a PRINT message to the thread pointed to by first mb. To wake the main thread of execution at each of these points, use the function tring signal.