Processes and Threads Flashcards

1
Q

Process

A
  • The thing which represents our work to the system.
  • Sometimes referred to as a heavyweight process.
    *An instance of a program in execution.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Instance of a Program in Execution.

A
  • May be more than one version of the same program running at the same time. (Hopefully sharing the code.)
    Each instance has resource limitations, security information - rights, capabilities etc.
  • Includes code, data, connections (to files, networks, other processes), access to
    devices.
  • It needs the processor to run.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Two Parts to a Process

A
  1. Resources, the things the process owns (may be shared). Also, information about the process.
  2. What the process is doing - the streams of execution.
    * Traditional processes had resources and a single current location. e.g., traditional UNIX.
    The resource part is called a task or a job. The location part is commonly called a thread.
    * Most operating systems now provide support to keep these parts separate, e.g., Linux, Solaris, Windows, macOS.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Threads

A
  • Sometimes referred to as lightweight processes. A sequence of instructions being
    executed when there is no external intervention.
  • Multiple threads of a process, share the process resources – but have their own thread ID, PC, registers, stack
  • Easier to create than a process. They provide a nice encapsulation of a problem within a process rather than multiple processes.
  • Easier to switch between threads than between processes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Typical Uses of Threads

A

*Splitting work across processors (shared memory multiprocessor, multiple cores)
*Added responsiveness (handle user input while still finishing another
function)
*Controlling and monitoring other threads
*Server applications
*Can help with program abstraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

User-Level (or Green Threads)

A
  • The OS only sees one thread per process.
  • The process constructs other threads by user-level library calls or by hand.
  • User-level control over starting and stopping threads.
  • Usually a request is made to the OS to interrupt the process regularly (an alarm clock) so that the process can schedule another thread.
  • The state of threads in the library code does not correspond to the state of the process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

System-Level Threads

A
  • The OS knows about multiple threads per process.
  • Threads are constructed and controlled by system calls.
  • The system knows the state of each thread.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

User-Level Thread Advantages

A

*Works even if the OS doesn’t support threads.
* Just a normal library procedure call.
* No switch into kernel mode (this saves time).
*Control can be application specific.
*Easier to switch between - saves processor mode changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

System-Level Thread Advantages

A

*Each thread can be treated separately.
*A thread blocking in the kernel doesn’t stop all other threads in the same
process.
*On a multiprocessor (including multi-core) different threads can be
scheduled on different processors (only if OS knows about the threads).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Jacketing

A
  • A blocking system call has a user-level jacket.
  • The jacket checks to see if the resource is available, e.g., device is free.
  • If not another thread is started.
  • When the calling thread is scheduled again (by the thread library) it once
    again checks the state of the device.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Solaris (versions < 9)

A

Had both user-level and system-level threads.
Task | Task | Task
⧙ ⧙ ⧙ (User level threads)
∘ ∘ ∘ (Lightweight Processes)

⧙ ⧙ ⧙ (Kernel threads)
Kernel
\ | /

CPU

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Original Linux Threads (Before 2.6)

A

Thread creation is done through clone() system call.
Clone() - makes a new process
* Shares memory address space, open files (actually descriptors), signal handlers
From one point of view original Linux threads were processes - but they shared
all resources and hence the advantages of threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Original LINUX Threads and POSIX

A
  • Can’t be set to schedule threads according to priority within a process
  • each thread is scheduled independently across all threads/processes in the system.
  • Can’t send a signal to the whole process.
  • Ordinary system calls e.g., read, were not cancellation points.
  • Starting a new program in one thread doesn’t kill the other threads in the same process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pthread Programming

A

POSIX threads or pthreads are used in UNIX type operating systems.
* Use “man pthread_create” to get the manual page SYNOPSIS
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
* the #include needs to be added to the top of your source file
* going through the parameters
* thread is a pointer to a pthread_t type - this is where information about the thread is
stored, we need this to join to the thread when we wait for it to finish
* attr is a pointer to pthread attributes, to use the default we put NULL
* start_routine is the name of the function which the thread runs
* arg is a pointer to arguments/parameters used by the function</pthread.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Making Sure a pthread Has Finished

