Some Elements of an Operating System
Lecture P2: Interacting with the OS
Files. ■
Abstraction for storage (disks, DVD).
■
File manipulation commands.
Processes. ■
Abstraction for processor (CPU).
■
Launching an application = initiating a process.
Interactions. ■
Between user and machine.
■
Among network of machines.
■
Between files and processes.
■
I/O redirection and pipes.
2
User Interface
Files
Point and click. ■
■
/
File.
User launches applications by clicking. – Compile → Project → hello.c
■
■
Restricted to pre-packaged menu options. ■
Sequence of bits.
■
■
aaclarke
User types commands at terminal. % gcc hello.c
lib
etc
u
Extended for things beyond disks.
Command line. ■
bin
A simple and powerful abstraction for permanent storage (disks).
cs126
zrnye
Directory. ■
Easily customizable. % gcc126 hello.c
Sequence of files (and other directories). files
submit
Filename.
Extends to complex command sequences.
■
Sequence of directory names on the path stock from "/" to the file.
mandel
See "In the Beginning was the Command Line" by Neal Stephenson. ■
http://www.spack.org/words/commandline.html
Implements folder abstraction.
mand32.txt
/u/cs126/files/mandel/mand32.txt C:\cs126\files\mandel\mand32.txt 3
4
Processes
Process Interconnection Abstractions
Process.
Standard input, standard output.
■
An abstraction for the processor (CPU).
■
Launching an application = initiating a process.
■
Abstract files for command interfaces.
Redirection: Multitasking. ■
■
■
Illusion of multiple machines for your use. – abstraction provided by operating system – outgrowth of 1960s "time-sharing"
■
Use it by opening one window for each application. – browser, editor, terminal window
■
random > saveanswer
Standard output to file. – File → Save As Standard input from file. – File → Open – drag-and-drop Standard input from file, standard output to file.
3
average < saveanswer
sort < myfile > myfilesorted
4
Piping: ■
Connect standard output of one command to standard input of the next. – not easily expressed with point-andclick interface
random | average
5
5
6
I/O Redirection and Pipes
Triangle Game Game played on equilateral triangle, with vertices R, G, B. ■
■
Start at R. Repeat the following: – pick a random vertex – move halfway between current point and vertex – draw a "dot" in color of vertex
B
2 5 1
What picture emerges?
3 6 4
0
R
7
G
8
Triangle Game: Boring Text Output
Turtle Graphics
triangle.c ANSI C does not directly support graphical output. #include #define N 50000 #define SQRT3 1.732050808 int randomInteger(int n) { ... }
■
■
int main(void) { int i, r; double x = 0.0, y = 0.0, x0, y0;
Need help from OS. In this course we use "turtle graphics language." – render with PostScript, OpenGL, or Java
Turtle graphics. ■
for (i = 0; i < N; i++) { r = randomInteger(3); if (r == 0) { x0 = 0.0; y0 = 0.0; } else if (r == 1) { x0 = 512.0; y0 = 0.0; } else if (r == 2) { x0 = 256.0; y0 = 256 * SQRT3; } x = (x0 + x) / 2.0; y = (y0 + y) / 2.0; printf("%f %f\n", x, y); } return 0; }
You command turtle to move, turn, and draw using relative coordinates.
D R D R D R
512 120 512 120 512 120
(512, 0) 10
triangle.c #include #include #define N 50000 #define SQRT3 1.732050808 int randomInteger(int n) { ... }
You also command turtle to fly to absolute coordinates and and drop colored spots below.
int main(void) { int i, r; double x = 0.0, y = 0.0, x0, y0; printf("F 0 0\n"); printf("D 512 R 120 D 512 R 120 D 512 R 120\n");
Turtle Graphics Commands 200 60 400 1 0 80 200 100 90 50 0 0 90 100
(0, 0)
Triangle Game: Turtle Graphics Output
Flying turtle graphics.
F S F C S F D R D C R D
// Walk 512 units, pen down // Turn 120° counterclockwise
9
Flying Turtle Graphics ■
(256, 256 √3)
Turtle Graphics Commands (relative)
100 100 0 400
// // // // // // // //
Fly to (200, 100) Leave spot of size 60 Fly to (400, 100) Change pen color to red Leave spot of size 80 Fly to (200, 400) Walk 100 units, pen down Fly to (400, 100)
(300, 450)
for (i = 0; i < N; i++) { r = randomInteger(3); if (r == 0) { x0 = 0.0; y0 = 0.0; } else if (r == 1) { x0 = 512.0; y0 = 0.0; } else if (r == 2) { x0 = 256.0; y0 = 256 * SQRT3; } x = (x0 + x) / 2.0; y = (y0 + y) / 2.0;
y (200, 400)
printf("F %f %f\n", x, y); printf("S 5\n");
1 (0, 0)
draw triangle
draw dot
} return 0;
x
} 11
12
Triangle Game: Turtle Graphics Output Text output still boring!
Triangle Game Game played on equilateral triangle, with vertices R, G, B.
Command Line % F D D D F S F S F S F S F S
echo 5 1.5 0 0 512 R 120 512 R 120 512 R 120 128.000000 1.500000 320.000000 1.500000 160.000000 1.500000 336.000000 1.500000 424.000000 1.500000
■
| triangle
■
110.851252 55.425626
Start at R. Repeat the following: – pick a random vertex – move halfway between current point and vertex – draw a "dot" in color of vertex
B
2 5
27.712813 1
13.856406
What picture emerges?
6.928203
3 6 4
Write C program turtle.c to translate text into PostScript; render PostScript with ghostscript. ■
Three pipes.
■
See Assignment 2.
0
R
G
Command Line % echo 5 1.5 | triangle | turtle | gs 13
14
Turtle Graphics in PostScript
Turtle Graphics in PostScript
turtle.c (includes and helper functions)
turtle.c (main skeleton)
#include #include #define PI 3.1415926535897932385 // PostScript header void header(void) { printf("%%!PS-Adobe-3.0 EPSF-3.0\n"); printf("%%%%BoundingBox: 0 0 512 512\n"); } // PostScript footer void footer(void) { printf("showpage\n"); }
int main(void) { char command; double d; double a; double r, g, b; I’m a PostScript program.
double x = 0.0; double y = 0.0; double alpha = 0.0;
// // // //
input turtle command distance change in angle new RGB colors
// x coordinate of turtle // y coordinate of turtle // turtle orientation
header(); Print me out. // READ IN TURTLE GRAPHICS COMMANDS AND PROCESS footer(); return 0; } 17
18
Turtle Graphics in PostScript
Turtle Graphics: Relative Coordinates Relative coordinates.
turtle.c (absolute coordinates)
■
// read in turtle graphics commands and process while (scanf(" %c", &command) != EOF) { switch(command) {
■
■
■
// change color to r, g, b case ’C’: scanf("%lf %lf %lf", &r, &g, &b); printf("%f %f %f setrgbcolor\n", r, g, b); break;
Current location = (x, y). Current direction = α. Turtle moves d units. New location = (x’, y’). ! x’ = x + d cos α. ! y’ = y + d sin α.
(x’, y’)
// fly to location (x, y) case ’F’: scanf("%lf %lf", &x, &y); break;
d
// draw spot of size d case ’S’: scanf("%lf", &d); printf("%f %f %f %f rectfill\n", x – d/2, y – d/2, d, d); break;
α (x, y)
19
Turtle Graphics in PostScript turtle.c (relative coordinates) // rotate case ’R’: scanf("%lf", &a); alpha += a; break; // move d units in current direction, pen down case ’D’: scanf("%lf", &d); printf("%f %f moveto\n", x, y); x += d * cos((PI/180.0) * alpha); y += d * sin((PI/180.0) * alpha); printf("%f %f lineto\n", x, y); printf("stroke\n"); break; } }
// end switch // end while
21
20