Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAM IN EXECUTION Subject code: CSCI-620

Similar presentations


Presentation on theme: "PROGRAM IN EXECUTION Subject code: CSCI-620"— Presentation transcript:

1 PROGRAM IN EXECUTION Subject code: CSCI-620
Subject name: Operating Systems Security Exercise No.: Unit: 01 Unit topic: Program development procedure Professor : R. A. Mihajlović School year: /2013 © R.A. Mihajlovic, Reproduction is prohibited

2 CSCI-620 Operating Systems Security
Topics Download compiler and install it Editor as OS admin and programmer’s utility Editing the source text file of 3GL program instructions Program compilation and compiler utility Program linking and linker utility Program testing CSCI-620 Operating Systems Security Exercise 2.1

3 Compiler installation
Go to and download compiler files archive tc.rar. CSCI-620 Operating Systems Security Exercise 2.1

4 Compiler installation
Unarchive tc.zip file and place all of the files found in the archive in the directory c:\tc. This very simple compiler will help us better understand program development procedure and distinguish basic memory elements of the program in execution. CSCI-620 Operating Systems Security Exercise 2.1

5 CSCI-620 Operating Systems Security
HelloW.c Program In the working directory c:\tc start editor EDIT.COM. And atart editing the file helloW.c CSCI-620 Operating Systems Security Exercise 2.1

6 Simple source code of a program
Edit simple program “HelloW.c” that uses standard input/output library function printf() to output character string “Hello World!” CSCI-620 Operating Systems Security Exercise 2.1

7 Testing editing results
Verify that the C language source text file is saved properly. Character f in printf() stands for formatted printing. Character ‘\n’ is invisible character when printed moves screen pointer to the next line. CSCI-620 Operating Systems Security Exercise 2.1

8 CSCI-620 Operating Systems Security
Program compilation Compile the program using cc (i.e., cc.exe program file) compiler. CSCI-620 Operating Systems Security Exercise 2.1

9 Compiler is programmer’s utility
Compiler is a program that translates 3GL C-language text into the 0GL binary object code compatible with the specific CPU instruction set document. CSCI-620 Operating Systems Security Exercise 2.1

10 Compiler executable file
Compiler cc.exe file is a copy of the original turbo C compiler program file Tcc.exe. Compiler file name cc is the original UNIX file name. CSCI-620 Operating Systems Security Exercise 2.1

11 Verifying compiler’s output
Verify that the compiler has produced the object code file HelloW.obj and that the linker has produced the executable file HelloW.exe. CSCI-620 Operating Systems Security Exercise 2.1

12 CSCI-620 Operating Systems Security
Testing the program Try to execute the complete linked program HelloW.exe. Program file is executable only In the Windows-DOS operating environments, not under Linux or Solaris. CSCI-620 Operating Systems Security Exercise 2.1

13 Compilers, Operating Systems Security & portability
Given compiler is specialized in producing object code only for specific CPU and specific operating system. Same source program HelloW.c may be compiled by the Linux compiler gcc. The output of Linux gcc compiler could be compatible with the same CPU but executable only under Linux. C source code is portable without any reprogramming changes from one compiler to another (e.g., from Windows-DOS compiler Tcc.exe to Linux compiler gcc) C object or executable code (e.g., programs and object libraries) are not portable form one OS to another or from one CPU to another family of the CPUs. CSCI-620 Operating Systems Security Exercise 2.1

14 CSCI-620 Operating Systems Security
Output is kernel’s job Executable binary program has displayed a text or string of characters on the monochrome console display. This output operation could only be handled by the OS kernel video output related service. CSCI-620 Operating Systems Security Exercise 2.1

15 Linkable library functions
Function printf() was used by the programmer to call kernel’s help regarding the output of the character string. Compiled and ready for linking function printf() was found by the linker in the stdio library of functions. CSCI-620 Operating Systems Security Exercise 2.1

16 Linker is programmer’s utility
Programmers use programming utility programs or tools such as editor, compiler or linker to do their work. Example: Linker utility program known as turbo linker is stored in the executable file Tlink.exe. CSCI-620 Operating Systems Security Exercise 2.1

17 Small code stdio library file cs.lib
Standard I/O library of C program linkable functions such as printf() is stored in the file cs.lib. File cs.lib is binary object file, made of CPU object code instructions, (Not a text formatted file). CSCI-620 Operating Systems Security Exercise 2.1

