Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 RunTime Environments.

Similar presentations


Presentation on theme: "Lecture 1 RunTime Environments."— Presentation transcript:

1 Lecture 1 RunTime Environments

2 Administrative Stuff Reception Hour: TBD Course Site: Assignments: 4 assignments. (in pairs) – 40% (11%,11%,11%,7%) Final Exam – 60%

3 Course Overview System programming:
Mostly writing software that will service other software. Application programming also require system programming skills. A programmer often deals with hardware-level concepts Memory Data transfer Protocols Need to understand runtime environment (RTE) – the environment in which the programs runs. There are a few types of RTE’s but all follow a similar scheme

4 Course Overview We will review the main services provided by RTEs
Memory management. Concurrency in multi-thread and multi-process environments. Object Oriented Design - design patterns, principles and essentials Inter-process communication Networking and TCP/IP mode. Data storage – files/databases Scripting – compiling scripts, execution scripts Application layer protocols, such as HTTP or SSH protocols We will use: C++, Java, Python, SQL

5 Course Overview Week Lectures Overview 1 RTE and System modeling
Runtime Environments, introduction to C++, Java, and Python. 2 C++ Memory Memory Management, Stack and Heap Models, Memory Allocation, Garbage Collector 3 C++ Pointers Pointers, Member Initialization List, lvalue/rvalue References, Parameter Passing, C++ Arrays 4 C++ Objects Objects, Object Construction/Destruction, Rule of Three/Five, C++ Templates, Smart Pointers 5 Java 8 Type Safety, Generics, Lambdas, Class Object 6 TDD and DbC 7 Threads Processes, threads, actors, immutables, Thread safety 8 synchronization, atomics, semaphore, executors, liveness 9 Communication Sockets, multithreading examples (single thread, fixed thread-pool, thread per client), echoProtocol 10 Java NIO, ByteBuffers (changes to accommodate new code) 11 Selector, Flyweight pattern, Reactor pattern (new code) 12 Python/SQL Introduction to Python and SQL 13 Python ORM

6 The Operating System (OS)
The OS is an RTE. Processes – the entities running inside the OS. “programs” or “applications” OS Purpose: Manages hardware resources sharing among the different processes HDD (SSD), RAM, etc Performs basic tasks such as controlling input/output devices, CPU time, facilitating networking and file systems. The entity in the OS that manages these resources is called “kernel”. To interface with the kernel, a process uses “system calls” System calls are low level functions for a process to communicate with the OS

7 The OS diagram

8 Process Process- An abstraction for an execution unit.
It is an instance of a running program launched from the OS A program – a collection of instructions. Process – the execution of those instructions. There may be many instances of the same program – each one is a process. Thread – launched from inside a process (process code) Each process may have multiple threads Each thread may be executed in parallel We will discuss this extensively in several weeks

9 Process – OS interaction
A process interacts with the OS when hardware resources are needed The main resources the OS manages among all processes: Memory CPU time Disk space Input/Output devices Communication (WiFi/Ethernet/3G) Notification services The interaction can be partitioned into two: Services initiated by the OS CPU time, initial memory allocation to load the program Services requested by the process Memory, disk-space, access to I/O devices

10 Process Life Cycle: Execution
The OS manages the life of a process: Creates the process Manages its execution Terminates the process The user executes a program using an execution command The OS is asked by the user to create a process The OS identifies the executable file for the program The executable file consists of binary instructions, specific for each OS The OS creates the process which runs the executable To execute the process, the operating system: Provides each process a unique id (number) Allocates memory for the process in the main memory (RAM) Loads the executable instructions to the RAM Identifies the “main” function (using the entry point value), and executes it. It can execute it with arguments given by the user or the OS itself.

11 Process Life Cycle: System Calls
System calls are the interface between a program and the OS: Application developers often do not have direct access to the system calls They can access them through an application programming interface (API) The functions that are included in the API invoke the actual system calls

12 Process Life Cycle: System Calls
Programming interface to the services provided by the OS Typically written in a high-level language (C or C++) Three most common APIs are: Win32 API for Windows, POSIX API (all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM) There are 5 different categories of system calls: process control file manipulation (and protection) device manipulation information maintenance communication. Functions and system calls look the same but are different: printf(“hello”) vs func1(1,2).

13 Process Life Cycle: System Calls
There are 5 different categories of system calls: Process control Handles process execution/halting File manipulation (and Protection) Handles access to storage devices to manipulate files Device manipulation Manipulation hardware devices such as the monitor, graphics card, keyboard or mouse Information maintenance Storing process meta information, time, date, etc. Communication Inter-process communication and sharing data between them.

14 Process Life Cycle: System Calls
Process control end, abort, load, execute, create process, terminate process, get process attributes, set process attributes, wait for time, wait event, signal event, allocate and free memory File manipulation (and Protection) create file, delete file, open, close, read, write, reposition, get file attributes, set file attributes Device manipulation request device, release device, read, write, reposition, get device attributes, set device attributes, logically attach or detach devices

15 Process Life Cycle: System Calls
Information maintenance get time or date, set time or date, get system data, set system data, get process, file, or device attributes, set process, file, or device attributes Communication (between processes) Inter Process Communication or IPC, can be done via sockets, pipes, shared memory and others. create, delete communication connection, send, receive messages, transfer status information, attach or detach remote devices Example for pipes: ls –l | wc –n 2

16 System Calls: C++ API

17 Process Life Cycle: Termination
When a process executes “exit()” system call, the process is terminated by the OS as follows: Releases allocated memory space Closes open files (file descriptors) Cancels all pending timers Initiates a final “context switch” from the process Finally, process ID is released, and the process is deleted from the OS process table.

