Operating System and Processes
Operating System
Processes
Concept
Scheduling
Interprocess Communication
Operating System
Process Concept
Operating System
Process in Memory
Operating System
Process States
As a process executes, it changes from state
to state, from creation to termination:
new: The process is being created
Running: Instructions are being executed
waiting: The process is waiting for some
event (I/O, signal, a resource, etc.) to occur
ready: The process is ready for execution,
or waiting to be run.
terminated: The process has finished
execution.
Operating System
Diagram of Process States
Operating System
Process States: related operations-1
Create
A process is created by an existing process (parent/child); a
process is created by a service, or as a new batch job, or an
interactive logon session
Terminate
Normal completion (exit);
External completion (forced to completion: by operator, by the
parent process);
Internal completion (error, time overrun)
Schedule to run
A process is selected out of the ready queue (is scheduled)
and dispatched to the running state
Pre-empt
The running process is pre-empted because some other
process of higher priority has become ready
Operating System
Process States: related operations-2
Suspend
A process starts a time consuming IO operation, or is
swapped out, or makes a spontaneous request to sleep, or
is blocked in a synchronization operation. When a process is
suspended, it is on the related queue, namely the queue all
the processes wait for the same resource.
Actually a process, during it’s life, moves from queue to
queue
Continue
Opposite of Suspension: the resource requested becomes
available (timer signal, IO completion, unblocked).
Operating System
Process Control Block (PCB)
OS maintains
a data structure, called Process
Control Block-PCB for each
process to be able to follow it
from creation to termination.
Operating System
A Typical PCB Content
Operating System
Multiprogramming (or Multiprocessing)
a) Multiprogramming of four programs
b) Conceptual model of 4 independent, sequential
processes
c) Only one program active at any time instant
Operating System
Multiprogramming can be viewed as an illusion of multiple processors?
vCPU1 vCPU2 vCPU3
vCPU1 vCPU2 vCPU3 vCPU1 vCPU2
Shared Memory
Time
• Multiplex CPU in time, as if each program is run by a
different CPU; a virtual CPU per process.
• Each process’s “CPU” needs to know:
• Program Counter (PC), Stack Pointer (SP)
• Registers (Integer, Floating point, others…?)
• Switch from one virtual CPU to the next requires
• Saveing PC, SP, and registers in the related PCB
• Loading PC, SP, and registers from the new PCB
• Process switch is triggered by
• Timer clock, voluntary yield, I/O, its completion, others…
Operating System
How to switch between the processes
1. Save the “real world” of the currently
active process
2. Restore the “real world” of the
selected process (the one to be
executed next)
Operating System
Shareing Code is possible:
2 process one program!
PCB 1 PCB 2
Shared Code
Data 1 Data 2
The shared code must not modify itself, re-
entrent
Operating System
Process Scheduler: Which process to execute next
Scheduler:
The lowest operating system layer :
✓ Triggered by interrupts.
✓ After saving the world of the running
process, chooses the next process to
run.
✓ Loads context of the new process to
execute.
All other process related tasks, such as
accounting, logging, etc. are handled by other
higher layers of the operating system.
Operating System
Implementation of Process Switch upon an
interrupt- context switching
Operating System
Context Switching Overhaed
▪ Saving the state of the old
process and loading the state for
the new one takes time. This is
known as “Context-switch
overhead”.
▪ Time it takes depends on the
hardware
Operating System
Process Switching
Operating System
Process Queues: Maintained by the OS
Operating System
Process Scheduling Levels
Short-term scheduler (or CPU scheduler) –
selects which process in he ready queue should be
executed next and allocates CPU
Mid-term scheduler: Select which partially
executed process should be brougt into the ready
queue.
Long-term scheduler (or job scheduler) – selects
which processes should be brought into the ready
queue
Operating System
Short-term Process Scheduling
Operating System
Medium-term Scheduling
Operating System
More on Schedulers
Short-term scheduler is invoked very frequently (micro-
milliseconds) (must be fast)
Mid-term scheduler is invoked if the degree of multiprogramming
falls beyond certain threshold.
Long-term scheduler is invoked infrequently (seconds, minutes)
The long-term scheduler controls the degree of
multiprogramming
Processes can be described as either:
I/O-bound process – spends more time doing I/O than
computations; it many have short CPU bursts
CPU-bound process – spends more time doing computations;
few but long CPU bursts
Operating System
Process Creation From within another program
OS implements processes as a tree:
◆ Parent process can create children processes,
which, in turn creates other processes, forming
a tree of processes.
◆ Children share subset of parent’s resources
◆ Execution order:
◆ Parent and children execute concurrently.
No provisioning exist about which to
execute next, if they have the same priority.
◆ Parent, normally should wait until children
terminates, before termination…
Operating System
Process Creation and Address Space
New address space is created for a new
process:
Child process is created as duplicate
(clone) of the parent. So, the parent
program is loaded into it by default.
UNIX uses fork() system call to create new
process, which is duplicate of the parent in
terms of program and the data.
Operating System
A typical parent child relationship
Operating System
Process Termination
The last statement of the process forces it to terminate. The
exit() system call is often the last statement.
The allocated resources are de-allocated.
Parent may wait for the chiled (via wait() system call)
Pid_t pid; int status;
pid = wait(&status);
Parent may terminate execution of children processes explicitly
(abort())
If parent does not wait, then the child processes are considered
zombies.
If parent is terminated, the child processes are orphans.
Operating System
Multiprocess Example: Browser
Google Chrome Browser is executed in the form of 3 types of processes:
Browser process manages user interface, disk and network I/O
Renderer process renders web pages, deals with HTM and Javascript. A
new renderer for each website opened
Renderer Runs in sandbox restricting disk and network I/O,
minimizing effect of security exploits
Plug-in process for each type of plug-in; (eg. digital audio, video, and Web
browsing, etc.)
Operating System
fork() system call
• This new process (which is a child of
the parent) has a new process identifier
(PID).
• The fork() function returns the child's
PID to the parent, but it returns 0 to the
child process
• This will allow the two identical
processes to distinguish one another.
• The parent process can either continue
execution or wait for the child process
to complete.
Operating System
Process Tree Example 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
An example process tree on a UNIX OS
Operating System
How shell commands are executed in UNIX
• When a user types a command, to the
command interpreter, the shell creates a new
process by forking off a clone of itself.
• The new shell then calls exec to overlay its
memory with the contents of the executable
file ls.
• A new PCB and user area are created for the
child the process, largely from the parent.
• The child is given a PID, its memory map is
set up, and it is given shared access to its
parent’s files.
• It is ready to run with it’s registers are set up.
Operating System
An example in C
int main()
{
Pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);}}
Operating System
execxx() Family of System Calls
Exec() family functions: execl, execle,
execlp, execv, execve, and execvp,
The exec family functions of Unix-like
operating systems cause the running
process to be completely replaced by the
program passed as an argument.
As a new process is not created, the process
identifier (PID) does not change, but the
data, heap and stack of the original process
are reinitialized.
The new process image inherits the current
environment variables.
Operating System
C Program: exec() family system Calls
Prototypes of exec() family system calls:
The functions are declared in the unistd.h header for the POSIX standard.
int execl(char const *path, char const *arg0, ...);
int execle(char const *path, char const *arg0, ..., char const * const *envp);
int execlp(char const *file, char const *arg0, ...);
int execv(char const *path, char const * const * argv);
int execve(char const *path, char const * const *argv, char const * const *envp);
int execvp(char const *file, char const * const *argv);
Operating System
Example for exec system calls-1
execl()
The following example executes the ls
command, specifying the pathname of the
executable ( /bin/ls) and using arguments
supplied directly to the command.
#include <unistd.h>
int ret;
...
ret = execl ("/bin/ls", "ls", "-l", (char *)0);
Operating System
Example for exec system calls-2
execle()
The following example is similar to Using
execl(). In addition, it specifies the environment
for the new process image using the env
argument.
#include <unistd.h>
int ret;
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
...
ret = execle ("/bin/ls", "ls", "-l", (char *)0, env);
Operating System
Example for exec system calls-3
execlp()
The following example searches for the
location of the ls command among the
directories specified by the PATH environment
variable.
#include <unistd.h>
int ret;
...
ret = execlp ("ls", "ls", "-l", (char *)0);
Operating System
Example C programs for exec system calls-4
execv()
The following example passes arguments to
the ls command in the cmd array.
#include <unistd.h>
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
...
ret = execv ("/bin/ls", cmd);
Operating System
Example C programs for exec system calls-5
execve()
The following example passes arguments to the ls
command in the cmd array, and specifies the environment
for the new process image using the env argument.
#include <unistd.h>
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char
*)0 };
...
ret = execve ("/bin/ls", cmd, env);
Operating System
Example C programs for exec system calls-6
execvp()
The following example searches for the location of
the ls command among the directories specified by
the PATH environment variable, and passes
arguments to the ls command in the cmd array.
#include <unistd.h>
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
...
ret = execvp ("ls", cmd);
Operating System
?
Operating System