0% found this document useful (0 votes)
18 views22 pages

Lecture3 Process Management

Uploaded by

mupo528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views22 pages

Lecture3 Process Management

Uploaded by

mupo528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Process Management

COMP304
Operating Systems (OS)

Hakan Ayral
Lecture 3

Operating Systems-COMP 304 (original slides by Didem Unat) 1


Outline
• Process Concepts
• Process State
• Context Switch
• Schedulers
• Process Creation and Termination

• Reading
– Chapter 3.1-3.4 from textbook – Very Good!
– Linux Kernel Development – Chapter 3

Operating Systems-COMP 304 2


Process
• Process – a program in execution;

• A program is passive entity stored on disk (executable file),


process is active
– A program becomes a process when executable file loaded
into memory
• Terms job, task and process are almost interchangeably used

• Execution of a program starts via GUI mouse clicks, command


line entry of its name, etc
• One program can have several processes
– Consider multiple users executing the same program
– Ex. Multiple browsers running at the same time

Operating Systems-COMP 304 3


Process
• The program code, also called text Process’ Address Space
section
– Binary code
• Current activity includes program
counter and other processor registers
• Stack containing temporary data
– Function parameters, return addresses,
local variables
• Data section containing global
variables
• Heap containing memory dynamically
allocated during run time

Operating Systems-COMP 304 4


Concurrent Execution
• OS implements an abstract machine per process

• Multiprogramming enables
– N programs to be space-muxed in executable memory, and
time-muxed across the physical machine processor.

• Result: Have an environment in which there can be


multiple programs in execution concurrently*, each
as a process
– Concurrently means processes appear to execute
simultaneously, they all make some progress over time

Operating Systems-COMP 304 5


Process State
• As a process executes, it changes its state
– new: The process is being created
– running: Instructions are being executed
– waiting: The process is waiting for some event (e.g., IO) to
occur
– ready: The process is waiting to be assigned to a CPU
– terminated: The process has finished execution

Operating Systems-COMP 304 6


Transition between Process States

• Process transitions from one state to another


• An animation for process states
– https://www.youtube.com/watch?v=Y3mQYaQsrvg
Operating Systems-COMP 304 7
Process Context
• Also called process control block

• When an interrupt occurs, what information OS


needs to keep around so that we can reconstruct
process’s context as if it was never interrupted its
execution?

Operating Systems-COMP 304 8


Process Control Block (PCB)
Keeps the process context
Process control block (PCB)
• Process state – running, waiting, etc
• Program counter – location of instruction to
next execute
• CPU registers – contents of all process 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

Operating Systems-COMP 304 9


Context Switch
• OS needs to store and restore the context of a process
– So that execution of the process can be resumed from the same point at a
later time
– This is called context switch

• When does OS switch context?


– In case of an interrupt
– When process’s time is up
• Even though process has still some work to do
– When a process terminates

Operating Systems-COMP 304 10


Process P0 operating system Process P1

executing Interrupt

Save state into PCB0


idle
Context Switch
Reload state from PCB1

idle executing
Interrupt

Save state into PCB1

Context Switch idle

Reload state from PBC0


executing

• Switching between threads of a single process can be faster than between two
separate processes
Operating Systems-COMP 304 11
Context Switch

• Context switch is pure overhead


– Why?
– Should be very small
• 100s of nanoseconds to couple microseconds
• Exact time depends on the CPU and size of the context
– Hardware support for context switching improves
the performance

Operating Systems-COMP 304 12


Threads
• A process has at least one thread of execution
• Consider having multiple program counters per process
– Multiple locations can execute at once
– Word document
• Spell checker – 1 thread
• Typing text – 1 thread

• Must then have storage for thread details, multiple


program counters in PCB or each thread has a PCB

• More on threads later (in Chapter 4)

Operating Systems-COMP 304 13


Unix Processes
• Each process has its own address space
– Subdivided into text, data, & stack segment – a.out file
describes the address space
• OS kernel creates descriptor (PCB) to manage process
• Process identifier (PID): User handle for the process
(descriptor)

Operating Systems-COMP 304 14


task_struct
• 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 */
...

Search for task_struct (line ~1200)


https://elixir.bootlin.com/linux/latest/source/include/linux/sched.h
Operating Systems-COMP 304 15
‘top’ and ‘ps’ commands
• top: Displays processor activity of POSIX-based OS and also
displays tasks managed by kernel in real-time.
• ps: snapshot of process states

Operating Systems-COMP 304 16


Tree of Processes
Parent process creates children processes, which, in turn create other processes,
forming a tree of processes.

• init is very first process (pid =1)


• logind process is for users directly logged in to the system
• sshd process is for users remotely logged in to the system
– Starts an openSSH SSH daemon
Operating Systems-COMP 304 17
Creating/Destroying Processes
• UNIX fork()creates a process
– Creates a new address space
– Copies text, data, & stack into new address space
– Provides child with access to open files of its parent

• UNIX wait()allows a parent to wait for a child to change its


state
– This is a blocking call, parent waits until it receives a signal
– http://linux.die.net/man/2/wait

• UNIX exec()system call variants (e.g.execve()) allow a child


to run a new program

Operating Systems-COMP 304 18


Creating a UNIX process
int value, mypid=-1;
...
value = fork(); /* Creates a child process */

/* value is 0 for child, nonzero for parent */


if(value == 0) {
/* The child executes this code concurrently with parent */
mypid = getpid();
printf(“Child’s Process ID: %d\n”, mypid);
exit(0);
}
/* The parent executes this code concurrently with child */

parentWorks(..);
wait(…);
...
Operating Systems-COMP 304 19
Process Creation
• Address space
– Child duplicates the address space of the parent
– Child has a program loaded into it
• UNIX examples
– fork() system call creates a new process
– exec() system call is used after a fork() to replace the process’ memory
space with a new program

Operating Systems-COMP 304 20


Child Executing a Different Program
int mypid;
...
/* Set up the argv array for the child */
...
/* Create the child */
if((mypid = fork()) == 0) {
/* The child executes its own absolute program */
execve(“childProgram.out”, argv, 0);
/* Only return from an execve call if it fails */
printf(“Error in the exec … terminating the child …”);
exit(0);
}
...
wait(…); /* Parent waits for child to terminate */
...

Operating Systems-COMP 304 21


Reading

• From text book


– Read Chapter 3.1-3.4
– Linux Kernel Development (Chapter 3)

• Acknowledgments
– Original slides are by Didem Unat which were adapted from
• Öznur Özkasap (Koç University)
• Operating System and Concepts (9th edition) Wiley

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy