Processes and Threads Flashcards
How do processors execute instructions?
1) Fetches the instructions from memory 2) Decodes them (to determine type and operands) 3) Executes them This is repeated for subsequent instructions
What does each process have?
An associated address space containing executable program, its data and its stack
How do processes work in multiprogramming?
Each process has its own logical program counter (there is only one physical program counter), so when a process runs its logical program counter is loaded into the physical program counter.
What events cause processes to be created?
1) System initialisation
2) Execution of a process creation system call by a running process
3) A user request to create a new process
4) Initiation of a batch job
How are processes that are not created at boot created?
They must be created by other processes issuing system calls to create processes to help out
CreateProcess
Windows call that both creates a new process and loads correct program
How does a process terminate and are these voluntary or involuntary?
1) Normal exit (voluntary) 2) Error exit (voluntary) 3) Fatal error exit (involuntary) 4) Killed by another process (involuntary)
Give an example of a fatal error
Seg fault Divide by 0 error Bus error
How many parents does a process have?
1, but may have 0-many children
How are signals sent to a process group?
Signals are sent to all members of an associated process group. Each process can choose to catch, ignore or take the default action (be killed) with regard to the signal
What is the default action of a process when receiving a signal?
To be killed
Explain the concept of process hierarchy in Windows
There is no concept of process hierarchy in Windows. All processes are created equal. The parent process is however given a token called a handle, which can be used to control the child process. However, this can be passed to other processes, invalidating the hierarchy.
Can parent processes in UNIX disinherit their children?
No
All processes in a system belong to what?
A single tree (inheritance) with init at the root
Explain the concept of process hierarchy in UNIX
Association exists between parent and child processes. A process and all of its descendants form a process group
How does init work?
This is present in the boot image. It reads in a file with the number of terminals and forks off one process per terminal. These terminals wait for a user to login. If successful, login process executes a shell to accept commands, including starting more processes
How many processes does a program running twice count as?
2
How does a process run a new program?
The process forks off a child process, which executes execve to change memory image and run a new program. By doing this, the child can change file descriptors between the new program and the execve to achieve redirection of standard input, output and error.
Are background processes associated with particular users?
No
Why shouldn’t programs be built with assumptions about timing?
Multiprogramming will cause inconsistencies in this if programs are temporarily stopped to let others run
What states can a process exist in?
1) Running: currently using CPU 2) Ready: ready to run, but waiting on another process currently using CPU 3) Blocked: unable to run until some external event happens
What are the transitions between the states that a process can exist in?
1) Running to Blocked: Process realises that it is dependent on some external information in order to continue execution 2) Running to Ready: Caused by scheduler deciding to allocate time to another process 3) Ready to Running: Caused by scheduler allocating time to this process 4) Blocked to Ready: External event occurs meaning that process is able to logically run. If no process is running, transition 3 will be automatically triggered
What states can a thread exist in?
1) Ready 2) Running 3) Blocked (same as process)
Why is there no protection between threads?
1) Not possible 2) Shouldn’t be necessary: a process is always owned by a single user and threads should be made to cooperate
What items are shared by everything within one process (ie. every thread of a process shares what)?
1) An address space 2) Global variables 3) Open files 4) Child processes 5) Pending alarms 6) Signals and signal handlers 7) Accounting information
What items within a process are specific to individual threads (assume that the process is multithreaded)?
1) Program counter: keeps track of which instruction to be executed next 2) Registers: hold current working variables 3) Stack: execution history with one frame for each procedure called but not yet returned from 4) State: running, ready, blocked
How does multithreading work on a single CPU?
The same way as multiprogramming. Only one thread can run at a time, so CPU implements pseudoparallelism through the technique of rapid switching between threads
What does a thread do when it’s finished its work?
It exits by calling the appropriate library procedure (eg. thread_exit)
Can threads exist in a hierarchical structure?
Yes; however, often threads are created equal.
What two concepts is the thread model based on?
1) Resource grouping 2) Execution
Does a thread have to execute in a process?
Yes
What’s the difference between a thread and a process?
Threads are entities scheduled for execution on the CPU, while processes are groups of related threads
What are the advantages of threads?
Allows multiple executions to take place in same environment mostly independently of each other Do not offer performance gain with just one CPU, but threads can overlap saving time if significant I/O is involved (not needing to wait while blocked) Real parallelism on multicore processors
How are new threads usually created?
A single thread calls a library procedure to create more threads with them: thread_create
What problems are introduced with multithreading?
1) If a parent process is multithreaded and creates a child process, does each thread have access to the child? 2) Threads share data structures. What happens if one thread closes a file that another thread is trying to read from? 3) Multiple space allocation - if all threads try to allocate more memory for the same thing
What is an advantage to having multiple threads as compared to having multiple processes?
Since threads do not have resources, they are easier to create and destroy than processes
Compare single threaded processes, multithreaded processes and finite state machines
Single threaded: No parallelism, blocking system calls Multithreaded: Parallelism, blocking system calls Finite state machines: Parallelism, non blocking system calls
What modes can threads be executed in?
User or kernel modes
What are the advantages to threads executed in user space?
1) Mode switch not required
2) Thread scheduling can be application specific
Explain the process of interrupt handling and scheduling
1) Hardware stacks program counter 2) Hardware loads new program counter from interrupt vector 3) Assembly language procedure saves registers 4) Assembly language procedure sets up a new stack 5) C interrupt service runs 6) Scheduler decides which process to run next 7) C procedure returns to assembly code 8) Assembly language procedure starts up new current process
Explain the difference between implementing threads in kernel and user modes
No runtime system needed in kernel Kernel mode has one thread table instead of one for each process
How do hybrid implementations of threads work?
Kernel level threads are used and user level threads are multiplexed onto some or all of these threads Kernel is only aware of kernel level threads User level threads are created and destroyed at minimal cost
What are some disadvantages to user level threads?
1) If one thread blocks, the entire process will block
2) Cannot take advantage of multiprocessors
What is the problem with changing user-level thread system calls to nonblocking?
This would require changes to the OS, which would invalidate one of the reasons why user-level threads are advantageous. Changes would need to be made to multiple other programs.
What is the problem with creating a wrapper around user-level threads to prevent blocking?
Parts of the system call library must be rewritten
Explain the concept of a jacket/wrapper with regard to user-level thread blocking
Code, referred to as a wrapper/jacket (eg. select in UNIX) is placed around the system call to check if it will read or block. If it blocks, the system call will not be executed, and other threads will start. This thread will be returned to later.
Explain how page faults affect user-level threads
If a program tries to access an instruction that is not in main memory, the OS will need to go to the disk to retrieve this instruction. The kernel, not knowing about threads, will block the entire process until disk I/O is complete - even though other threads might be runnable.