Docsity

Java Process Creation and Multithreading, Study notes of Operating Systems

Toggle sidebar
bg1
Process Creation in Java
Process Creation in Java
•Note: Java really doesn’t implement processes per se, but
provides a clean interface to spawn processes to run system
programs outside of the JVM
ā€¢ā€œProcessā€ class in Java
–Runtime.getRuntime.exec(ā€œā€) method will return a process object (interface)
that executes what is in the string argument
–all process objects support the following methods:
•waitFor()
•exitValue()
•destroy()
•Note: Java really doesn’t implement processes per se, but
provides a clean interface to spawn processes to run system
programs outside of the JVM
ā€¢ā€œProcessā€ class in Java
–Runtime.getRuntime.exec(ā€œā€) method will return a process object (interface)
that executes what is in the string argument
–all process objects support the following methods:
•waitFor()
•exitValue()
•destroy()
try {
Process child1 = Runtime.getRuntime().exec(ā€œ/bin/lsā€);
InputStream in = child1.getInputStream();
// echo output of ā€˜ls’
child1.waitFor();
System.out.println(ā€œChild exited with ā€œ + child1.exitValue());
} catch(IOException e) {
System.err.println(e);
}
New Subtopic: Threading
New Subtopic: Threading
•Up till now, we’ve viewed processes as
–unit of resource ownership
–unit of scheduling (dispatching)
•Consider a Server Process:
•Up till now, we’ve viewed processes as
–unit of resource ownership
–unit of scheduling (dispatching)
•Consider a Server Process:
Server
LOCAL
OS
FILE REQUEST(S)
FILE REQUEST(S)
FILES
FILES
read(), write()
SYSTEM CALL
(BLOCKING)
SYSTEM CALL
(BLOCKING) ā€œBOTTLENECKā€:
The Server Process
has a single ā€œthread
of executionā€
1
pf3
pf4
pf5

Partial preview of the text

Download Java Process Creation and Multithreading and more Study notes Operating Systems in PDF only on Docsity!

Process Creation in JavaProcess Creation in Java

• Note: Java really doesn’t implement processes per se, but

provides a clean interface to spawn processes to run system

programs outside of the JVM

• ā€œProcessā€ class in Java

– Runtime.getRuntime.exec(ā€œā€) method will return a process object (interface)

that executes what is in the string argument

– all process objects support the following methods:

• waitFor()

• exitValue()

• destroy()

• Note: Java really doesn’t implement processes per se, but

provides a clean interface to spawn processes to run system

programs outside of the JVM

• ā€œProcessā€ class in Java

– Runtime.getRuntime.exec(ā€œā€) method will return a process object (interface)

that executes what is in the string argument

– all process objects support the following methods:

• waitFor()

• exitValue()

• destroy()