A

To keep the compiler happy our thread function must be of the type:
void (start_routine) (void *)
* The start_routine is a pointer to a function. In C this is exactly the same as a function name.
* The function returns a pointer to a void (i.e., it can return a pointer to anything, we can cast it).
* The function accepts one parameter which is also a pointer to a void (i.e., we can make it point to anything).
void *the_function(void *params) {…}
* We run this in a thread by calling
pthread_create(&thread_info, NULL, the_function, (void *)&args)
* Where args is a pointer to the values we want to send to the function, and thread_info is a pthread_t.
pthread_t thread_info;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Thread Function Parameters

A
  • We can only pass one pointer to the thread, so if we need to pass more than one value we normally use a struct and package up the parameters we want to pass.
  • Then we pass the address to the struct.
  • When the thread function gets called we unpackage the values.
  • Before calling
    struct values {
    int first;
    int second;
    };
    struct values args;
    args.first = 34; args.second = 97;
    pthread_create(&thread_info, NULL, the_function, (void *)&args);
  • In the function
    void *the_function(void *params) {
    struct values args = *(struct values *)params;
    // then we can use args.first and args.second
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Segmentation Fault

A
  • Each region has its own set of permissions. For example,
    the code segment is usually marked as read-only to prevent accidental modification of the program code.
  • Situation where we may get a segmentation fault:
    1. Accessing uninitialized memory
    2. Dereferencing a null pointer
    3. Stack overflow.
    4. Accessing memory outside of its allocated space
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Benefits of Memory Mapping

A
  • Efficient use of physical memory - By allowing multiple processes to share the same physical memory, the operating system can use memory more efficiently.
  • Protection - Each process has its own virtual memory
    space, which provides protection against other processes accessing its memory.
  • Large address space - Virtual memory allows each process to have a larger virtual address space than is available in
    physical memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Virtual Memory

A

Virtual addresses can stored in virtual memory(disk)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Address Translation

A

Virtual address spaces(pages) can be stored in physical address space(frames).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Matrix Multiplication (Dot Product)

A

[■(a_1,1&a_1,2&a_1,3@a_2,1&a_2,2&a_2,3 )]∙[■(b_1,1&b_1,2@b_2,1&b_2,2@b_3,1&b_3,2 )]=[■(a_1,1 b_1,1+a_1,2 b_2,1+a_1,3 b_3,1&a_1,1 b_1,2+a_1,2 b_2,2+a_1,3 b_3,2@a_2,1 b_1,1+a_2,2 b_2,1+a_2,3 b_3,1&a_2,1 b_1,2+a_2,2 b_2,2+a_2,3 b_3,2 )]

22
Q

Matrix Multiplication Rule

A

A∙B=AB
↓ ↓ ↓
m×n n×p m×p
(Dimensions of A and B)

23
Q

Process Control Block (PCB)

A

Information associated with each process (also called task control block)
Where the OS can find all the information it needs about a process.
* Process identified and managed via a process identifier (pid)
* Process state – running, waiting, etc.
* Program counter – location of instruction to next execute
* CPU registers – contents of all process-centric registers
* CPU scheduling information- priorities, scheduling queue pointers
* Memory-management information – memory allocated to the process
* Accounting information – CPU used, clock time elapsed since start, time
limits
* I/O status information – I/O devices allocated to process, list of open files
Doesn’t have to be kept together (different information is required at different times)

24
Q

PCB Structure

A

Process Number

Program Counter

Registers

Memory Limits

Lists of Open Files

25
Q

UNIX PCBs

A

PCB →|→|→User Structure | Kernel Stack
↓ (resident tables) ↘ | | (System data Structure)
| | Stack
↳ | |Data
Text Structure→|→| Text
(User Space)
(Swappable Process Image)

26
Q

Windows NT PCBS -Executive Process Block

A

Executive Process Block (EPROCESS) includes:
* KPROCESS and PEB (Process Environment Block)
* pid and ppid (the ppid is not visible to Win32)
* file name of program
* window station - the screen or remote terminal
* exit status
* create and exit times
* links to next process
* memory quotas
* memory management info
* Ports for exceptions and debugging
* Security information

27
Q

Kernel Process Block (KPROCESS)

A

Kernel Process Block (KPROCESS) includes info the kernel needs to schedule threads
* Kernel and user times.
* Pointers to threads.
* Priority information.
* Process state
* Processor affinity

28
Q

Process Environment Block (PEB)

A

Process Environment Block (PEB) includes info which needs to be writable
in user mode
* image info: base address, version numbers, module list
* process parameter information (e.g. command line)

29
Q

Process Representation in LINUX

A

Represented by the C structure task_struct
pid t_pid; /* process identifier /
long state; /
state of the process /
unsigned int time_slice /
scheduling information */
struct task_struct parent; / this process’s parent /
struct list_head children; /
this process’s children */
struct files_struct files; / list of open files */
struct mm_struct mm; / address space of this process */

30
Q

Process State

A
  • As a process executes, it changes state
  • New: The process is being created
  • Running: Instructions are being executed
  • Waiting: The process is waiting for some event to occur
  • Ready: The process is waiting to be assigned to a processor
  • Terminated: The process has finished execution
31
Q

Process Creation

A

Different methods of creating processes
* create process system call - takes a program name or a stream
with the program data
* copy process system call
* create a new terminal session

32
Q

Process Creation Requirements

A

Requires
* find a spare or create a new PCB
* mark it “new”
* generate a unique identifier
* get some memory or fill in the page table entries
* set up PCB fields with initial values
* set prority, resource limits
* when all set up change the state to “ready”
* this could be done by inserting into a queue of runnable processes
* Some OSs carefully allocate resources before a process runs (this prevents
deadlock later)
* Others leave these to the process to collect as it runs

33
Q

Process Creation - Address Space

A
  • Child duplicate of parent - each one has its own copy of any data.
  • Child has a new program loaded into it
    UNIX examples
  • fork() system call creates new process
  • exec() system call used after a fork() to replace the process’ memory space
    with a new program
  • Parent process calls wait()waiting for the child to terminate
34
Q

fork() System Call

A
  • The UNIX fork call duplicates the currently running process.
  • parent process - the one which made the call
  • child process - the new one
  • Traditionally memory was duplicated
  • Share open files as well.
  • Open file information blocks will have
    the count of processes using them increased by one.
  • And shared memory regions.
  • Fork returns 0 in the child process and the child’s pid in the parent.
35
Q

exec() System Call

A
  • checks to see if the file is executable
  • saves any parameters in some system memory
  • releases currently held memory
  • loads the program
  • moves the saved parameters into the stack space of the new program
  • ready to run again

Fork used to copy the data memory of the process.
If the child is going to do an exec this is a waste of effort. Particularly bad with virtual memory.

36
Q

Solutions for Bad Memory

A
  1. copy on write
    * No copy is made at first.
    * The data pages of the parent process are set to read only.
    * If a write occurs the resulting exception makes a copy of the page for the other
    process – both copies are then marked writable.
  2. vfork
    * Trust the programmers to know what they are doing.
    * With vfork - parent process blocks until child finishes or calls exec.
    Copy on write is the predominant strategy.
    * It is used in many situations to improve throughput
37
Q

Runnable

A
  • On a single core, only one process/thread can run at a time.
  • Many may runnable - either running or ready.
38
Q

Pre-Emptive Multitasking

A
  • A clock interrupt causes the OS to check to see if the current thread
    should continue
  • Each thread has a time slice
  • Advantages
  • control
  • predictability
  • Disadvantages
  • critical sections
  • efficiency
39
Q

Cooperative Multitasking

A
  • Two main approaches
    1. a process yields its right to run
    2. system stops a process when it makes a system call
    This does NOT mean a task will work to completion without allowing another
    process to run
40
Q

Context Switch

A

The change from one process running to another one (or from a process/thread running to handling an interrupt) running on the same processor is usually referred to as a “context switch”.
* The context changes as the process executes.
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch

41
Q

Context

A
  • registers
  • memory - including dynamic elements such as the call stack
  • files, resources
  • but also, things like caches, TLB values - these are normally lost
  • Context of a process represented in the PCB
  • The context changes as the process executes.
42
Q

Back to Running

A

State transition
* Must store process properties so that it can restart where it was.
* If changing processes the page table needs altering.
* Rest of environment must be restored.
* If changing threads within the same process simply restoring registers might
be enough.
* Some systems have multiple sets of registers which means that a thread
change can be done with a single instruction

43
Q

Waiting

A
  • Processes need when they start:
  • memory
  • data from files or devices
  • Waiting processes must not be allowed to unnecessarily consume
    resources
  • state is changed to waiting
  • more than one type of waiting state
  • short wait
  • long wait
  • removed from the ready queue
  • probably entered on a queue for whatever it is waiting for
  • When the resource becomes available
  • state is changed to ready
  • removed from the waiting queue
  • put back on the ready queue
44
Q

Suspended

A
  • Another type of waiting
  • ctrl-z in some UNIX shells
  • Operators or OS temporarily stopping a process
  • allows others to run to completion more rapidly
  • or to preserve the work done if there is a system problem
  • or to allow the user to restart the process in the background etc.
  • Suspended processes are commonly swapped out of real memory.
  • This is a state which affects the process not individual threads.
45
Q

Why we don’t use Java suspend()

A
  • If dealing with threads in Java we don’t use these deprecated methods:
  • suspend() freezes a thread for a while.
    resume() releases the thread and it can start running again.
  • suspend() keeps hold of all locks gathered by the thread.
  • If the thread which was going to call resume() needs one of those locks
    before it can proceed we get stuck.
46
Q

Why we don’t use stop()

A
  • stop() kills a thread forcing it to release any locks it might have.
  • We will see where those locks come from in later lectures.
  • The idea of using locks is to protect some shared data being manipulated
    simultaneously.
  • If we use stop() the data may be left in an inconsistent state when a new thread accesses it.
47
Q

Waiting in Some Unixes

A
  • A process waiting is placed on a queue.
    (originally used to scan whole process table)
  • The queue is associated with the hash value of a kernel address
    (waiting or suspended processes may be swapped out)
  • when the resource becomes available
  • all things waiting for that resource are woken up
  • (may need to swap the process back in)
  • first one to run gets it
  • if not available when a process runs the process goes back to waiting
48
Q

Process Termination

A
  • Process executes last statement and then asks the operating system to delete it using the exit() system call.
  • Returns status data from child to parent (via wait())
    pid = wait(&status);
  • Parent may terminate the execution of children processes using the abort()
    system call. Some reasons for doing so:
  • Child has exceeded allocated resources
  • Task assigned to child is no longer required
  • The parent is exiting, and the operating systems does not allow a child to continue if its parent terminates (cascading termination – initiated by OS)
49
Q

Process Termination Requirements

A
  • All resources must be accounted for
  • may be found in the PCB or other tables
  • reduce usage count on shared resources
  • if the process doesn’t tidy up then something else must
  • accounting information is updated
  • remove any associated processes
  • remove the user from the system
50
Q

UNIX Stopping

A
  • Usually call exit(termination status)
  • open files are closed - including devices
  • memory is freed
  • accounting updated
  • If no parent waiting (did not invoke wait() ) process is a zombie
  • If parent terminated without invoking wait() , process is an orphan
  • children get “init” as a step-parent
  • parent is signalled (in case it is waiting or will wait)
  • after the parent retrieves the termination status the PCB is freed