what are the different ways a CPU can execute its tasks?
sequential execution
concurrent execution
parallel execution
parallel and concurrent execution
each execution method is a different combination of parallel and concurrent
what are the different combinations of parallel and concurrent that applications can be executed with?
not parallel : not concurrent
parallel : not concurrent
not parallel : concurrent
parallel : concurrent
what combination of parallel and concurrent is SEQUENTIAL?
not parallel : not concurrent
what combination of parallel and concurrent is CONCURRENT?
not parallel : concurrent
what combination of parallel and concurrent is PARALLEL?
parallel : not concurrent
what is sequential execution?
Application processes and executes one task or subtask (job, process) at a time
Needs to complete its execution for another task to start
not efficient, old solution
what is concurrent execution?
application processes more than one task at the same time, but no two tasks are executed at same time instant.
Multiple tasks or subtasks appear to run in parallel
Takes advantage of the CPU time-slicing feature of the operating system → Takes share of time (managed by the operating system)
Runs part of a task then go to waiting state. While in the waiting state another task is running and so on → processes are in different states (eg. waiting, running) and can change states
what is parallel execution?
application processes and executes multiple tasks at the same time executing at the same time
Requires two or more CPUs/cores
eg. for 1 point in time, many tasks may be running
what is parallel and concurrent execution?
eg. multiple CPUs means parallel tasks can run, but within each CPU the tasks run concurrently
what is parallelism?
one application divided into subtasks that can be mapped to different CPUs, communication between the subtasks occurs
what is the difference between parallel execution and paralleism?
if the program not developed as parallel then cannot be run in parallelism as parallelism
for parallel execution applications don’t need to be specifically developed to suit it. parallel execution works as long as the tasks happening on each CPU are different at a given time
what is the difference between concurrency and parallelism?
concurrency | parallelism
tasks start/ run/ complete in overlapping time periods vs. tasks run at the same time
processes execute independently vs. simultaneously executing possibly related computations
dealing with lots of things vs. actively doing lots
not the same thing but they both need synchronisation.
what is a thread?
lightweight process, basic unit of CPU utilisation
what does a thread have?
a thread ID
a program counter
its own temporary data consisting of
a register set for data,
a stack to store variables
what do threads from the same process share?
a common memory space: made up of code section, data section, and other operating system resources (e.g. files, etc.).
how many threads does a traditional/ heavyweight process have
a single thread of control.
how do processes and threads relate?
processes are divided into threads, therefore, if a process has many threads it can perform more than one task at a time e.g. A web browser might have one thread that displays images or text while another thread retrieves data from the network.
when are applications single threaded or multithreaded?
applications are mainly multithreaded where many threads belonging to same process
what issues are there when threads share data?
data inconsistency - 2 threads sharing the same data and trying to update at same time, which is correct
in what situation would a single application need to perform many similar tasks?
A web server accepts client requests for web pages, images, sound etc.
A busy web server may have several clients concurrently accessing it.
what are the different methods are there to manage a single application that is required to perform many similar tasks?
a single threaded process using process creation
multithreaded server
how does a single threaded process work?
run the server as a single process used for accepting and servicing requests
Only manage one client at a time (might have to wait a very long time for its request to be serviced = not efficient
uses the process creation method
what is the Process-creation method?
When a server receives a request, it creates a separate process to service that request.
process creation is time consuming and resource intensive.
what is the purpose of multithreading?
The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program aiming at maximising CPU time utilisation.
what is a multithreaded program?
A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.
how does a Multithreaded server work?
When a request is made, rather than creating another process, the server creates a new thread to service the request and resume listening for additional requests.
what other ways are multithreaded servers used?
remote procedure call, doing RPC using a multithreaded server allows interprocess communication and lets us service concurrent requests by creating a new thread after a message is received
multithreaded kernel where each thread does a specific task e.g. manage devices or managing memory.
what are the benefits of threads/ multithreaded server?
responsiveness
resource sharing
economy
scalability
[ra ra, emerging servers]
what is the responsiveness benefit?
Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation. a single-thread app would be unresponsive until user operation complete
eg. seeing what is displayed in web browser whilst in background retrieving data
what is the resource sharing benefit?
Processes can only share resources through techniques such as shared memory and message passing.
Threads share the memory and the resources of the process to which they belong by default - allows an application to have several different threads of activity within the same address space.
what is the economy benefit?
Allocating memory and resources for process creation is costly. Because threads share the resources of the process to which they belong, it is more economical (faster) to create and context-switch new threads rather than create new processes
what is the scalability benefit?
as the system gets larger e.g. multiprocessor architecture, can run faster and the benefits are better
threads may be running in parallel on different processing cores
a single-threaded process can run on only one processor, regardless of how many are available.
how many states can a thread be in at a given point in time?
only 1 state at a time
what are the different thread states?
new
runnable
blocked
waiting
timed waiting
terminated
what is the thread state diagram/ thread lifecycle?
what is the new thread state?
A thread that has not yet started.
When a thread is created, it’s in NEW state. At this point, thread is not alive and it’s a state internal to Java programming. It remains in this state until the program starts the thread using it’s start() method.
what is the runnable thread state?
A thread executing in the Java virtual machine.
Calling start() method on thread puts it in RUNNABLE state. At this point, execution control is passed to thread scheduler to finish it’s execution.
The OS gives a small amount of processor time to each thread – called a quantum or timeslice – with which to perform its task.
what is the blocked thread state?
A thread that is blocked waiting for a monitor lock.
A RUNNABLE thread transitions to the BLOCKED state when it attempts to perform a task that cannot be completed immediately and it must temporarily wait until that task completes
runnable → blocked = issue io request/ enter synchronized statement
blocked → runnable - inturrupt/ acquire lock/ io complete
what is the waiting thread state?
A thread that is waiting indefinitely for another thread to perform a particular action before it can continue its current action.
A thread can be put in waiting state for various reasons.
runnable → wait = wait()
wait → runnable = notify()/ notifyAll()
what is the timed waiting thread state?
A thread that is waiting for another thread to perform an action for up to a specified waiting time.
A RUNNABLE thread can transition to the TIMED WAITING state if it provides an optional wait interval
runnable → timedWait = sleep(millis)/ wait(millis)
timedWait → runnable = notify()/ notifyAll()/ waitIntervalExpires()
returns to the RUNNABLE state when it’s notified by another thread or when the timed interval expires – whichever comes first.
what is the terminated thread state?
A thread that has exited.
A thread enters the TERMINATED state (sometimes called the dead state) when it successfully completes its task or otherwise terminated due to any error or even it was forcefully killed.
runnable → terminated = taskExpires()