18 CPU time sharing The CPU executes machine instructions one by one sequentially CPU time is the main resource shared by the processes The OS shares the CPU among all processes executing concurrently That is “multitasking”. “Context switch”: Stop executing one process and switch to another one Context switches are expensive: Each takes the time of about 10,000 “regular” instructions The OS’s “scheduler” is the entity that decides “who runs next”.

19 CPU time sharing A process may be in several states:
New: Recently created, not scheduled yet Ready: Ready to run, but not yet assigned to a processor Running: Actually executing on a processor Waiting: Not currently executing. Not currently able to execute. Won't be runnable until some specific external event occurs, such as an I/O operation. Terminated: Done executing

20 Types of Programming Languages
Interpreted: executed using interpreter, directly from text code! the initial language may be compiled into an intermediate language, which is interpreted. Examples: Python, Bash, Matlab Compiled: directly processed by the machine CPU (native code) text files containing code are translated to machine code though a compiler and a linker. Examples: C, C++ Hybrid: starts with interpreted code and compiled to native code on the fly. Examples: Java

21 Interpreted vs. Compiled to binary Language
Advantages of Interpreted Languages: higher level: and easier to program than compiled languages. Means simpler syntax than compiled languages More portable: can run on any machine/OS that supports the interpreter without any code modifications. Disadvantages of Interpreted Languages: The code can be run only with the interpreter – not “standalone”. Binary codes runs faster than interpreted codes. Although interpreters with runtime optimizations may also be efficient.

22 Hybrid Language: The Java Virtual Machine
The Java Virtual Machine is a process, and acts as an intermediate layer between the Java byte code and the OS. A text file P.java is compiled to a java byte code file P.class. All Java code is (interpreted) executed using the JVM process Java code is not executed via the operating system! This allows Java code to be portable. Interacting with the OS and hardware is done via interacting with the presented JVM objects only. Example: The object system.out represents the JVM support for printing.

23 Java Just-In-Time (JIT) Compiler
 The JVM also contains a Just-In-Time (JIT) compiler While the JVM interpreting the byte code program, highly executed pieces of code are compiled to an optimized native machine code. JIT is what allows the JVM to have a performance that is in many cases comparable with native code. But not always!

24 Java Program Lifecycle
Launch: The JVM is invoked with a parameter which refers to a class file. For example, java P.class. The JVM starts running as a process inside the OS. The JVM loads the class file, and other “imported” classes. The JVM identifies a method static Main(String args[]), parses the command line parameters, and then invokes the Main method with these parameters. Requesting System Calls: When the program requires a service from the JVM, it invokes the corresponding method from the JVM interface. The JVM services are located in the System package. The Java program interacts with the JVM only - it never invokes a system call directly. Exit: When the program executes the System.exit method the JVM cleans up all the resources allocated to the program Only then the JVM process is terminated

25 The JVM operation environment

26 Compiled Language: C++
C++ code is compiled directly to machine code! Done by using C++ compiler (named g++). System calls can be directly initiated from the code via given API Executing the compiled code is done by the OS itself, no intermediates Examples: any *.exe file in windows or executable files in linux.

27 C++ Compiling Procedure
Preprocessor: accepts source code(*.cpp); removes comments; add the content of the include files. Compiler: translates source to assembly code (AND, OR, JMP, SUB etc.). Assembler: creates object code (machine code, 0-s and 1-s, *.o files). Linker: links the object files to an executable and creates an executable machine code; marks the main() as the starting point of the execution.

28 Interpreted Language: Python
Simply write the code, and execute it. No compiler needed! Code is mostly shorter, and easy to read Syntax created to be short No types – increases clarity Extensive built in iterators and looping tools. Disadvantages It lacks true multiprocessor support, concurrency is not easy to use Slower in execution speed in comparison to other languages Not suitable for memory intensive tasks

29 C++ vs Java vs Python Code Length 3-5 times shorter than Java programs
TOPIC C++11 Java 8 Python 3.5 Memory Management manual allocation manual deallocation automatic garbage collection automatic allocation automatic deallocation Code Length Longer lines of code as compared to python. About 40% more code in comparison to C++ 3-5 times shorter than Java programs Typing System statically typed, both code and compiler dynamically typed, Interpreter is strongly typed, language is type-free! Compiling Compiled to machine code Compiles to byte code, then interpreted by the JVM Interpreted by the python interpreter Execution Speed(loops, dynamic arrays with numbers, basic math operations) 100% 1176% 1800% Portability Platform dependent, must be recompiled for different platform Platform independent, byte code generated works on every OS. Platform independent, code is interpreted upon execution. Thread Support Built-in support since C++11 It has built-in thread support. No multithreading! Syntax Complexity Defining blocks using {, ending statements using ; Using indentation only.

30 Summary Our programs interact with the RTE that executes them.
The program invokes services provided by the RTE The RTE can send events or change the state of the process as it is running The main responsibilities of an RTE include: Process management (creation, initialization, destruction) Storage management (memory, file, dbms) Communication management Hardware/Devices management Security

31 Runtime Environments Topics to be Covered:
What is a runtime environment (RTE)? Management of memory in a RTE. Management of concurrency in a modern RTE. Communication and networking in a RTE. Data management in a RTE.

32 Runtime Environments We will compare RTEs:
Operating systems – Unix/Windows. Java virtual machine. Other RTEs – V8, CLR. For each RTE, we will define: Which component plays the role of the RTE Which components are entities running inside the RTE.

33


Download ppt "Lecture 1 RunTime Environments."

Similar presentations


Ads by Google