csi500/400 starts: c language programming for os students

29
CSI500/400 Starts: C Language Programming for OS Students Prof. Seth Chaiken January 11, 2010

Upload: others

Post on 16-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSI500/400 Starts: C Language Programming for OS Students

CSI500/400 Starts: C Language Programmingfor OS Students

Prof. Seth Chaiken

January 11, 2010

Page 2: CSI500/400 Starts: C Language Programming for OS Students

About This Course

IntroductionWhat is an OS?The C Programming Abstraction

Statements, Functions and DeclarationsStatementsFunctionsDeclarations and Definitions

Types

The Memory Model for C

Page 3: CSI500/400 Starts: C Language Programming for OS Students

Catalog Descriptions

Graduate4 cr. Intro. to OS. Topics incl. processes, concurrency,synchronization, deadlock, memory management, segmentation,paging, replacement policies, caching, interprocess communication,file systems, and protection. Heavy emphasis on abstractions,mechanisms, policies, and design. Prereq: Csi 333, 310, Mat 367,& 1 of Csi 400, 402, or 404.

Undergraduate

3 cr. Historical overview; OS services; mass storage fileorganization; memory management in multiprogrammed systems;virtual memory; resource alloc.; concurrent processes; deadlockdetection and prevention; security; the design of contemporary OSsuch as UNIX. Prereq: Csi 333.

Page 4: CSI500/400 Starts: C Language Programming for OS Students

Top Level Objectives: Demonstrated Abilities

1. Explain what a complex software/hardware system doesduring normal and abnormal operational scenarios, and “drilldown” to details of those subsystems that were studied.Which one? Modern general purpose computer operatingenvironment, emphasis on Linux/Unix.

2. Do intermediate (1st yr. grad., 4th sem. undergrad. semester)complexity C and some i386 Asm. projects that utilize and/orsimulate technologies listed in the course descriptions.

3. Solve problems: Analyze scenarios for each topic, includingidentification and compared consequences of alternativechoices. Utilize numbers, graphs, formulas, sequencediagrams, etc., together with accurate logical reasoning.

4. Install Linux systems, build Linux kernels and kernel modules,find and annotate code in Linux sources for given OSfunctions, run, debug and test kernel modifications andmodules. Report OS behavior using system tools andinterfaces.

Page 5: CSI500/400 Starts: C Language Programming for OS Students

Interface between Abstractions and Resources

Hardware

SoftwareUser mode

Kernel mode Operating system

Web browser

E-mail reader

Music player

User interface program

1. OS is software between hardware and application software.

2. Hardware plus OS implements the process abstraction.

3. OS performs coordination and allocation of resources.

Page 6: CSI500/400 Starts: C Language Programming for OS Students

Abstraction over Reality: Examples(Derived from M. Seltzer, Harvard CS161, 2009)

Abstract Files

I open, close, read, write: System call functions refer to a fileby a number (handle).

I hierarchical name space.

I (byte) addressable array.

Real Disks

I 512 byte blocks, numbered sequentially or by Cylinder, Head,Sector number.

I bad blocks.

I same disk, many files.

I motor to be turned on and off.

I varying times to access data.

Page 7: CSI500/400 Starts: C Language Programming for OS Students

Abstraction over Reality: Examples

(Derived from M. Seltzer, Harvard CS161, 2009)

Running Programs (Processes)

I Modern systems can ran as many programs as you want.

I Each may have many network connections (open “sockets”).

Fixed amount of hardware.

I Fixed number of processors or cores.

I Fixed number of registers.

I One or a few network cards (hardware interfaces to wire, fiberoptics or wireless.)

Page 8: CSI500/400 Starts: C Language Programming for OS Students

Abstraction over Reality: Examples

(Derived from M. Seltzer, Harvard CS161, 2009)

Virtual Memory

I Each program “believes” it has 4GB of memory.

I Each programs memory starts at address 0.

I Huge programs with huge amounts in data can be run.

Real, i.e., Physical Memory

I Limited amount installed.

I Large address ranges refer to hardware devices connected viasystem busses.

I Addresses not used by RAM or busses are illegal.

I Space on disks is used instead of RAM for huge programs.

Page 9: CSI500/400 Starts: C Language Programming for OS Students

