Göm menyn
IT-Programmet, Tema 1 i termin 4:

TTIT61 Processprogrammering och Operativ System

/Concurrent Programming and Operating Systems/


Lab 00: Introduction to C programming

1. Hello, world. [10 min]

Everytime you start to work with a new programming language, it is tradition to make the "Hello, world" program. Create it, compile it and run it.

2. debug this [20 min]

First try to understand the program below and spot the bug, but don't fix it. (It's ok if you can't find it.)

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

#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;
    bufi++;
    stri--;
  }

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

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

N.B. This is a silly and not very pretty program.

3. Create a linked list of integers. [50 min]

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 destroy(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;

4. Browsing the code. [10 min]

Using tags with emacs. Install pintos using the instructions from Lab0. Change to your new ${YOURPATH}/pintos/src and run:
     find . -name \*.c | xargs etags -
     find . -name \*.h | xargs etags --append -
This created an index of structs and functions in a new file named TAGS. Now let's find where the pintos semaphore is. Start emacs and press meta-. (hold meta and press dot). Type the keyword you wish to look for, "semaphore" in this case, and press enter. Press enter again to confirm the path to your TAGS file, and there you are!

Sometimes when you want to know where a file is in the hierarchy this command is useful:

     find ${YOURPATH}/pintos/src |grep filename
Try find the file list.c. Open it to see a more complicated linked list implementation than the one you just created.

Now imagine that you want to find all files where this list is used, for this you can use the following command:

     find ${YOURPATH}/pintos/src -type f -print |xargs grep list_init

Next Laboratory work

Laboratory Assignment 1 in which you will learn the basics of synchronization

TTIT61
Temamål
Temaplan
Schema
Examination
Referenslitteratur
Personal
Register for labs

Föreläsningarna
Programexempel
Forum
Labresultat

Schemaläggning
Kritiska sektioner
Processorstöd för operativsystem
Sekundärminne
UNIX, WinNT
Säkerhet

Intro: C/make
Intro: installation
Threads and synchronisation
System calls
Execution of user programs
File system

Lesson 1
Lesson 2
Lesson 3

C/C++ OH
C/C++ tutorial
C pointers tutorial
Pintos documentation
Memory Issues in Pintos
Pintos on-line documentation
The gnu DDD documentation
DDD tutorial
Debugging topics
Programing with threads

Guidelines for writine and changing source code
Pintos source code

Sidansvarig: Sergiu Rafiliu
Senast uppdaterad: 2011-09-12