CS代考 CSCI 4061 Introduction to Operating Systems

CSCI 4061 Introduction to Operating Systems
Instructor:
Programs
 C Program Structure
 Libraries and header files
 Compiling and building programs  Executing and debugging
 Assume familiarity with C programming
2
C Refresher
 Set of variables, types and functions  Variables:
 Global: Visible in all functions
 Local: Visible within a single function
 Static: Visible to all functions within a file
 Types: structs, unions, typedefs  Functions:
 User-defined or library functions
3
Function main
int main(int argc, char *argv[])
 main is the program starting point
 argc: number of command-line arguments  argv: array of command-line arguments
 What are the value of argc and argv for: myprog hello world
myprog “hello world”
4
1

Standard Program Elements
 Parse the command line
 Check for syntax and extract various options  Report error/usage if ill-formed command line
 Do the desired execution
 Read files, run threads, write output, etc.
 Cleanup and exit
 Free all resources
 Exit gracefully (print error msg if abnormal exit)
5
Program Structure
 Modules:
 Independent and interacting parts of a program
implementing specific functionality
 Typically implemented in separate files
 E.g.: Web browser:
 Module for tracking user input
 Module for communicating with server  Module for displaying Web pages
6
C Program Files
Header files Source files
Program
Libraries
7
Header Files (.h)
 Contain types, function prototypes, constants
 Contain elements that are shared across
modules
 Contain the interface exposed to external programs
 E.g.: library header files
 Default include path: /usr/include
8
2

Source files (.c)
 Core of the program modules
 Implement the functions of the program
 May contain static variables that are required only in that source file
 Contain include statements for header files to include global declarations and prototypes
#include
#include “decls.h”
9
Libraries
 Utility functions/system calls
 Pre-installed on the system
 Prototypes of functions called from program must be declared
 Include appropriate header files
 String library: #include
 Libraries may have to be linked at compile time using “-l” flag. E.g.:
cc –o hello hello.c -lpthread
10
Programming Commandments
 Thou shalt write readable code  Thou shalt use documentation  Comments and indentation
 Thou shalt use good coding style
 Meaningful variable/function names
 E.g.: sort_list() instead of foo()
 Use named constants instead of numbers
 E.g.: MAXLEN instead of 1024
 Avoid very big and bloated functions
 Put unrelated functions in separate files
11
Error Handling
 If a function can return an error, check for it  Check for every possible error, even if rare
 Return values from your functions
 Error codes on error
 Do not exit abruptly from your functions  Exit gracefully from main()
 System/library calls: use perror()  Library calls return -1, set errno
 Always check library calls for errors if (close(fd)==-1)
perror(“Failed to close the file”);
12
3

Program Compilation
 Preprocessor: Processes include statements, macro and constant definitions
 Compiler: Converts source code into assembly code
 Assembler: Converts assembly code into machine language
 Linker: Combines multiple object files into executable program
13
Program Compilation
hello.c
#include #define MAXLEN 1024
void print_msg() {
char *msg=“Hello World”;
printf(“%s\n”, msg); }
Compiler hello.s
hello.o
01110010100 0111…..
Assembler
Object files, Libraries
Linker
hello
Print_msg: store this load that push register .
. .
14
Program Compilation: make
 make builds programs by using dependency trees
 Makefile allows multiple program dependencies to be specified in a file
 If a source/header file changes, make recompiles dependent targets recursively
15
hello: hello.o util.o
gcc –o hello hello.o util.o
hello.o: hello.c hello.h
gcc –c hello.c
util.o: util.c util.h
gcc –c util.c
Program Execution
 Runtime environment  Program arguments
 Environment variables  Dynamic libraries
 Other programs and the Operating System
16
4

Program Arguments and Environment
 Command-line arguments:  argc, argv
 Environment variables:
 Associated with each program
 Typically determined by the invoking shell
environment
17
Dynamically Linked Libraries
 Static vs. dynamic linking
 Statically linked libraries are linked into the
executable at compile time  Is this a problem?
 Dynamically linked libraries are loaded at runtime when the corresponding function is called
18
Other Programs and Operating System
 Other programs impact your program in several ways:
 Communication
 Sharing of data
 Affect performance
 Operating system:
 Provides all the low-level services and utilities  Manages resources for your program
 Provides security and isolation
19
Testing
 Run lots of test-cases
 Ill-formed command lines (too few, too many
arguments)
 Missing files, data, input, etc.
 Boundary conditions: NULL pointers, zero-size
arrays, (n+1)-length strings
 Too long/too short strings, numbers
20
5

Debugging
 Iterative process  Using printf()
 Track variables, functions by printing out useful information
 Edit, recompile, run to get additional info
21
Debugging: Using a Debugger
 Allows examining the program in slow-motion with pauses and fast-forwards (playbacks, but no rewinds)
 Set breakpoints
 Step through program
 Examine variable values, stack contents
 E.g.: gdb (GNU debugger)
 Use –g flag while compiling program
gcc –g –o hello hello.c
22
Program Performance
 Correctness and Functionality  Quantitative Metrics
 Runtime
 Latency
 Throughput
 System Resource usage
 CPU time, memory, disk space, network bandwidth
23
Observing a Running Program
 time: Get the runtime of your program (both user and kernel time)
 ps: Gives information about running programs in the system (processes)
 top: Gives resource usage information (CPU share, memory, running time, status, etc.)
 strace: Trace system calls
24
6