Run time environments

Download Report

Transcript Run time environments

Run time environments
SPL/2010
1
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
SPL/2010
2
Topics we will cover
1.
What is a runtime environment (RTE)?
2.
Concurrency management in RTE
3.
Memory management in RTE
4.
Communication and networking in RTE
5.
Persistent data management in RTE
SPL/2010
‫סביבת הרצה‬
3
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
SPL/2010
4
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
SPL/2010
5
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
SPL/2010
6
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
SPL/2010
7
/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
orig_eax
# save
SAVE_ALL
GET_CURRENT(%ebx)
testb $0x02,tsk_ptrace(%ebx) # PT_TRACESYS
jne tracesys
cmpl $(NR_syscalls),%eax
jae badsys
call *SYMBOL_NAME(sys_call_table)(,%eax,4)
movl %eax,EAX(%esp)
SPL/2010
# save the return value
8
Unix system calls
●
Sys calls for file I/O
●
●
Sys calls for process
control
●
●
open() close() read()
write()
fork() wait() execv(),
exit() sigal() kill()
Sys calls for IPC
●
pipe() dup()
SPL/2010
asmlinkage ssize_t sys_read(unsigned int fd, 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;
}
9
Operating System modules
SPL/2010
10
Processes
●
●
Main OS abstraction of an execution unit
Instance of a program, which is currently
executing.
SPL/2010
11
Process vs. Program
●
Program = passive collection of instructions
●
Process = actual execution of instructions
●
Possibly many simultaneous incarnations of
same program, each incarnation = process
SPL/2010
12
Process / OS interaction
●
●
Resource allocation: manage computer
resources between processes
Main resources :
●
●
●
●
●
CPU time
Disk space
Memory
I/O devices
other services (communication, etc…)
SPL/2010
13
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
SPL/2010
14
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
SPL/2010
15
Process Life Cycle
●
Processes states:
●
Running (executed in CPU)
●
Waiting
–
CPU time,
–
I/O (much slower than a CPU operation),
–
SPL/2010
event (synchronization: process waiting for another
process to reach a certain state before it can
continue)
16
Process Life Cycle
●
OS manages the life cycle of a process
●
3 main steps: create, manage, terminate
SPL/2010
17
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
SPL/2010
18
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.
SPL/2010
19
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
SPL/2010
20
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.
SPL/2010
21
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.
●
SPL/2010
22
Process Life Cycle – summary
●
Main elements involved when an OS RTE
creates a process
SPL/2010
23
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
SPL/2010
24
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
SPL/2010
25
Interpreter vs. Compiler
●
●
byte code: java code compilation into JVM
instructions.
machine code: C/C++ compilation in binary
code
SPL/2010
26
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
–
–
–
SPL/2010
supplies services to program
portability, code is not "standalone“
test on the fly code modifications
27
JVM Bytecode compromise
●
can be compiled to intermediate language (bytecode
in Java)
SPL/2010
28
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
SPL/2010
29
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
SPL/2010
30
Java Program Lifecycle
●
●
●
●
●
●
java byte code compilation: P.java
P.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
SPL/2010
31
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
SPL/2010
32
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
SPL/2010
33
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>
SPL/2010
34
HTTP Web Server as RTE
●
Model: web server host programs (servlets),
to be run on demand
●
receive client request
for URL
●
run servlet
●
return output to client
SPL/2010
35
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
SPL/2010
36
SPL/2010
37
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
SPL/2010
38
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
SPL/2010
39
URL - Uniform Resource Location
●
●
standard format to uniquely identify a resource
that 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
SPL/2010
40
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:
●
●
●
●
●
load servlet class, create an instance
initialize servlet instance
listener Class
invoke service method, passing request and response
objects
finalizes servlet by calling destroy method.
SPL/2010
41
distributed RTE ‘s
SPL/2010
42
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…
SPL/2010
43
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, running
components which are the java classes
server is written in Java, hence
running in the JVM RTE.
SPL/2010
44
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 deactivate 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.
SPL/2010
45
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.
SPL/2010
46