threads, processes, and jobs - dalhousie universitynzeh/teaching/2132/slides/processes.pdf · the...
TRANSCRIPT
![Page 1: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/1.jpg)
CSCI 2132: Software Development
Threads, Processes, and Jobs
Norbert Zeh
Faculty of Computer Science Dalhousie University
Winter 2019
![Page 2: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/2.jpg)
Processes
Recall:
• A program is a file storing executable code
• A process executes a program
Process occupies memory:
• Code (executable machine code)
• Data (static data)
• Heap (used for dynamic memory allocation)
• Stack (local variables of subroutines, supports recursion)
We will learn more about these types of memory in the context of C programming.
![Page 3: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/3.jpg)
Threads
A thread executes the instructions in a program one at a time (sequentially).
Thread state:
• Program counter (memory address of next instruction to execute)
• Register contents
Traditionally, one thread per process.
Modern OSs: one process can have many threads (utilize multicore CPUs). Different processes/threads can execute the same code in memory.
![Page 4: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/4.jpg)
Threads
A thread executes the instructions in a program one at a time (sequentially).
Thread state:
• Program counter (memory address of next instruction to execute)
• Register contents
Traditionally, one thread per process.
Modern OSs: one process can have many threads (utilize multicore CPUs). Different processes/threads can execute the same code in memory.
![Page 5: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/5.jpg)
Threads
A thread executes the instructions in a program one at a time (sequentially).
Thread state:
• Program counter (memory address of next instruction to execute)
• Register contents
Traditionally, one thread per process.
Modern OSs: one process can have many threads (utilize multicore CPUs). Different processes/threads can execute the same code in memory.
![Page 6: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/6.jpg)
The Cost of Processes and Threads
Generally, there are many more processes/threads than CPU cores.
OS allocates “times slices” of CPU cores to threads/processes.
Processes store more admin information than threads.
Process creation and process switching is more costly than thread creation and thread switching.
![Page 7: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/7.jpg)
Process Control Block (PCB)
Created by OS when a process starts
Includes:
• Process identifier (PID)
• Program counter
• Resources allocated to process (e.g., memory, open files)
• Process ownership (user and group)
• Process state (running, sleeping, pre-empted, created, zombie)
![Page 8: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/8.jpg)
Process Creation
Every process is created by another process and becomes its child process.
One exception: the root process init (PID=1)
This gives us a process tree
![Page 9: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/9.jpg)
Process Tree
root 1 0 0 2018 ? Ss 15:23 /usr/lib/systemd/systemd ""... root 2382 1 0 2018 ? Ss 5:51 /usr/lib/systemd/systemd-journald root 2414 1 0 2018 ? Ss 0:00 /usr/sbin/lvmetad root 4575 1 0 2018 ? S<sl 1:01 /sbin/auditd polkitd 4598 1 0 2018 ? Ssl 0:40 /usr/lib/polkit-1/polkitd dbus 4600 1 0 2018 ? Ssl 3:18 /usr/bin/dbus-daemon root 4631 1 0 2018 ? Ssl 3:12 /usr/sbin/NetworkManager root 4633 1 0 2018 ? Ss 1:45 /usr/lib/systemd/systemd-logind chrony 4691 1 0 2018 ? S 0:17 /usr/sbin/chronyd root 5022 1 0 2018 ? Ss 1:33 /usr/sbin/sshd root 5027 1 0 2018 ? Ss 9:54 /usr/sbin/cupsd root 5031 1 0 2018 ? Ssl 242:58 /usr/lib/gitlab-runner/gitlab-runner ""... root 15934 5022 0 22:09 ? Ss 0:00 sshd: nzeh [priv] ""... nzeh 15937 15934 0 22:09 ? S 0:00 sshd: nzeh@pts/29 ""... nzeh 15938 15937 0 22:09 pts/29 Ss 0:00 -bash
![Page 10: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/10.jpg)
Process Creation How the Shell Starts a Program
Parent process running shell
Child process running program
Child process running shell
Parent process waiting for child
Child process terminates
Parent process awakens
Duplicate: fork()
Wait for child: wait()
Differentiate: exec()
Terminate: exit()
Signal
![Page 11: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/11.jpg)
Foreground and Background Processes
Foreground process controls the terminal
Background process cannot read from keyboard but can print to terminal
![Page 12: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/12.jpg)
Job and Process Control
Job control = shell functionality for managing processes
Print jobs (processes started from the current shell)
Print processes
Start a process in background
$ jobs
$ ps
$ xterm &
![Page 13: Threads, Processes, and Jobs - Dalhousie Universitynzeh/Teaching/2132/Slides/processes.pdf · The Cost of Processes and Threads Generally, there are many more processes/threads than](https://reader035.vdocuments.net/reader035/viewer/2022071100/5fd8e62acfe244641f3c2a13/html5/thumbnails/13.jpg)
Job and Process ControlSuspend a process
Put suspended job in background
Resume suspended job in foreground
Terminate a job or process
Ctrl-Z
$ bg
$ fg
$ bg %job
$ fg %job
$ kill pid$ kill %job