try { Process child1 = Runtime.getRuntime().exec(ā€œ/bin/lsā€); InputStream in = child1.getInputStream(); // echo output of ā€˜ls’ child1.waitFor(); System.out.println(ā€œChild exited with ā€œ + child1.exitValue()); } catch(IOException e) { System.err.println(e); }

New Subtopic: ThreadingNew Subtopic: Threading

• Up till now, we’ve viewed processes as

– unit of resource ownership

– unit of scheduling (dispatching)

• Consider a Server Process:

• Up till now, we’ve viewed processes as

– unit of resource ownership

– unit of scheduling (dispatching)

• Consider a Server Process:

Server

LOCAL

OS

FILE REQUEST(S)FILE REQUEST(S)

FILESFILES

read(), write()

SYSTEM CALL

(BLOCKING)

SYSTEM CALL

(BLOCKING)

ā€œBOTTLENECKā€:

The Server Process

has a single ā€œthread

of executionā€

Threading IntroductionThreading Introduction

• Observation: multiple threads within a process?

– each has its own PC, stack, etc. (register context)

– schedulable

• Distinguishing Threads & Processes

– PROCESS: memory space

– PROCESS: ā€œownsā€ resources

– PROCESS:ā€big PCBā€

– THREAD:shared memory with other threads

– THREAD: per-thread static storage for local variables

– THREAD: execution state (READY, RUNNING, etc) & execution stack

• Threads are ā€œlightweight processesā€

• Resource-ownership --> Processes

• Scheduling --> Threads

• Observation: multiple threads within a process?

– each has its own PC, stack, etc. (register context)

– schedulable

• Distinguishing Threads & Processes

– PROCESS: memory space

– PROCESS: ā€œownsā€ resources

– PROCESS:ā€big PCBā€

– THREAD:shared memory with other threads

– THREAD: per-thread static storage for local variables

– THREAD: execution state (READY, RUNNING, etc) & execution stack

• Threads are ā€œlightweight processesā€

• Resource-ownership --> Processes

• Scheduling --> Threads

Threading ModelsThreading Models

1 Process 1 Thread

1 Process 1 Thread

1 Process Multiple Threads

1 Process Multiple Threads

Multiple Processes Multiple Threads/Process

Multiple Processes Multiple Threads/Process

Multiple Processes 1 Thread/Process

Multiple Processes 1 Thread/Process

Thread ImplementationThread Implementation

• Kernel-Level Threading (KLT)

– OS Kernel does all scheduling/management of threads

– thread switching is privileged instruction

• Advantages

– OS Kernel can take advantage of multiple CPUs

– OS Kernel can block threads without blocking processes

• Disadvantages

– switching overhead

• NEAT IDEA: kernel threads as interrupts

• Kernel-Level Threading (KLT)

– OS Kernel does all scheduling/management of threads

– thread switching is privileged instruction

• Advantages

– OS Kernel can take advantage of multiple CPUs

– OS Kernel can block threads without blocking processes

• Disadvantages

– switching overhead

• NEAT IDEA: kernel threads as interrupts

Multithreading ModelsMultithreading Models

• When OS supports BOTH user and kernel threads, there are

a few possibilities for implementing multithreading:

• Many-to-One

– all user threads map to one kernel thread

– pros/cons?

• One-to-One

– each user thread maps to its own kernel thread

– pros/cons?

• Many-to-Many

– user threads ā€œmultiplexedā€ to <= # of kernel threads

– pros/cons?

• When OS supports BOTH user and kernel threads, there are

a few possibilities for implementing multithreading:

• Many-to-One

– all user threads map to one kernel thread

– pros/cons?

• One-to-One

– each user thread maps to its own kernel thread

– pros/cons?

• Many-to-Many

– user threads ā€œmultiplexedā€ to <= # of kernel threads

– pros/cons?

Threading in Java (2 approaches)Threading in Java (2 approaches)

• Define new class that extends Thread class

– default technique assumed by book, and by us

• Define class that implements Runnable interface

– ā€œinterface:ā€ a promise by a class to offer support for a collection of methods.

– this is preferred when we need to make an object of a class that is already an

extension of some parent execute inside a thread. (e.g. Applet)

• Define new class that extends Thread class

– default technique assumed by book, and by us

• Define class that implements Runnable interface

– ā€œinterface:ā€ a promise by a class to offer support for a collection of methods.

– this is preferred when we need to make an object of a class that is already an

extension of some parent execute inside a thread. (e.g. Applet)

public class First { public static void main(String args[] ) { Worker1 runner = new Worker1(); runner.start(); System.out.println(ā€œI’m a main threadā€); } }

class Worker1 extends Thread { public void run() { System.out.println(ā€œI’m a worker threadā€); } }

Another View of Multithreading & PC Another View of Multithreading & PC

public class Printer extends Thread { private int extra; public Printer (int x) {extra = x;} public void run( ) { int y = extra + 1; System.out.println(ā€œTotal is ā€ + y); }

} SOURCE CODESOURCE CODE

public class Test { public static void main () { (new Printer(1)).start(); (new Printer(2)).start(); } }

MOV @x,@extra JMP (exit) LOD r1,@extra INC r1, MOV r1,@y CALL (println) JMP (exit)

MACHINE CODEMACHINE CODE

Printer1 PCPrinter1 PC Printer1 PCPrinter1 PC

Printer2 PCPrinter2 PC Printer2 PCPrinter2 PC Printer1 PCPrinter1 PC Printer2 PCPrinter2 PC Printer2 PCPrinter2 PC Printer2 PCPrinter2 PC

Printer1 PCPrinter1 PC

Printer2 PCPrinter2 PC

Printer1 PCPrinter1 PC

Printer2 PCPrinter2 PC

Printer1 PCPrinter1 PC Printer1 PCPrinter1 PC