Abstraction for Programmers—Behold BeautyThe beautiful interface is

I CPU(s) (Instruction Set Architecture from real hardware)limited to user-mode instructions (including traps for systemcalls)

I plus a Virtual Memory, containing program instructions anddata.

It’s most beautiful seen in C.

Operating system

Hardware

Ugly interface

Beautiful interface

Application programs

Page 10: CSI500/400 Starts: C Language Programming for OS Students

Benefits of Virtualization

Virtual CPU

I Real CPU is timeshared; it’s shared among many runningprograms at once.

I Ugly instructions (and other interfaces) to control memoryand hardware are hidden.

I Code to use ugly interfaces (1) resides in the OS kernel and(2) is invoked ultimately by traps for system calls.

Virtual Memory

I The virtual memories used by different programs arecompletely separate (usually). Provides protection.

I Memory addresses used by one program are independent ofaddresses used by another program.

I The OS manages paging data between RAM and disk whenRAM is too small.

Page 11: CSI500/400 Starts: C Language Programming for OS Students

Introduction to the Process

The MOST IMPORTANT ABSTRACTION for OSA PROCESS is (1) One Virtual Memory together with (2) One (ormore for multithreading) Virtual CPU(s) that together areCURRENTLY implementing ONE instance of a program inexecution and (3) some allocated resources (files, networkconnections, ... )

In other words, a process is

One virtual machine that is currently holding one running instanceof one program.

Today’s systems (all but the smallest) provide multiprogramming.

Multi-...Multiprogramming is many processes in a system at once.Multiprocessing is multiprogramming with multiple CPUs so severalprocesses can run truly in parallel, i.e., actually at the same time.

Page 12: CSI500/400 Starts: C Language Programming for OS Students

i386 Basic Execution Environment

Page 13: CSI500/400 Starts: C Language Programming for OS Students

Virtual Memory Contents Simplified

Running C Program’s and Other Processes’ Abstraction ofMemory

Page 14: CSI500/400 Starts: C Language Programming for OS Students

To build and run a C application

1. Write or edit the source file with a text editor, and name itwith the .c suffix, say ASourceFile.c

2. Compile and link using one or more commands.Example: gcc -o printArg1 ASourceFile.c

3. Run the linked executable file from the command lineExample: ./printArg1 TheFirstArgString

OS students use the shell’s command line interfaceThe shell is a program that prompts for and accepts typewrittencommands. When an executable file’s name is given as acommand, the shell runs (or executes) that file.We will SOON learn much more about this.

Where a process comes from

The shell (1) commands the OS to create a new process (child)and (2) makes the new process run the printArg1 program.Meanwhile, the shell waits for the child process to exit.

Page 15: CSI500/400 Starts: C Language Programming for OS Students

OS students break out gcc steps

Required for current assignments

1. gcc -S ASourceFile.c Compiles into Assembly Language.

2. gcc -c ASourceFile.s Assembles into an Object File.

3. gcc --static -o printArg1 ASourceFile.o Links(statically) the object file with library objects and writes outexecutable file named printArg1

Assignments include analyzing assembly langauge files likeASourceFile.sNOTE DOWN these commands for future use (soon).

Page 16: CSI500/400 Starts: C Language Programming for OS Students

A C Function

#include <unistd.h>static int strlen(char *pch) {int len = 0;while( *pch != 0 ) {len++;pch++;

}return len;

}int main( int argc, char* argv[]) {if( argc != 2 ) {write(2,"Wrong num. Args\n", 17);return 1;

}else {write(2, argv[1], strlen(argv[1]));write(2, "\n", 1);return 0;

}}

Page 17: CSI500/400 Starts: C Language Programming for OS Students

Elements Illustrated

1. A .c source file.

2. Consistent indentation!

3. Include files.

4. Functions.

5. Static Function.

6. Parameter variables.

7. C strings.

8. “Local” variables.

9. While loops.

10. Pointer variables.

11. Dereferencing Operator.

12. Increment an int variable.

13. Increment a pointer variable.

14. Test when pointer addressesthe end of a C string.

15. Return.

16. The main function.

17. Pointer to an array of Cstrings.

18. Command line argumentparameters of main().

19. The write system call.

20. File descriptor (2) for std.output.