18 Object code file HelloW.obj
Not all object files are executable, (e.g., HelloW.obj). Only fully linked object file is executable, (HelloW.exe). CSCI-620 Operating Systems Security Exercise 2.1

19 CPU and OS executable files
All object code files are CPU executable, but only some are OS executable. Completely linked object file is CPU executable and OS executable. Object file HelloW.obj without function printf() and the output service code could not print. CSCI-620 Operating Systems Security Exercise 2.1

20 Question: Linkers and compatibility
Why is executable program HelloW.exe executable under Windos-DOS OS not executable (portable) on Linux on using the same hardware platform? CSCI-620 Operating Systems Security Exercise 2.1

21 Answer: Linkers and compatibility
Why is executable program HelloW.exe executable under Windos-DOS OS not executable (portable) on Linux on using the same hardware platform? Linker utility known as linkage editor is attaching all needed library functions to the needy main program and is adding all OS specific instructions directed at the OS program loader, instructions known as the executable file header record. These headers are different under different Operating Systems Security. Function such as printf() appear to programmer under Windows and Linux as the same function but the internal object code of the library function is different. CSCI-620 Operating Systems Security Exercise 2.1

22 Executable file internal format
Complex files, such as executable file, identify their nature, purpose and organization with the header record at the very beginning of the file! These headers are different on the executable files on different Operating Systems Security. Different OSs require different instructions on how to handle the file. An executable UNIX file internal structure CSCI-620 Operating Systems Security Exercise 2.1

23 Homework Question: Executable Windows file formats.
Use google, wiki etc., and write short paper on the internal structure of executable Windows .DLL, .EXE and .COM files. CSCI-620 Operating Systems Security Exercise 2.1

24 Question: Compiler, linker and stdio.h file
Inspect the source file “HelloW.c”. What is the purpose of the instruction #include “stdio.h”? CSCI-620 Operating Systems Security Exercise 2.1

25 Answer: Compiler, linker and stdio.h file
Inspect the source file “HelloW.c”. What is the purpose of the instruction #include ”stdio.h”? C-program instruction lines starting with the character ‘#’ are aimed at the preprocessor program. Instruction #include tells preprocessor to find C-language text file stdio.h and replace the instruction line with the contents of the found stdio.h file. CSCI-620 Operating Systems Security Exercise 2.1

26 Compiler, linker and stdio.h file
In the stdio.h file one C-language line instructs the compiler not to be alarmed by the use of printf() function without any subsequent source code for the used function. CSCI-620 Operating Systems Security Exercise 2.1

27 Compiler, linker and stdio.h file
Find the line in stdio.h file that tells compiler to compile program as is, not to report error, and leave to the linker to add the missing printf() code to the program. Use edit.com editor and Alt-S editor command. CSCI-620 Operating Systems Security Exercise 2.1

28 Compiler, linker and stdio.h file
Line in the stdio.h file containing the printf() character string is C-language function prototype (C++ signature) instruction, telling compiler to just reserve the space in the data segment for the return value (int) and parameters to be passed. CSCI-620 Operating Systems Security Exercise 2.1

29 CSCI-620 Operating Systems Security
Exercise: Repeat all shown above. Present screen shots of your work. You may use Linux and gcc, but you must show each screen shot as shown here. CSCI-620 Operating Systems Security Exercise 2.1

30 CSCI-620 Operating Systems Security
Exercise: Write a short program HelloN.c that prints on the console display text string “Hello your name\n” Rename library file cs.lib to css.lib, compile your program and try to execute it. What is wrong? Explain. CSCI-620 Operating Systems Security Exercise 2.1

31 CSCI-620 Operating Systems Security
Exercise: Rename library file css.lib back to cs.lib, compile your program and try to execute it. What is the new file that you have obtained now? Who has created such a file? CSCI-620 Operating Systems Security Exercise 2.1

32 CSCI-620 Operating Systems Security
OS Kernel OS kernel is the central part of any operating system. Kernel is in charge of providing service to application programs by running some of its code for application that has called/needs the system service. Operating System OS Kernel Host A – Memory Space CSCI-620 Operating Systems Security Exercise 2.1

33 OS Kernel Serving Applications
Application programs run internal functions (Calls and returns) and some time have to do something like printing that original programmer did not know how to program, then kernel service function is called. Program in Execution Operating System printing I/O service OS Kernel Host A – Memory Space CSCI-620 Operating Systems Security Exercise 2.1

