Folien

Transcrição

Folien
Übung „Betriebssysteme“
2011-10-27
Übungsabgabe/-bewertung
https://maserati.mi.fu-berlin.de/
redmine/projects/betriebssysteme2011
●
mit Institutsaccount einloggen
●
Account-Daten (Name, Email) angeben
●
bei obiger URL auf „Join Project“ klicken
Übungsabgabe/-bewertung
●
●
●
●
Im Reiter „Coursework“ (o.ä.) sind die aktuellen
Übungen/Projekte sichtbar (ab spätestens
Dienstag)
Es können Gruppen angelegt werden und
Dateien (Antworten/Lösungen) während des
Abgabezeitraumes hochgeladen werden.
Nach Ende Abgabezeitraum keine Änderungen
mehr möglich! (Harte Deadline)
Punkte und evtl. Bemerkungen dann an gleicher
Stelle einsehbar.
Projekte
5 Projekte:
●
0. Projekt → C auffrischen
●
1. Projekt → Threads (bis November)
●
2. Projekt → User Programs (bis Dezember)
●
3. Projekt → Virtual Memory (bis Januar)
●
4. Projekt → File Systems (bis Februar)
Pintos
●
Projekte 1-4 mit Pintos
Pintos is a simple instructional operating system
framework for the 80x86 architecture. The
software supports kernel threads, loading and
running user programs, and a file system, but it
implements all of these in a very simple way.
(Wikipedia)
http://www.stanford.edu/class/cs140/
projects/pintos/pintos.html
Pintos Setup
●
als Voraussetzung für alle Projekte
●
Download pintos.tar.gz
●
Extrahiere pintos.tar.gz
●
Installiere Emulator
●
qemu
●
bochs
Bochs 2.2.6
●
●
●
Download bochs-2.2.6.tar.gz
in pintos/:
mkdir bochs
export PINTOSDIR=`pwd`
export SRCDIR=$USER/Downloads
export DSTDIR=$PINTOSDIR/bochs
cd src/misc && sh bochs-2.2.6build.sh
kompiliert nicht durch :-(
Bochs 2.4.6
●
sudo apt-get install bochs-x
Qemu (unoffiziell)
●
sudo apt-get install qemu
●
Ändere src/utils/pintos
●
●
Zeile 103 bochs durch qemu ersetzen
●
Zeile 622 auskommentieren (-no-kqemu)
Im jeweiligen Projekt in der Make.vars
SIMULATOR = --qemu
setzen
Pintos Debug-Tools
●
cd src/utils
●
make
●
gcc setitimer-helper.o -lm -o setitimer-helper
●
make
→ Binaries in PATH kopieren oder
in PATH aufnehmen
Pintos testen
cd $PINTOSDIR
cd src/threads
make
cd build
../../utils/pintos run alarm-multiple
Coding Conventions
●
siehe Anhang C der Pintos-Dokumentation und GNU
style (dort verlinkt)
int
lots_of_args (int an_integer, long a_long, short a_short,
double a_double, float a_float)
{
if (x < foo (y, z))
haha = bar[4] + 5;
else
{
while (z)
{
haha += foo (z, z);
z--;
}
return ++x + bar ();
}
return 0;
}
Coding Rules
●
Sauberkeit
●
Verständlichkeit
→ Einhaltung der Coding Conventions
→ Kommentare (sinnvolle!)
Coding Rules
●
if (1 == var) …
●
{ … } bei if und Schleifen immer benutzen
●
Casts explizit aufrufen
●
vernünftige Variablennamen (int i,j,k,n,q,x,y;)
●
vernünftige Funktionsnamen
→ bestehender Pintos-Code kann als
Vorbild dienen!
Coding Rules
●
für ein neues malloc sofort free einfügen
●
ditto für open/close
●
aufpassen, wenn Funktionen Pointer
zurückgeben (wer ist für Freigabe zuständig?)
Compiler
-g -ggdb3
-O2
-Wall -Wextra -Werror
-std=c99 -pedantic
-D_POSIX_C_SOURCE=200809L
Projekt 0
●
Abgabe bis Mittwoch 8:10 Uhr (online/digital!)
●
Quellcode als zip-Datei (inkl. Oberverzeichnis)
Projekt 0 - Beispielablauf
# ./agg tiere.dat
What is the animal you want to start with? Krokodil
Is it a "Krokodil"?
n
What is the animal you are thinking of? Kolibri
Please enter a yes/no question to distinguish between a Kolibri and a Krokodil: Kann es fliegen?
Kann es fliegen?
s
Datei wird gespeichert
j
Is it a "Kolibri"?
j
Kann es fliegen??
n
Is it a "Krokodil"?
n
What is the animal you are thinking of? Pinguin
Please enter a yes/no question to distinguish between a Krokodil and a Pinguin: ...
Projekt 0 – Beispielablauf (cntd)
# ./agg tiere.dat
Datei existiert und wird geladen
Kann es fliegen?
j
Is it a "Kolibri"?
j
Kann es fliegen??
n
Is it a "Krokodil"?
n
What is the animal you are thinking of? Pinguin
Please enter a yes/no question to distinguish between a Krokodil and a
Pinguin: ...
Projekt 0
Fragen?
Fun with C
"C is quirky, flawed, and an enormous success."
— Dennis M. Ritchie.
Beispielfehler
Nachfolgend ein paar falsche C-Programme
Standardfehler:
●
int x[3], i; for (i=0; i<=3; i++) { … }
●
if (i=2) { … }
●
Switch-Code ohne break
White Noise
#include <stdio.h>
#include <stdlib.h>
main()
C != Python
{
int i;
for (i=0; i<10; i=i+1);
printf("i is %d\n",i);
}
i is 10
Flower Power
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
const char *flowers[] = {
5
"rose", "tulip", "daisy"
6
"petunia", "orchid", "lily"
7
};
Kein Komma → „daisypetunia“
8
9
int main() {
10
int i;
11
int choice;
12
13
for( i = 0; i < 25; i++ ) {
14
choice = rand() % 6;
15
printf( "%s\n", flowers[choice] );
16
}
17
return 0;
18 }
Programm stürzt ab – warum?
April → lirpA?
1
#include <stdio.h>
2
#include <string.h>
3
4
char April[] = "April";
5
void swap( char *p, char *q )
6
{ char t = *p; *p = *q; *q = t; }
7
void reverse( char *s, size_t len ) {
8
char *top, *bot;
9
for( bot = s, top = s+len-1;
10
bot < top, *bot;
11
bot++, top-- )
12
swap( bot, top );
13 }
14 int main() {
15
reverse( April, strlen(April) );
16
printf( "%s\n", April );
17
return 0;
18 }
Ausgabe ist „April“ … ?!
Baseball Season
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#define N 5
5
const char * const names[N] =
6
{ "Red Sox", "Yankees", "Giants",
7
"Rangers", "Phillies" /* etc. */ };
8
void find_name( char *x ) {
9
x = (char *)malloc(100);
10
if( !x ) exit(1);
11
strcpy( x, names[ rand() % N ] );
Übergibt den Wert des Pointers – ein
Assignment ändert dann die LOKALE
Variable x
12 }
13 int main() {
14
char *x;
15
find_name(x);
16
printf( "%s\n", x );
17
free(x);
18
return 0;
19 }
Segmentation fault – warum?
Prost!
1
#include <stdio.h>
2
3
enum Spirits { rum, gin, scotch };
4
enum Mixer { coke, vermouth, water, tonic };
5
6
bool ok( Spirits s, Mixer m ) {
7
if( s == rum && m != coke ) return false;
8
if( s == gin && (m != vermouth ||
9
m != tonic) ) return false;
10
if( s == scotch && m != water ) return false;
11
return true;
12
}
13
14 int main() {
15
if( !ok( rum, coke ) ||
16
!ok( gin, vermouth ) ||
17
!ok( scotch, water ) )
18
printf( "The drink checker is broken\n" );
19
20 }
return 0;
m != 1 || m != 3 immer wahr
Voting
1
#include <stdio.h>
2
#include <string.h>
3
const char *presidents[] = {
4
"johnson 61.1%",
5
"f. roosevelt 60.8%",
6
"nixon 60.7%",
7
"harding 60.3%", //...
8
};
9
void add( char *list, int pos ) {
10
strcat( list, presidents[pos-1] );
11
strcat( list, " " );
12
}
Buf → „johnson 61.1% f. Roosevelt 60.8% nixon...
13 int main() {
14
char buf[ 300 ] = { 0 };
15
add( buf, 1 ); add( buf, 2 ); add( buf, 3 );
16
printf( "Top 3 vote percentages were:\n" );
17
printf( buf );
18
return 0;
19
}
Segmentation fault – warum?
Am Abgrund
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
if (x > y) {
if (x > 8)
x++;
else
x--;
}
printf("x = %d\n", x);
return 0;
}
Weather report
1
#include <stdio.h>
2
3
const int march[31] = {
4
8, 5, 7, 2, -4, -14, -7, -4, -2, 0,
5
0, 2, 5, 7, 2, -4, -14, -7, -4, -2,
6
1, 7, 2, 2, -2, -3, -4, 6, -4, 3, 9 };
7
8
int main() {
9
unsigned i, count = 31;
10
int sum = 0;
signed/unsigned → unsigned!
(vgl. K&R, A6.5)
11
12
for( i = 0; i < count; i++ ) {
13
sum += march[ i ];
14
}
15
printf( "The average low temperature in March was"
16
17
18 }
" %d degrees\n", sum / count );
return 0;
Es wird viel zu heiß ...
++
#include <stdio.h>
int main() {
int x = 7;
int *p = &x;
*p++ => *(p++)
*p++;
printf("Result = %d\n", x);
}
Result = 7 ??
???
int main() {
int a = 1, b = 2, x;
x = a+++b;
printf("x = %d\n“, x);
}
x = ___