21. Literal C strings.

22. Value returned by main() tothe shell.

Page 18: CSI500/400 Starts: C Language Programming for OS Students

The 3 storage classes of C

“Local Lifetime”Local variables and parameters reside in the stack frame createdwhen the function is called.Those variables go away when the function returns.Examples: fun(int LV1){ int LV2; int LV3; ... }

“Static Lifetime”Reside in the “bss” and “data” sections of program’s virtualmemory.They exist throughout the entire lifetime of the process.Ways to declare:

I OUTSIDE of any function body.

I static qualifier inside a function or block.

I C string literals "Like This"

Page 19: CSI500/400 Starts: C Language Programming for OS Students

The 3 storage class of C

Local, Static, and (3)

“Dynamic Lifetime”

Analogous to objects allocated by new in Java and C++. A“heap” area is used.C applications call p=malloc(no. bytes requested) to allocatememory and free(p) to recycle it.Kernals have their own dynamic memory allocation and freeingfunctions.

Page 20: CSI500/400 Starts: C Language Programming for OS Students

Storage Lifetimes in Assembly Language

Static

Page 21: CSI500/400 Starts: C Language Programming for OS Students

Example

int AStaticVar;int ASInited = 14;

char *fun(){ return

"A Lit Str";}

Compiled withgcc -S Data.c

.file "Data.c"

.globl ASInited

.data

.align 4

.type ASInited,@object

.size ASInited, 4ASInited:.long 14.section .rodata.LC0:.string "A Lit Str"

.text

.globl fun

.type fun, @functionfun:pushl %ebpmovl %esp, %ebpmovl $.LC0, %eaxpopl %ebpret.size fun, .-fun.comm AStaticVar,4,4.ident "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)".section .note.GNU-stack,"",@progbits

Page 22: CSI500/400 Starts: C Language Programming for OS Students

C Elements Used

Computes length of a C-string

I Formal parameters argc, argv are a local variables.

I Value pch is the address of the first string character.

I len, temp are also local variables.

I Only one library function, write is used. It is a Unix systemcall.

I temp, declared “char *temp” is a pointer variable. *temprefers to the byte-sized variable containing the character inmemory whose address is the current value of temp.

I ( *temp != 0 ) tests if that character is “the nullcharacter”.

Page 23: CSI500/400 Starts: C Language Programming for OS Students

C Language

1. Java syntax is based on C syntax.

2. Imperative: An executable statement commands to computerto do something.

3. Data types: Interpretations for data stored as raw bits in thecomputer.What operations are legal.

4. Variables: HOLD, HAVE, STORE, RETAIN, KEEP TRACKOF the current VALUE.

5. Each variable also has a (data) type.

6. Control Statements: Chief Java/C similarity.

Page 24: CSI500/400 Starts: C Language Programming for OS Students

C Statements

For now, consider executable statements only.

1. Expression Statement: Ends with a semicolon.

2. Compound Statement:

3. Block: Delimited by matching curly-braces.

Page 25: CSI500/400 Starts: C Language Programming for OS Students

Functions

1. Synonyms: Subroutines, Procedures, Methods.

2. Defining Functions.

3. Calling Functions.

4. Return value.

Page 26: CSI500/400 Starts: C Language Programming for OS Students

Every variable must be declared

I int retlen; declares retlen to be a local variable of typeinteger.

I char *pch (in function definition header!) declares pch to beof type “pointer to character”

Page 27: CSI500/400 Starts: C Language Programming for OS Students

Types

Primativeshort int, long int, int, char, float, doublepointers

Composite: Structures

Like classes in Java, but only with data members.

Composite: Arrays

Composite: Unions

Page 28: CSI500/400 Starts: C Language Programming for OS Students

pointer in C = reference in Java

SimilaritiesBoth a C pointer and a Java reference value is an address, which(if not null or illegal) REFERS TO an object in memory.

I ..

Differences

I Numerical arithmentic (binary) CAN be done on C pointers,but CANNOT be done on Java references.

I A number (say a literal 0x1FF0000) CAN be used as a Cpointer by CANNOT be used as a Java reference.

Page 29: CSI500/400 Starts: C Language Programming for OS Students

Variable = Memory Chunk = Object