Hide menu

Lab 0 - C pointers and Pintos setup


Lab 0 can be seen as preparation to the Pintos labs (labs 1-4). The lab consists of three parts
  1. Pointer debugging: This part is intended to make sure that you are able to understand and fix a program that uses arrays and pointers in C in a non-trivial fashion.
  2. Linked list implementation: This part is intended to make sure that you understand how lists are implemented with pointers in C (including memory (de)allocation) and also that you are able to create relevant test cases for your solutions.
  3. Pintos setup: The final part of this lab is to setup Pintos in your environment.
You need to demonstrate each part to your lab assistant to pass this lab.

Pointer debugging (part 1)

First try to understand the program below and spot the bug, and fix it.

Run the program in ddd or gdb to find the row where the segmentation fault occurs. You must first compile it with gcc -g for this to work.

Copy the follwing text and save and compile it:

#include <stdio.h>

int main(int argcchar ** argv)
  char str[] = "sihtgubed";
  char *stri = &str[8];
  char *buf[9];
  char **bufi, **bufend;
  bufi = buf;
  bufend = &buf[9];

  while (bufi != bufend){
    *bufi = stri;

  while (bufi != buf){
    *(*bufi) -= 32;

  while (bufi != bufend){
    printf("%c", **bufi);

N.B. This is a silly and not very pretty program. Hint: Check an ASCII table (for example by typing man ascii in the console).

Linked list implementation (part 2)

Create a linked list with the following functions:

  void append(struct list_item *firstint x); /* puts x at the end of the list */
  void prepend(struct list_item *firstint x); /* puts x at the beginning of the list */
  void print(struct list_item *first);  /* prints all elements in the list */
  /* input_sorted: find the first element in the list larger than x
     and input x right before that element */

  void input_sorted(struct list_item *firstint x);
  void clear(struct list_item *first); /* free everything dynamically allocated */

Hint1: You need to use malloc (and free of course) and a struct list_item that contains a pointer to the same struct:
  struct list_item {
    int value;
    struct list_item * next;

Hint2: Implementing the list will be easier if you make first be a static head element that does not contain any real data. (Because there are fewer exceptions if you know first is never NULL.) Your main can start like this:
  int mainint argcchar ** argv)
    struct list_item root;
    root.value = -1/* This value is always ignored */
    root.next = NULL;
Write a piece of code that tests you linked list implementation. You should test all the relevant cases including, creation, insertion (all different types), printing, and clearing. Try several different orders of these functions (including printing after clearing the list). Use Valgrind to make sure you have got no memory leaks. You can use the following command:

% valgrind --tool=memcheck --leak-check=yes your-program

Pintos setup (part 3)

The follwing instructions are for installing Pinots in Linux (mint).

Pintos is an instructional operating system that runs on x86 machines. You will run it in QEMU -- a computer system emulator, which runs as a user process under Linux. Pintos is written in C. The lab assignments require modification of the provided version of Pintos by adding functionality to parts of it, for instance, synchronization of threads and memory management.


Step 1: The first thing you must do is to create a repository:

       svnadmin create $HOME/svnlinuxrepository

Step 2: Import the linux Pintos files into it (have patience, this may take a few minutes):

       svn import /home/TDDB68/labs/linux/ file://$HOME/svnlinuxrepository -m "linux pintos "

Step 3: Now check out a working copy into a directory called pintos:

       svn checkout file://$HOME/svnlinuxrepository/pintos linuxpintos
       cd linuxpintos

Step 4: Run the following script to instruct svn to ignore some files


Step 5: Do not allow anyone else to read your code:

       chmod -R og-rwx $HOME/svnlinuxrepository/ $HOME/linuxpintos

Commit your code:
       cd linuxpintos/src/
       svn -m "initial commit message" commit

Step 6: go int utils

       cd utils
       ln -s $(which qemu-system-i386) qemu

Step 7:
       chmod +x pintos
       chmod +x pintos-mkdisk
       chmod +x backtrace
       chmod +x pintos-gdb

Step 8: Make the pintos commands available in your terminal using the follwoing command: (you might also want to add this to your ~/.bashrc file, you might need to create the file if it is missing.)

       export PATH="${HOME}/linuxpintos/src/utils/:${PATH}/"


To compile Pintos for the first time, change your current directory to the threads directory (cd pintos/src/threads) and issue the command:


Go down to the build directory and start Pintos to see that it runs:

        cd build
        pintos --qemu -- run alarm-multiple
You should get a new window with an output that ends with something similar to the following (show this to your lab assistant):

eriha@parlomba1:build$ pintos --qemu -- run alarm-multiple
Writing command line to /tmp/EAhN6wTYgK.dsk...
qemu -hda /tmp/EAhN6wTYgK.dsk -m 4 -net none -serial stdio
Kernel command line: run alarm-multiple
Pintos booting with 4,088 kB RAM...
371 pages available in kernel pool.
371 pages available in user pool.
Calibrating timer...  314,163,200 loops/s.
Boot complete.
Executing 'alarm-multiple':
(alarm-multiple) begin
(alarm-multiple) Creating 5 threads to sleep 7 times each.
(alarm-multiple) Thread 0 sleeps 10 ticks each time,
(alarm-multiple) thread 1 sleeps 20 ticks each time, and so on.
(alarm-multiple) If successful, product of iteration count and
(alarm-multiple) sleep duration will appear in nondescending order.
(alarm-multiple) thread 0: duration=10, iteration=1, product=10
(alarm-multiple) thread 0: duration=10, iteration=2, product=20
(alarm-multiple) thread 1: duration=20, iteration=1, product=20
(alarm-multiple) thread 2: duration=30, iteration=1, product=30
(alarm-multiple) thread 0: duration=10, iteration=3, product=30
(alarm-multiple) thread 0: duration=10, iteration=4, product=40
(alarm-multiple) thread 1: duration=20, iteration=2, product=40
(alarm-multiple) thread 3: duration=40, iteration=1, product=40
(alarm-multiple) thread 4: duration=50, iteration=1, product=50
(alarm-multiple) thread 0: duration=10, iteration=5, product=50
(alarm-multiple) thread 0: duration=10, iteration=6, product=60
(alarm-multiple) thread 1: duration=20, iteration=3, product=60
(alarm-multiple) thread 2: duration=30, iteration=2, product=60
(alarm-multiple) thread 0: duration=10, iteration=7, product=70
(alarm-multiple) thread 3: duration=40, iteration=2, product=80
(alarm-multiple) thread 1: duration=20, iteration=4, product=80
(alarm-multiple) thread 2: duration=30, iteration=3, product=90
(alarm-multiple) thread 4: duration=50, iteration=2, product=100
(alarm-multiple) thread 1: duration=20, iteration=5, product=100
(alarm-multiple) thread 1: duration=20, iteration=6, product=120
(alarm-multiple) thread 2: duration=30, iteration=4, product=120
(alarm-multiple) thread 3: duration=40, iteration=3, product=120
(alarm-multiple) thread 1: duration=20, iteration=7, product=140
(alarm-multiple) thread 2: duration=30, iteration=5, product=150
(alarm-multiple) thread 4: duration=50, iteration=3, product=150
(alarm-multiple) thread 3: duration=40, iteration=4, product=160
(alarm-multiple) thread 2: duration=30, iteration=6, product=180
(alarm-multiple) thread 4: duration=50, iteration=4, product=200
(alarm-multiple) thread 3: duration=40, iteration=5, product=200
(alarm-multiple) thread 2: duration=30, iteration=7, product=210
(alarm-multiple) thread 3: duration=40, iteration=6, product=240
(alarm-multiple) thread 4: duration=50, iteration=5, product=250
(alarm-multiple) thread 3: duration=40, iteration=7, product=280
(alarm-multiple) thread 4: duration=50, iteration=6, product=300
(alarm-multiple) thread 4: duration=50, iteration=7, product=350
(alarm-multiple) end
Execution of 'alarm-multiple' complete.

Page responsible: Mikael Asplund
Last updated: 2018-01-12