Module 61CSE218: Operating System
Process
(Part 1)
Vietnamese-German University
Ngoc Tran, Ph.D.
[Link]@[Link]
Operating System Concepts – 9th10th Edition
Edition Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Objectives
To introduce the notion of a process -- a program in
execution, which forms the basis of all computation
To describe the various features of processes, including
scheduling, creation and termination
10
Operating System Concepts – 9th th Edition
Edition 3.2 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Program
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Terms job and process are used almost interchangeably
Multiple parts
The program code, also called text section
Current activity including program counter, 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 System Concepts – 9th10th Edition
Edition 3.3 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Batch System
10Edition
Operating System Concepts – 9th th Edition 3.4 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Program Code in Memory
Operating System Concepts – 9th10th Edition
Edition 3.5 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process
Process – a program in execution; process execution must
progress in sequential fashion
Program is passive entity stored on disk (executable file),
process is active
Program becomes process when executable file loaded into
memory
Execution of program started via GUI mouse clicks, command
line entry of its name, etc.
One program can be several processes
Consider multiple users executing the same program.
Operating System Concepts – 9th
10 Edition
th Edition 3.6 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process
A process needs certain resources—such as CPU time, memory,
files, and I/O devices — to accomplish its task.
These resources are allocated to the process either when it is
created or while it is executing.
A process is the unit of work in most systems.
Systems consist of a collection of processes: operating-
system processes execute system code, and user processes
execute user code.
All these processes may execute concurrently.
10Edition
Operating System Concepts – 9th th Edition
3.7 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process State
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
Which state is only one?
Operating System Concepts – 9th10th Edition
Edition 3.8 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Diagram of Process State
Operating System Concepts – 9th10th Edition
Edition 3.9 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
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
Operating System Concepts – 9th10th Edition
Edition 3.10 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Representation in Linux
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 */
Operating System Concepts – 9th10th Edition
Edition 3.11 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Context Switch
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
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful
work while switching
The more complex the OS and the PCB the longer the
context switch
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
Operating System Concepts – 9th10th Edition
Edition 3.12 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
CPU Switch From Process to Process
Operating System Concepts – 9th10th Edition
Edition 3.13 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Threads
Process has a single thread of execution
Consider having multiple program counters per process
Multiple locations can execute at once
Multiple threads of control -> threads
Must then have storage for thread details, multiple program
counters in PCB
Operating System Concepts – 9th10th Edition
Edition 3.14 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Scheduling
Maximize CPU use, quickly switch processes onto CPU for
time sharing
Process scheduler selects among available processes for
next execution on CPU
Maintains scheduling queues of processes
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main
memory, ready and waiting to execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
Which queue is only one?
Operating System Concepts – 9th10th Edition
Edition 3.15 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Ready Queue And Various I/O Device Queues
Operating System Concepts – 9th10th Edition
Edition 3.16 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Representation of Process Scheduling
Queueing diagram represents queues, resources, flows
Operating System Concepts – 9th10th Edition
Edition 3.17 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Schedulers
Processes can be described as either:
I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts
CPU-bound process – spends more time doing computations; few very
long CPU bursts
Operating System Concepts – 9th10th Edition
Edition 3.18 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Operations on Processes
System must provide mechanisms for:
process creation,
process termination
Operating System Concepts – 9th10th Edition
Edition 3.19 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Creation
Parent process creates children processes, which, in turn
create other processes, forming a tree of processes
Generally, process identified and managed via a process
identifier (pid)
Resource sharing options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Operating System Concepts – 9th10th Edition
Edition 3.20 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
A Tree of Processes in Linux
init
pid = 1
login kthreadd sshd
pid = 8415 pid = 2 pid = 3028
bash khelper pdflush sshd
pid = 8416 pid = 6 pid = 200 pid = 3610
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
Operating System Concepts – 9th10th Edition
Edition 3.21 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a 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
Operating System Concepts – 9th10th Edition
Edition 3.22 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
C Program Forking Separate Process
Operating System Concepts – 9th10th Edition
Edition 3.23 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
C Program Forking Separate Process
Exercise 1: What is the output of the below program?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
fork();
printf("Hello world!\n");
return 0;
}
[Link]
Operating System Concepts – 9th10th Edition
Edition 3.25 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
C Program Forking Separate Process
Exercise 2: What is the output of the below program?
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
[Link]
Operating System Concepts – 9th10th Edition
Edition 3.26 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
C Program Forking Separate Process
Exercise 3: What is the output of the below program?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");
// parent process because return value non-zero.
else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
[Link]
Operating System Concepts – 9th10th Edition
Edition 3.27 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Creating a Separate Process via Windows API
Operating System Concepts – 9th10th Edition
Edition 3.29 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Termination
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())
Process’ resources are deallocated by operating system
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
Operating System Concepts – 9th10th Edition
Edition 3.30 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Process Termination
Some operating systems do not allow child to exists if its parent
has terminated. If a process terminates, then all its children must
also be terminated.
cascading termination. All children, grandchildren, etc. are
terminated.
The termination is initiated by the operating system.
The parent process may wait for termination of a child process by
using the wait()system [Link] call returns status information
and the pid of the terminated process
pid = wait(&status);
If no parent waiting (did not invoke wait()) process is a zombie
If parent terminated without invoking wait , process is an orphan
Operating System Concepts – 9th10th Edition
Edition 3.31 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Exercise
1. Build and run a project running [Link] as on page 29.
2. Modify the project in 1) so that users can input the path to a given
executable file and run that file from the console.
3. Try commands:
TerminateProcess([Link], exitcode);
GetExitCodeProcess([Link], exitcode);
or
Exit(status);
Operating System Concepts – 9th10th Edition
Edition 3.32 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Exercise - Hints
#include <windows.h> 2
#include <stdio.h>
#include <tchar.h>
void _tmain(int argc, TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
[Link] = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (argc != 2)
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
Operating System Concepts – 9th10th Edition
Edition 3.33 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
Exercise - Hints
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject([Link], INFINITE);
// Close process and thread handles.
CloseHandle([Link]);
CloseHandle([Link]);
}
Operating System Concepts – 9th10th Edition
Edition 3.34 Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013
End
Operating System Concepts – 9th Edition
10th Edition Lecturer: Ngoc Tran, Ph.D. Silberschatz,
Silberschatz,Galvin andGagne
Galvin, Gagne©2018
©2013