Process Management: Threads

5.0(1)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/40

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

41 Terms

1
New cards
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
2
New cards
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
3
New cards
what combination of parallel and concurrent is SEQUENTIAL?
not parallel : not concurrent
4
New cards
what combination of parallel and concurrent is CONCURRENT?
not parallel : concurrent
5
New cards
what combination of parallel and concurrent is PARALLEL?
parallel : not concurrent
6
New cards
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
* 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
7
New cards
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
\
\
* 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
8
New cards
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
* 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
9
New cards
what is parallel and concurrent execution?
* eg. multiple CPUs means parallel tasks can run, but within each CPU the tasks run concurrently
* eg. multiple CPUs means parallel tasks can run, but within each CPU the tasks run concurrently
10
New cards
what is parallelism?
* one application divided into subtasks that can be mapped to different CPUs, communication between the subtasks occurs
* one application divided into subtasks that can be mapped to different CPUs, communication between the subtasks occurs
11
New cards
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
12
New cards
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.
13
New cards
what is a thread?
lightweight process, basic unit of CPU utilisation
14
New cards
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
15
New cards
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.).
16
New cards
how many threads does a traditional/ heavyweight process have
\
* a single thread of control.
17
New cards
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.
18
New cards
when are applications single threaded or multithreaded?
applications are mainly multithreaded where many threads belonging to same process
applications are mainly multithreaded where many threads belonging to same process
19
New cards
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
20
New cards
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.
21
New cards
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
22
New cards
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
23
New cards
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.
24
New cards
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.
25
New cards
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.
26
New cards
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.
 

* 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.
27
New cards
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.
28
New cards
what are the benefits of threads/ multithreaded server?
responsiveness

resource sharing

economy

scalability

\
\[ra ra, emerging servers\]
29
New cards
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
30
New cards
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.
31
New cards
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
32
New cards
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.
33
New cards
how many states can a thread be in at a given point in time?
only 1 state at a time
34
New cards
what are the different thread states?
new

runnable

blocked

waiting

timed waiting

terminated
35
New cards
what is the thread state diagram/ thread lifecycle?
knowt flashcard image
36
New cards
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.
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.
37
New cards
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.
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.
38
New cards
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
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$$
39
New cards
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()
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()$$
40
New cards
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.
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.
41
New cards
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()
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()$$