Module_02_OS
Module_02_OS
Figure 3.1
Process State
o As process executes it changes its state and each process may be in one of the
following states:
o New: The process is being created
o Running: Instructions are being executed
o Waiting: The process is waiting for some event to occur
o Ready: The process is waiting to be assigned to a process
o Terminated: The process has finished execution
o Only one process can be running on any processor at any instant. Many processes
may be ready and waiting.
o The state diagram corresponding to these states is shown below figure 3.2.
Figure 3.2
Figure 3.3
The PCB contains important information about the specific process including,
Process state: The current state of the process i.e., whether it is ready, running, waiting, halted
and so on.
Program counter: Indicates the address of the next instruction to be executed for a process.
CPU registers: The registers vary in number and type. Along with program counter this state
information should be saved to allow process to be continued correctly after an interrupt occurs
as shown in below figure 3.4.
CPU scheduling information: This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
Memory-management information: This information may include the value of base and limit
registers, the page tables, or the segment tables, depending on the memory system used by the
OS.
Accounting information: This information includes the amount of CPU and real time used, time
limits, account numbers, job or process numbers, and so on.
I/O status information: This information includes the list of I/O devices allocated to the process,
a list of open files, and so on.
Threads.
A process is a program that performs a single thread of execution. For example,
when a process is running a word-processor program, a single thread of instruction is
being executed. This single thread of control allows the process to perform only one
task at one time. The user cannot simultaneously type in characters and run the spell
checker within the same process.
Many modern operating systems have extended the process concept to allow a
process to have multiple threads of execution and thus to perform more than one task
at a time. On a system that supports threads, the PCB is expanded to include
information for each thread. Eg: Windows OS and UNIX
Ready Queue:
The process that are placed in main m/y and are already and waiting to executes are placed
in a list called the ready queue. This is in the form of linked list. Ready queue header contains
pointer to the first & final PCB in the list. Each PCB contains a pointer field that points next
PCB in ready queue.
Device Queue:The list of processes waiting for a particular I/O device is called device. When
the CPU is allocated to a process it may execute for some time & may quit or interrupted or
wait for the occurrence of a particular event like completion of an I/O request but the I/O may
be busy with some other processes. In this case the process must wait for I/O. This will be
placed in device queue. Each device will have its own queue.
The process scheduling is represented using a queuing diagram. Queues are represented by
the rectangular box & resources they need are represented by circles. It contains two queues
ready queue & device queues. Once the process is assigned to CPU and is executing the
following events can occur,
It can execute an I/O request and is placed in I/O queue.
The process can create a sub process & wait for its termination.
The process may be removed from the CPU as a result of interrupt and can be put
back into ready queue.
Schedulers:
The following are the different type of schedulers
Long-term scheduler (or job scheduler) – selects which processes should be brought
into the ready queue.
Short-term scheduler (or CPU scheduler) – selects which process should be executed next
and allocates CPU.
Medium-term schedulers
Short-term scheduler is invoked very frequently (milliseconds) . (must be fast)
BGSCET/AIML 2023-24 Page 4 of 43
Operarting Systems – BCS303
Figure 3.5
o inetd and dtlogin are two children of init where inetd is responsible for networking
services such as telnet and ftp; dtlogin is the process representing a user login screen.
o When a user logs in, dtlogin creates an X-windows session (Xsession), which in turns
creates the sdt_shel process. Below sdt_shel, a user's command-line shell, the C-shell or
BGSCET/AIML 2023-24 Page 5 of 43
Operarting Systems – BCS303
csh is created. In this command line interface, the user can then invoke various child
processes, such as the ls and cat commands.
o There is also csh process with pid of 7778 representing a user who has logged onto the
system using telnet. This user has started the Netscape browser (pid of 7785) and the
emacs editor (pid of 8105).
o A process needs certain resources to accomplish its task. Along with the various logical
and physical resources that a process obtains when it is created, initialization data may
be passed along by the parent process to the child process.
o When a process creates a new process, two possibilities exist in terms of execution.
o The parent continues to execute concurrently with its children.
o The parent waits until some or all of the children have terminated.
o There are also two possibilities in terms of the address space of the new process.
1. The child process is a duplicate of the parent process.
2. The child process has a new program loaded into it.
o In UNIX OS, fork () system call creates new process. In windows Create Process() does
the job.
o Exec () system call is called after a fork () to replace the process memory space with a
new program.
o If there are two different processes running a copy of the same program, the pid for child
is zero and for the parent it is greater than zero. The parent process waits for the child
process to complete with the wait () system call.
o When the child process completes, the parent process resumes from the call to wait(),
where it completes using exit() system call. This is shown in below figure 3.6.
Figure 3.6
Process Termination
o A process terminates when it finishes executing its last statement and asks the operating
system to delete it by using exit() system call.
o Process resources are deallocated by the operating system. A process can terminate another
process via Terminate Process() system call. A Parent may terminate execution of
children processes (abort) for the following reasons.
o Child has exceeded usage of allocated resources.
o Task assigned to child is no longer required.
o If parent is exiting some operating system do not allow child to continue if its parent
terminates.
o Some systems does not allow child to exist if its parent has terminated. If process
terminates then all its children must also be terminated, this phenomenon is referred as
cascading termination.
Cooperating processes require an Inter-process Communication (IPC) mechanism that will allow
them to exchange data and information. There are two fundamental models of IPC as shown in
figure 3.7.
1. Shared memory: A region of memory that is shared by cooperating processes is
established. Processes then exchange information by reading and writing data to the shared
region.
2. Message passing: Communication takes place by means of message exchange between the
cooperating processes.
Figure 3.7 Communication models (a) Message passing. (b) Shared memory.
The differences between these two models are,
Message passing Shared memory
Useful for exchanging small amount of
large data
data.
easy to implement Complex
Slower Faster
system calls are required only to establish
implemented using system calls
Shared memory region
Shared Memory System
A region of memory that is shared by cooperating processes is established.
Processes then exchange information by reading and writing data to the shared region.
To illustrate cooperating processes, consider producer-consumer problem.
Producer process produces information that is consumed by a consumer process.
One solution to producer-consumer problem uses shared memory. To allow producer and
consumer processes to run concurrently, there must be a buffer of items that can be filled
by a producer and emptied by consumer. The buffer will reside in a shared memory region.
The producer can produce one item while the consumer is consuming another item. The
producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced by the producer.
Two types of buffers can be used.
o Unbounded-buffer: places no practical limit on the size of the buffer. The consumer
may have to wait for new items, but the producer can always produce new items.
o Bounded-buffer: assumes that there is a fixed buffer size, so the consumer must wait
if the buffer is empty, and the producer must wait if the buffer is full.
The following variables reside in a region of memory shared by the producer and
consumer processes
#define BUFFER_SIZE 10
typedefstruct {
……..
……..
}item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
The shared buffer is implemented as a circular array with two logical pointers in and out.
The variable in points to the next free position in the buffer; out points to the first full
position in the buffer. The buffer is empty when in= =out, the buffer is full when ((in+
1)% BUFFER_SIZE) = = out.
The code for the producer process is shown below.
itemnextProduced;
while (true)
{
/* produce an item in nextProduced*/
while ( ((in + 1) % BUFFER_SIZE) = = out); //do nothing
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
The code for the consumer process is shown below.
itemnextConsumed;
while (true)
{
while (in = = out); //do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
BGSCET/AIML 2023-24 Page 9 of 43
Operarting Systems – BCS303
Each mailbox has a unique id and processes can communicate only if they share a
mailbox.
The send() and receive() primitives are defined as,
o Send (A, message) – send a message to mailbox A
o Receive (A, message) – receive a message from mailbox A
Properties of communication link are,
o Links are established only if processes share a common mailbox.
o A link may be associated with many processes.
o Each pair of processes may share several communication links.
OS allows the process to do the following operations
o create a new mailbox
o send and receive messages through mailbox
o destroy a mailbox
Synchronization
Message passing may be either blocking or non-blocking. Blocking is considered
synchronous. Non-blocking is considered asynchronous.
o Blocking send: The sending process is blocked until the message is received by the
receiving process or by the mailbox.
o Non-blocking send: The sending process sends the message and resumes operation.
o Blocking receive: The receiver blocks until a message is available.
o Non-blocking receive: The receiver retrieves either a valid message or a null.
Buffering
Messages exchanged by communicating processes reside in a temporary queue, and such
queues can be implemented in one of three ways,
1. Zero capacity – Maximum length is zero and sender must wait for the receiver.
2. Bounded capacity – finite length ofn messages, sender must wait if link is full.
3. Unbounded capacity – infinite length and sender never waits.
**********************
The below figure 4.1. Illustrates the difference between a traditional single
threaded process and a multithreaded process.
Figure 4.1
4.1.1 Motivation
o Many software packages that run on modern desktop PCs are multithreaded.
o An application is implemented as a separate process with several threads of
control. Eg: A Web browser might have one thread to display images or text
while another thread retrieves data from the network.
o As process creation takes more time than threadcreation it is more efficient to use
process that contains multiple threads. So, that the amount of time that a client have
to wait for its request to be serviced from the web server will be less.
o Threads also play an important role in remote procedure call.
4.1.2 Benefits
The benefits of multithreaded programming
1. Responsiveness: Multithreading allows program to continue running even if
part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.
2. Resource Sharing: Threads share the memory and resources of the process to
which they belong. The benefit of sharing code and data is that it allows an
application to have several different threads of activity within the same
address space.
Figure 4.2
4.2.2 One-to-One
Each user-level thread maps to kernel thread as shown in figure.
It provides more concurrency than Many-to-One model by allowing thread to
run when a thread makes a blocking system call.
It allows multiple threads to run in parallel on multiprocessors.
The only drawback is, creating a user thread requires creating the
corresponding kernel thread and it burdens performance of an application.
Examples: Windows NT/XP/2000, Linux
Figure 4.3
if (argc != 2)
{
fprintf(stderr,"usage: a.out<integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0)
{
fprintf(stderr,"%d must be>= 0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr); /* get the default attributes */
pthread_create(&tid,&attr,runner,argv[1]); /* create the thread */
pthread_join(tid,NULL); /*wait for the thread to exit */
printf("sum = %d\n",sum);
}
void *runner(void *param) /* The thread will begin control in this function */
{
inti, upper= atoi(param);
sum = 0;
for (i = 1; i<= upper; i++)
sum += i;
pthread_exit(0) ;
}
if (ThreadHandle != NULL)
{
WaitForSingleObject(ThreadHandle,INFINITE);// now wait for the thread to
finish
CloseHandle(ThreadHandle); // close the thread handle
printf("surn = %d\n" ,Sum);
}
}
class Sum
{
privateint sum;
publicintgetSum()
{
return sum;
}
circumstances, each thread might need its own copy of certain data. Such data is
called as thread-specific data. For example, in a transaction-processing system, we
might service each transaction in a separate thread. Furthermore, each transaction
may be assigned a unique identifier.
Most thread libraries including Win32 and Pthreads provide support for thread-
specific data.
Figure 4.6
An application may require any number of LWPs to run efficiently.
In a CPU-bound application running on a single processor only one thread can run at
once, so one LWP is sufficient. An application that is I/O- intensive may require
multiple LWPs to execute.
One scheme for communication between the user-thread library and the kernel is
known as scheduler activation. It works as follows: The kernel provides an
application with a set of virtual processors (LWPs), and the application can
schedule user threads onto an available virtual processor. The kernel must inform an
application about certain events. This procedure is known as an upcall.
Upcalls are handled by the thread library with an upcall handler, and upcall handlers
must run on a virtual processor.
One event that triggers an upcall occurs when an application thread is about to block.
In this situation, the kernel makes an upcall to the application informing it that a
thread is about to block and identifying the specific thread. The kernel then allocates
a new virtual processor to the application.
The application runs an upcall handler on this new virtual processor, which saves the
state of the blocking thread and gives up the virtual processor on which the blocking
thread is running. The upcall handler then schedules another thread that is eligible to
run on the new virtual processor.
When the event that the blocking thread was waiting for occurs, the kernel makes
another upcall to the thread library informing it that the previously blocked thread is
now eligible to run.
The upcall handler for this event also requires a virtual processor, and the kernel may
allocate a new virtual processor or preempt one of the user threads and run the upcall
handler on its virtual processor.
After marking the unblocked thread as eligible to run, the application schedules an
eligible thread to run on an available virtual processor.
Figure 5.1
The duration of CPU bursts vary from process to process and from computer to
computer.
The frequency curve is as shown below figure5.2.
Figure 5.2
o When scheduling takes place only under circumstances 1 and 4, we say that the
scheduling scheme is non-preemptive or cooperative; otherwise, it is
preemptive.
o Under non-preemptive scheduling, once the CPU has been allocated to a process,
the process keeps the CPU until it releases the CPU either by terminating or by
switching to the waiting state. A scheduling algorithm is preemptive if, once a
process has been given the CPU and it can be taken away.
5.1.4 Dispatcher
o Another component involved in the CPU-scheduling function is the dispatcher.
o The dispatcher is the module that gives control of the CPU to the process selected
by the short-term scheduler.
o This function involves the following:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
o The dispatcher should be as fast as possible, since it is invoked during every
process switch.
o The time it takes for the dispatcher to stop one process and start another running
is known as the dispatch latency.
Turnaround time: The interval from the time of submission of a process to the time of
completion is the turnaround time. Turnaround time is the sum of the periods spent waiting
to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting time: The CPU scheduling algorithm does not affect the amount of time during
which a process executes or does I/O; it affects only the amount of time that a process
spends waiting in the ready queue. Waiting time is the sum of the periods spent waiting in
the ready queue.
Response time: The measure of the time from the submission of a request until the first
response is produced. This measure, called response time, is the time it takes to start
responding, not the time it takes to output the response.
o If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the
result shown in the following Gantt chart:
o The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27
milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17
milliseconds.
o The FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated
to a process, that process keeps the CPU until it releases the CPU, either by terminating
or by requesting I/O. The FCFS algorithm is thus particularly troublesome for time-
sharing systems, where it is important that each user get a share of the CPU at regular
intervals.
5.3.2 Shortest-Job-First scheduling algorithm
o This algorithm associates with each process the length of the process's next CPU
burst.
o When the CPU is available, it is assigned to the process that has the smallest next
CPU burst.
o If the next CPU bursts of two processes are the same, FCFS scheduling is used to
break the tie.
o As an example of SJF scheduling, consider the following set of processes, with the
length of the CPU burst given in milliseconds:
o Using SJF scheduling, we would schedule these processes according to the following
Gantt chart:
o The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9
milliseconds for process P3, and 0 milliseconds for process P4. Thus, the average
waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds.
o The SJF scheduling algorithm is optimal; it gives the minimum average waiting time
for a given set of processes. Moving a short process before a long one, decreases the
waiting time of the short process more than it increases the waiting time of the long
process. Consequently, the average waiting time decreases.
o The SJF algorithm can be either preemptive or non-preemptive. The choice arises
when a new process arrives at the ready queue while a previous process is still
executing. The next CPU burst of the newly arrived process may be shorter than what
is left of the currently executing process. A preemptive SJF algorithm will preempt
the currently executing process, whereas a non-preemptive SJF algorithm will allow
the currently running process to finish its CPU burst. Preemptive SJF scheduling is
sometimes called shortest-remaining-time-first scheduling.
o As an example, consider the following four processes, with the length of the CPU
burst given in milliseconds:
o If the processes arrive at the ready queue at the times shown and need the indicated
burst times, then the resulting preemptive SJF schedule is as depicted in the following
Gantt chart:
o Process P1 is started at time 0, since it is the only process in the queue. Process P2
arrives at time 1.
o The remaining time for process P1 (7 milliseconds) is larger than the time required by
process P2 (4 milliseconds), so process P1 is preempted, and process P2 is scheduled.
The average waiting time for this example is ((10 -1) + (1-1) + (17 -2) + (5- 3))/4 =
26/4 = 6.5 milliseconds. Non-preemptive SJF scheduling would result in an average
waiting time of 7.75 milliseconds.
5.3.3 Priority Scheduling algorithm
o The SJF algorithm is a special case of the general priority scheduling algorithm.
BGSCET/AIML 2023-24 Page 30 of 43
Operarting Systems – BCS303
o A priority is associated with each process, and the CPU is allocated to the process
with the highest priority.
o Equal-priority processes are scheduled in FCFS order.
o An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse
of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority,
and vice versa.
o As an example, consider the following set of processes, assumed to have arrived at
time 0, in the order P1, P2, … , P5, with the length of the CPU burst given in
milliseconds:
Figure 5.3
o There must be scheduling among the queues, which is commonly implemented as
fixed-priority preemptive scheduling. For example, the foreground queue may have
absolute priority over the background queue.
o An example of a multilevel queue scheduling algorithm with five queues, listed
below in order of priority:
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
o Each queue has absolute priority over lower-priority queues. No process in the batch
queue could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty.
o If an interactive editing process entered the ready queue while a batch process was
running, the batch process would be preempted.
o Another possibility is to time-slice among the queues. So, each queue gets a certain
portion of the CPU time, which it can then schedule among its various processes. For
example, the foreground queue can be given 80 percent of the CPU time for RR
scheduling among its processes, whereas the background queue receives 20 percent
of the CPU to give to its processes on an FCFS basis.
Figure 5.4
o A process entering the ready queue is put in queue 0. A process in queue 0 is given a
time quantum of 8 milliseconds. If it does not finish within this time, it is moved to
the tail of queue 1. If queue 0 is empty, the process at the head of queue 1 is given a
BGSCET/AIML 2023-24 Page 35 of 43
Operarting Systems – BCS303
o The data most recently accessed by the process is populated in the cache for the
processor and successive memory accesses by the process are often satisfied in
cache memory.
o If the process migrates to another processor, the contents of cache memory must
be invalidated for the processor being migrated from, and the cache for the
processor being migrated to must be re-populated. Because of the high cost of
invalidating and re-populating caches, most SMP systems try to avoid migration of
processes from one processor to another and instead tries to keep a process running
on the same processor. This is known as processor affinity, i.e., a process has an
affinity for the processor on which it is currently running.
o Processor affinity takes several forms. When an operating system has a policy of
attempting to keep a process running on the same processor but not guaranteeing
that it will do so, a situation is known as soft affinity. Here, it is possible for a
process to migrate between processors.
o Some systems such as Linux provide system calls that support hard affinity,
thereby allowing a process to specify that it must not migrate to other processors.
b. Load Balancing
o On SMP systems, it is important to keep the workload balanced among all
processors to utilize the benefits of having more than one processor. Otherwise, one
or more processors may sit idle while other processors have high workloads along
with lists of processes awaiting the CPU.
o Load balancing attempts to keep the workload evenly distributed across all
processors in an SMP system.
o There are two general approaches to load balancing: push migration and pull
migration.
o With push migration, a specific task periodically checks the load on each processor
and if it finds an imbalance it evenly distributes the load by moving (or pushing)
processes from overloaded to idle or less-busy processors.
o Pull migration occurs when an idle processor pulls a waiting task from a busy
processor.
c. Symmetric Multithreading
o SMP systems allow several threads to run concurrently by providing multiple
physical processors.
BGSCET/AIML 2023-24 Page 37 of 43
Operarting Systems – BCS303
Figure 5.5
as process-contention scope (PCS), since competition for the CPU takes place among
threads belonging to the same process.
To decide which kernel thread to schedule onto a CPU, the kernel uses system-
contention scope (SCS). Competition for the CPU with SCS scheduling takes place
among all threads in the system.
PCS is done according to priority. The scheduler selects the runnable thread with the
highest priority to run. User-level thread priorities are set by the programmer. PCS will
preempt the currently running thread in favour of a higher-priority thread.
1. For the process listed below, draw Gantt charts using preemptive and
non-preemptive priority scheduling algorithm. A larger priority number
has a higher priority.
Jobs Arrival time Burst time Priority
J1 0 6 4
J2 3 5 2
J3 3 3 6
J4 5 5 3
Answer
Gantt chart Preemptive
J1 J3 J1 J4 J2
0 3 6 9 14 19
16) = 4
Average WT: = (8+2+0+0+4)/5= 2.8 ATAT= (18+3+2+1+9)/5= 6.6
Answer
FCFS
P1 P2 P3 P4 P5
0 10 11 13 17 20
SJF Preemptive
P2 P3 P3 P5 P4 P1
0 1 2 3 6 10 20
SJF non-preemptive
P2 P3 P5 P4 P1
0 1 3 6 10 20