pthreads

This series of labs is built around the POSIX threads (pthreads) programming model. The pthreads package allows you to run different tasks concurrently within a process, lock access to shared data during critical sections, and communicate between threads using shared data.

A comprehensive guide to programming with pthreads.

Pthread interfaces are somewhat cumbersome to use. You can (optionally) take a look at a simple scoped lock implementation in rpc/slock.h. Check the RPC library code to understand how it is used. You are not supposed to use the Boost C++ library for this lab (Boost provides various wrapper classes for pthread primitives).

C++ Standard Template Library (STL)

We recommend you use the Standard Template Library, a collection of C++ classes implementing many common data structures and algorithms. You will find the classes std::string, std::map and std::vector particularly useful during these labs.

Debugging

printf statements are always your friend when debugging any kind of problem in your programs. However, when programming in C/C++, you should always be familiar with gdb, the GNU debugger. You may find this gdb reference useful. Below introduces a few gdb tips for complete newbies:

If your program is crashing (segmentation fault), type gdb program core where program is the name of the binary executable to examine the core file. If you don’t find the core file anywhere, type ulimited -c unlimted before starting your program again. Once inside gdb, type bt to examine the stack trace when the segmentation fault happened.

While your programming is running, you can attach gdb to it by typing gdb program 1234. Again, program is the name of the binary executable. 1234 is the process number of your running program. Of course, you can choose to run your program with gdb from the beginning. If so, simply type gdb program. Then at the gdb prompt, type run.

While in gdb, you can set breakpoints (use gdb command b) to stop the execution at specific points, examine variable contents (use gdb command p), etc.

To apply a given gdb command to all threads in your program, prepend thread apply all to your command. For example, thread apply all bt dumps the backtrace for all threads.

UNIX network programming:

W. Richard Stevens’ books “UNIX Network Programming” Volume 1 and 2 are classic references for network programming. If you are struggling with the sockets interface it could be a helpful purchase. See the suggested books list for other helpful references.

Coding style guidelines:

We strongly recommend that you look through a coding style guide, such as this one from Google. We will downgrade students with very messy coding styles up to 20% of their per-lab grade (see our programming policies)!