34 System memory map and locations
Systems memory map is one dimensional array of memory locations and address numbers. To simplify major concepts explanation we shall assume that one location may contain a complete machine language instruction. . . . A A+1 A+2 A+3 A+4 N-1 . . . CSCI-620 Operating Systems Security Exercise 2.1 34 34

35 Memory map and program parts
To execute programs CPU needs to find all program instructions and all data loaded into the systems memory space. . . . A A+1 A+2 A+3 A+4 Segment of the memory map holding all instructions, all program functions, is named Code Segment or CSp, and segment that holds all program’s variables and relevant data values is known as Data Segment or DSp. CSp . . . . . . DSp CSCI-620 Operating Systems Security Exercise 2.1 35 35

36 Memory map and program parts
Data and code are separated in memory as two distinct segments. This is truly, physically not exactly so but organizationally and logically appears to be so, and we shall assume to be even physically. Memory image of the loaded program of this sort exists in the memory map that we may call virtual memory map. . . . CSp . . . . . . DSp CSCI-620 Operating Systems Security Exercise 2.1 36 36

37 CSCI-620 Operating Systems Security
Program memory image 1 D D+1 L L+1 N Static linker utility helps make preliminary program’s memory image, by merging all instructions into one block that later becomes CSP. OS loader helps create program’s memory image. Complete process image contains: OS controlled process data block PCB, Data segment DSP, and Code segment CSP. OS PCB variables DSP main() CSP printf() CSCI-620 Operating Systems Security Exercise 2.1 37 37

38 Code segment – Made of functions
With modern modular programs, program’s code segment is made of functions. Functions as named individual containers of instructions, are the basic software module units. Example: C language program made of two functions: main() and printf() variables DSP main() CSP printf() CSCI-620 Operating Systems Security Exercise 2.1 38 38

39 Modular Program Development
Some of the functions are made by the application programmers and some are made by the library programmers. Libraries are reusable software, (Once made and when needed, simply copied and added to the compiled main code. Copied by the linker development tool). variables DSP Example: Function main() is made by the developer, and Function printf() is made by the library vendor’s programmers. main() CSP printf() CSCI-620 Operating Systems Security Exercise 2.1 39 39

40 Function call and kernel API
On each function call thread takes 2 jumps: Jump to the first instruction of the function printf() block, and Jump back or return. CSp CALL printf() . . . . . . START printf() FUNCTION return CSCI-620 Operating Systems Security Exercise 2.1 40 40

41 Function call and kernel API
Some function code can be used to reach functions of the kernel, the so called service routines. Kernel I/O service functions and kernel’s screen drivers are needed to display (to output the text string “Hello …” on the screen. To kernel CSp CALL printf() . . . . . . return START printf() FUNCTION return CSCI-620 Operating Systems Security Exercise 2.1 41 41

42 Programming to Use Interrupts
1 D D+1 L L+1 N Programmers may be reusing systems code by simply calling certain kernel resident programs. Service CSK X=3 Y=2.5 MAIN CSP PRINT CSCI-620 Operating Systems Security Exercise 2.1 42 42

43 Using non standard editors
Source code program text files can be edited with non standard editor programs like MS Word text processor.

44 Saving source text file
Saving file requires proper text encoding code selection (ASCII) and quoted file name.

45 Character encoding program errors
Some character (font-image) code conversions into the binary version of each character may not work properly. Some characters may not be wrongly encoded.

46 Correct wrong characters
Use standard editor whenever you can, or be careful when selecting character code to save source text file.

47 Notepad Windows standard text editor
Use quoted name “hello_1.c” to avoid .txt default extension.

48 CSCI-620 Operating Systems Security
Homework Perform all drills from this exercise. If you have alternative compiler (e.g., 64-bit compiler, on some other OS than Windows) try to do exactly the same as shown in this presentation. Present screens shots of all your work. If you are using different compiler, show exact equivalents of all screen shots shown here. CSCI-620 Operating Systems Security Exercise 2.1

49 CSCI-620 Operating Systems Security
Reference [1] C Programming Language [2] C Programming Tutorial [3] C Basics [4] Intro to C Programming I (IIT), youtube.com [5] Introduction to C Programming Prt1, youtube.com [6] C Programming Tutorial, youtube.com CSCI-620 Operating Systems Security Exercise 2.1

50 CSCI-620 Operating Systems Security
The End CSCI-620 Operating Systems Security Exercise 2.1


Download ppt "PROGRAM IN EXECUTION Subject code: CSCI-620"

Similar presentations


Ads by Google