DOC PREVIEW
U of I CS 241 - Systems Programming

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 Systems ProgrammingProgramsOverviewAdministrativeComment: Check out also R&R Appendix AOS: A Friendly DeceptionUsers, Programs, ProcessesAnalogyProcessesExecuting Programs (R&R 21-48)Importance of Writing Correct ProgramsFunction ‘checkpass’ susceptible to buffer overflowPassword check susceptible to buffer overflowStack Layout for ‘checkpass’Program LayoutLibrary Function CallsError reportingMany Library Calls abort if process is interrupted by signalHandling ErrorsArgument ArraysArgument ArraysCreating Argument ArraysIdentifiers, Storage and Linkage Classes (R&R, p812-814)IdentifiersLinkage classesStorage classes: static and automatic VariablesFunctionsSummaryCS241 Systems ProgrammingProgramsKlara NahrstedtLecture 21/23/061/23/2006CS 241 - System Programming, Klara Nahrstedt2Overviewz What is a process?z Programs : Importance to write correct programs–Layout of a Program Image– Programming Issues to Pay Attention to zSummary1/23/2006CS 241 - System Programming, Klara Nahrstedt3Administrativez This week –Read Chapter 2.1 (Tanenbaum)– Read Chapter 1.6, 2 and 3 (Robin&Robin)– Machine Problem 0 will be posted (January 25)– Deadline for MP0 is January 30– Read Newsgroup and Class Website1/23/2006CS 241 - System Programming, Klara Nahrstedt4Comment: Check out also R&R Appendix AzUNIX Man PageszCompilationzHeader FileszLinking and librarieszMacros and conditional compilationzMakefileszDebugging Aids – lint, debugger, trusszIdentifiers, Storage Classes and Linkage Classes1/23/2006CS 241 - System Programming, Klara Nahrstedt5OS: A Friendly DeceptionzOS–Manages resources–Involves asynchronous and sometimes parallel activitieszDrawback? Maybe lots of extra workz Benefit: makes life easier for user1/23/2006CS 241 - System Programming, Klara Nahrstedt6Users, Programs, ProcesseszUsers have accounts on the systemz Users launch programs–Many users may launch same program–One user may launch many instances of the same programzProcesses: –an executing program1/23/2006CS 241 - System Programming, Klara Nahrstedt7AnalogyzProgram: steps for attending the lecture–Step1: walk to Siebel Center Building–Step2: enter 1404 Lecture Room–Step3: find a seat–Step4: listen and take notes zProcess: attending the lecture–Action–You are all in the middle of a process1/23/2006CS 241 - System Programming, Klara Nahrstedt8ProcesseszA process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended, scheduled and terminatedzProcess is a unit of work in a modern computerzProcess Types: –OS processes executing system code program–User processes executing user code program zProcesses are executed concurrently with CPU multiplexing among them1/23/2006CS 241 - System Programming, Klara Nahrstedt9Executing Programs (R&R 21-48) zImportance of writing correct programs–Example: Buffer Overflow and Password CheckingzProgram Layoutz Library Function Callsz Function Return Values and Errorsz Argument Arraysz Use of Static Variables and Functions1/23/2006CS 241 - System Programming, Klara Nahrstedt10Importance of Writing Correct ProgramsBuffer Overflowchar buf[80];printf(“Enter your first name”);scanf(“%s”, buf);Problem: if the user enters more than 79 bytes, the resulting string andthe string terminator \0 do not fit in the allocated variable!!Possible Fix of the Problem: char buf[80]; printf(“Enter your first name”);scanf(“%79s”, buf);.1/23/2006CS 241 - System Programming, Klara Nahrstedt11Function ‘checkpass’ susceptible to buffer overflow#include <stdio.h> #include <string.h> int checkpass(void){ int x; char a[9];x = 0; fprintf(stderr,"a at %p and\nx at %p\n", (void *)a, (void *)&x); printf("Enter a short word: ");scanf("%s", a); if (strcmp(a, "mypass") == 0) x = 1; return x; }1/23/2006CS 241 - System Programming, Klara Nahrstedt12Password check susceptible to buffer overflow#include <stdio.h> int checkpass(void); int main(void) { int x; x = checkpass(); fprintf(stderr, "x = %d\n", x); if (x) fprintf(stderr, "Password is correct!\n"); else fprintf(stderr, "Password is not correct!\n"); return 0; }1/23/2006CS 241 - System Programming, Klara Nahrstedt13Stack Layout for ‘checkpass’32-bit address space, i.e., integers and pointers are 4 bytes1024baseaunusedxSaved frame pointerReturn address102010161012Buffer Overflow of ‘a’ can cause change of ‘x’ value DANGEROUS!!!10091000top1/23/2006CS 241 - System Programming, Klara Nahrstedt14Program Layout1/23/2006CS 241 - System Programming, Klara Nahrstedt15Library Function CallszTraditional UNIX – returns 0 successful, -1 unsuccessful, sets errnoPOSIX Standard Committee decides that no ‘errno’ be used. Instead: –all new functions return error codezGood coders handle ALL errors, not just mandatory (in the standard) ones.1/23/2006CS 241 - System Programming, Klara Nahrstedt16Error reportingzperror function outputs to standard error a message corresponding to the current value of errno–No return values of errors are defined by perror#include <stdio.h>Void perror(const char * s);--------------------------zstrerror function returns a pointer to the system error message corresponding to the error code errnum–If successful, strerror returns a pointer to the error string#include <string.h>Char *strerror(int errnum);1/23/2006CS 241 - System Programming, Klara Nahrstedt17Many Library Calls abort if process is interrupted by signalint error;int fildes;while (((error = close(fildes))== -1) && (errno== EINTR));if (error == -1) perror(“Failed to close the file”);1/23/2006CS 241 - System Programming, Klara Nahrstedt18Handling ErrorszAlways handle all errorszEither:–Print error message and exit program (only in main)–Return -1 or NULL and set an error indicator such as errno–Return an error codezAll functions should report errors to calling programzUse conditional compilation to enclose debugging print statements–cc -DDEBUG1/23/2006CS 241 - System Programming, Klara Nahrstedt19Argument ArrayszCommand line consists of tokens –arguments –ls –l (2 token)–mine –c 10 2.0 (4 tokens)int main(int argc, char * argv[])z argc is number of arguments, z argv is an array of pointers to the tokens1/23/2006CS 241 - System Programming, Klara Nahrstedt20Argument Arraysz An argument array is an array of pointers terminated by a NULL pointer.Each element of the array is of type char * and represents a


View Full Document

U of I CS 241 - Systems Programming

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Systems Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Systems Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Systems Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?