470 likes | 496 Views
Run time environments. What is runtime?. Runtime: time during which a program is running (executing). It contrasts to other phases of program as compile time, link time, load time, etc. Topics we will cover. סביבת הרצה. What is a runtime environment (RTE)? Concurrency management in RTE
E N D
What is runtime? • Runtime: time during which a program is running (executing). • It contrasts to other phases of program as compile time, link time, load time, etc
Topics we will cover סביבת הרצה • What is a runtime environment (RTE)? • Concurrency management in RTE • Memory management in RTE • Communication and networking in RTE • Persistent data management in RTE
Topics we will cover • RTE’s standard tools and abstractions • RTE limitations and costs: how knowing your RTE helps task design • Multiple levels of abstractions formed by intra-RTE relationships • Compare RTEs: Unix and Win32 OS, Java Virtual Machine (JVM) Servlets, distributed RTEs
RTE 1st example: OS • Who is the RTE and who are entities running inside? • OS vs. Process (entity running/executing inside OS): • Definition • Interaction • Process lifecycle
What is OS? • OS manages sharing of computer resources • OS performs basic tasks: • allocate and control memory • control I/O devices • control CPU time • networking • managing file systems
OS system calls • Most services offered by OS are abstract objects (e.g., files and directories) • echo abc > /dev/ttyS0 • Interface for service request from OS is through system calls • system call: low level function which enable interaction between process and OS
/usr/src/linux/fs entry.S : system-call and fault low-level handling routines * NOTE: This code handles signal-recognition, which happens every time after a timer-interrupt and after each system call. ENTRY(system_call) pushl %eax # save orig_eax SAVE_ALL GET_CURRENT(%ebx) testb $0x02,tsk_ptrace(%ebx) # PT_TRACESYS jnetracesys cmpl $(NR_syscalls),%eax jaebadsys call *SYMBOL_NAME(sys_call_table)(,%eax,4) movl %eax,EAX(%esp) # save the return value
Unix system calls • Sys calls for file I/O • open() close() read() write() • Sys calls for process control • fork() wait() execv(), exit() sigal() kill() • Sys calls for IPC • pipe() dup() asmlinkagessize_tsys_read(unsigned intfd, char * buf, size_t count) { ssize_t ret; struct file * file; ret = -EBADF; file = fget(fd); if (file) { if (file->f_mode & FMODE_READ) { ret = locks_verify_area(FLOCK_VERIFY_READ, file->f_dentry->d_inode, file, file->f_pos, count); if (!ret) { ssize_t (*read)(struct file *, char *, size_t, loff_t *); ret = -EINVAL; if (file->f_op && (read = file->f_op->read) != NULL) ret = read(file, buf, count, &file->f_pos); } } if (ret > 0) dnotify_parent(file->f_dentry, DN_ACCESS); fput(file); } return ret; }
Processes • Main OS abstraction of an execution unit • Instance of a program, which is currently executing.
Process vs. Program • Program = passive collection of instructions • Process = actual execution of instructions • Possibly many simultaneous incarnations of same program, each incarnation = process
Process / OS interaction • Resource allocation: manage computer resources between processes • Main resources : • CPU time • Disk space • Memory • I/O devices • other services (communication, etc…)
CPU time • CPU: execute machine code sequentially, one instruction at a time. • Multitasking: processes executing concurrently - CPU sharing. How? • Context switch: CPU stops executing one process and switch to another. • In modern OSs, CPUs, one context switch = ~10 microseconds (compare with single computation execution ~1 nanosecond) • 1 context switch = 10000 instructions • Scheduler: entity inside OS in charge of selecting which process will be executed next
Process / OS interaction • Services initiated by the OS • CPU (through context switch) • initial memory allocation (to load the code itself). • Services requested by the process • memory allocation • disk space • access external devices: mouse, KBD, screen
Process Life Cycle • Processes states: • Running (executed in CPU) • Waiting • CPU time, • I/O (much slower than a CPU operation), • event (synchronization: process waiting for another process to reach a certain state before it can continue)
Process Life Cycle • OS manages the life cycle of a process • 3 main steps: create, manage, terminate
Process Life Cycle • Compiler: translate a program in a language such as C or C++ to executable files • Executable: binary file that contains machine instructions to be executed on the CPU • Shell: piece of software that provides an interface to the services of an OS kernel
Process Life Cycle - invoke • OS is asked by user to create a process through a shell • OS identifies which program (= executable file) is to be executed.
Process Life Cycle – new process • OS creates a new process to run executable: • process ID unique identifier • process table: maintains information about running processes • OS allocates memory for new process in computer's main memory (RAM) • OS loads instructions from program executable file into main memory
Process Life Cycle - main • OS identifies starting function in program (e.g "main" in C and C++) and invokes it (calls the function). • command line arguments: main function receives arguments from OS. • instructions are executed, specified in program.
Process Life Cycle – sys call • Process invokes system call: printf("Hello"); • process is interrupted until the OS executes the service requested. • process undergoes a context switch. • system call exit() - process is terminated: • resources (memory, process ID) are freed • entry is removed from process table.
Process Life Cycle – summary • Main elements involved when an OS RTE creates a process
OS RTE • OS is an example of basic but complex RTE: • interface between the user code and the hardware of the machine hosting • multitasking via “process” abstraction (collection of variables): • state • current execution point • resources
The Java virtual machine (JVM) • JVM is an RTE! • Executes Java classes • Simulates a complete machine for Java programs execution • interprets Java byte code and translates into actions or Operating System calls
Interpreter vs. Compiler • byte code: java code compilation into JVM instructions. • machine code: C/C++ compilation in binary code
Interpreter vs. Compiler • Compiled languages: • directly processed on CPU • a compiler+linker translate text program to machine code • runs faster than interpreted code • Interpreted languages: • on the fly interpretation into machine code • interpreter is an RTE • supplies services to program • portability, code is not "standalone“ • test on the fly code modifications
JVM Bytecode compromise • can be compiled to intermediate language (bytecode in Java)
JVM RTE • JVM simulates a complete machine, on which Java programs are executed: • byte code: specific JVM instructions • portability: interpreted on any OS with a JVM
JVM RTE • JVM is a process inside OS RTE • intermediate between program and OS RTE • program interacts with JVM using object oriented paradigm = language interface • system.out - JVM support for printing
Java Program Lifecycle • java byte code compilation: P.javaP.class • JVM is invoked: java P.class • load class file and referred classes (through import)into JVM process main memory • invoke Main with parameters: static Main(String args[]) • program requires a service from the JVM through System package • System.exit : JVM cleans resources allocated to program, JVM process is terminated
Java program execution (inside JVM RTE inside OS RTE) • Program service requests from JVM • JVM translates requests into sys. calls to OS • JVM can run multiple Java class • OS can run multiple JVM
Internet Browser as RTE • browser provides RTE for applets: • mini-applications that run within a browser • written in Java • interact with Applet Container inside browser • applet interface - environment and applet interaction : • load - container • initialize - only once • start/stop – when page becomes visible/invisible • destroyed - container shut down/ no resources
Applet lifecycle • applet - visual object for information display in a window • import java.applet.Applet, import java.awt.Graphics • Invoking applets from HTML: • <applet code=SPL10Applet.class width=400 height=200>
HTTP Web Server as RTE • Model: web server host programs (servlets), to be run on demand • receive client requestfor URL • run servlet • return output to client
Java Servlet Container • web server: program that serves content, using Hypertext Transfer Protocol (HTTP) • servlets: object used to extend servers to host applications accessed via a request-response programming model • javax.servlet / javax.servlet.http packages • processing/storing data submitted by an HTML form • providing dynamic content, e.g. results of database query • managing state information ,e.g. online shopping cart
Tomcat Java Servlet Container • Tomcat HTTP server running as process • compiled servlet (Java class) • associate URL to servlet in server • Web client submit requests to server
Invoke servlet • web client connects the HTTP: http://localhost:8080/servlet1?action=edit • client connects the server at: localhost:8080 • HTTP server gets the request: /servlet1?action=edit • HTTP maps request to the servlet1 • HTTP server activates servlet, invokes method doGet with parameter "action=edit" string. • servlet executes code and returns a string • HTTP server passes string to client • client displays the answer on the screen
URL - Uniform Resource Location • standard format to uniquely identify a resourcethat can be accessed through a protocol. • format: protocol://[host]:[port][request-path]?[query-string] • protocol can be http or ftp. • host and port identify network location where the server providing access to the resource can be found • request path uniquely identifies a specific resource among the several resources managed by the server
Servlet lifecycle • controlled by the container in which the servlet has been deployed • a request is mapped to a servlet (URL maps to servlet in server configuration) • container performs: • loadservlet class, create an instance • initializeservlet instance • listener Class • invoke service method, passing request and response objects • finalizes servlet by calling destroy method.
Java Enterprise Edition (JEE) • development platform for distributed component-based applications /servers • application = collection of components that run on several computers across a network and collaborate (=server) • components = store data in a database, implement usage policies enforce access rights…
Enterprise Java Beans (EJB) • distributed RTE • server application • running on several machines on a network • communication through dedicated protocol • 3 level of abstractions: • EJB Server is an RTE that runs containers • containers are RTEs, runningcomponents which are the java classes • server is written in Java, hence running in the JVM RTE.
The EJB server/container services • Naming server: components are named so that they are found based on their name and not on location, can move from machine to machine without interference, naming server maps abstract names to network addresses • Messaging server: components send messages to each other. controls messages reach their destination and dispatches messages based on flexible policies. • Activation: the Application Server determines when each component should run. It can also de-activate a component, permanently (destroy) or temporarily (unload). Application Server manages the state of components. • Security: the Application Server enforces access rights according to application roles. Each component can specify which users can perform which actions. • Transactions: a sequence of actions (even across components) behaves as a "unit" transaction - a primitive operation that can either succeed as a whole (commit), or fail as a whole without leaving partial results in any component (full rollback). • Resource Pooling: the Application Server can "pool" (reuse/share) resources . In this case, whenever a component accesses a resource (e.g., a file, a thread, a database connection), the Application Server must make sure the resource is acquired by the component.
Summary • programs we write interact with the environment that executes them and their behavior is defined by it. • interface between program and RTE is bi-directional: program invokes services provided by container. Container can send events or change the state of the process as it is running. • main responsibilities of an RTE include: • Process management • Storage management (memory, file, dbms) • Communication management • Hardware/Devices management • Security • different RTEs (OS: Linux/Win32, JVM, applet, servlet -container, distributed RTEs). The structure of the interface remains quite stable.