Operating System Important Questions and Answers - Crowley
Operating System Important Questions and Answers - Crowley
Operating System Important Questions and Answers - Crowley
There are 2 operating modes in the CPU; the kernel mode and the user mode. To ensure proper execution of operating
system there must be a way to distinguish between user code and operating system code. For this purpose, one bit is
maintained in Hardware. That bit is called mode bit. When the mode bit is zero then the system is said to be in kernel mode
and when this mode bit is 1 then the system is said to be in user mode.
Whenever the computer execute some code on behalf of user application then it is said to be in user mode and at that time
this mode bit will be 1.
When the operative system gains control over the computer system then it is said to be in kernel mode and at that time this
mode bit should be 0.
Kernel mode is also called supervisor mode or system mode. It is also called privileged mode.
Whenever the computer system is executing on behalf of a user application then the system is in user mode. But when this
user application requests service from operating system through system call then at that time there must be a transition from
user mode to kernel mode
To protect the operating system from end users, the machine instructions which can cause harm are designated as privileged
instructions. Hardware allows these privileged instructions to be executed only in kernel mode.
System call is a special machine instruction that causes an interrupt. Example: syscall, trap, svc. There are
some tasks which can be performed only by the operating system. So user program asks the operating
system to perform these tasks on its behalf by system call. So system call provides a way to a user program
by which it can ask the operating system to perform these tasks. Whenever a system call is executed,
it is treated by the hardware as a software interrupt (software interrupt is nothing but system call)
and through interrupt vector, control passes to interrupt handler service routine in the operating
system
and the mode bit is set to the kernel mode
then kernel examines the interrupting instruction and determines the type of system call and checks
whether the user has the privilege, then kernel executes the request and
it returns the control to the instruction following the system call in the user program.
Example:
#include <stdio.h>
int main()
{
printf("hello, world!\n");
return 0;
}
The above program uses printf, which makes a system call to write those bytes to stdout. stdout is standard
output, which is by default monitor.
https://www.youtube.com/watch?v=ru-ouiqVDqc
This system call would create a pipe for one-way communication i.e., it creates two descriptors, fd[0] and
fd[1]. Descriptor fd[0] is for reading and fd[1] is for writing. Whatever is written into fd[1] can be read from
fd[0].
This pipe() system call would return zero on success and -1 in case of failure. To know the cause of failure,
check with errno variable or perror() function.
#include<unistd.h>
ssize_t read(int fd, void *buf, size_t count)
The above system call is to read from the specified file with arguments of file descriptor fd, proper buffer
with allocated memory (either static or dynamic) and the size of buffer.
The file descriptor id, fd is to identify the respective file, which is returned after calling open() or pipe()
system call. The file needs to be opened before reading from the file. It automatically opens in case of
calling pipe() system call.
This call would return the number of bytes read (or zero in case of encountering the end of the file) on
success and -1 in case of failure.
#include<unistd.h>
ssize_t write(int fd, void *buf, size_t count)
The above system call is used to write to the specified file.
https://www.youtube.com/watch?v=wQUY2W49xjQ
https://www.youtube.com/watch?v=QRKn32A3eeY
(vi). Then the operating system will link this process to scheduling queue and the process state would be
changed from ‘New’ to ‘Ready’. Now process is competing for the CPU.
(v). Additionally, operating system will create some other data structures such as log files or accounting files
to keep track of processes activity.
https://www.youtube.com/watch?v=i6gPBm-zOXc
6) Multilevel Feedback Queue Scheduling
A process can move between queues. The idea is to separate processes according to their CPU bursts.
I/O – bound and interactive processes will be in highest priority queue.
If a process takes more CPU time, it is moved to lower priority queue.
If a process waits too long in lower priority queue, it is moved to higher priority queue to prevent starvation.
As shown in the below figure, let there be 3 queues Q0, Q1, Q2.
1. Q0 – time quantum 8 milliseconds
2. Q1 – time quantum 16 milliseconds
3. Q2 – FCFS
Scheduling
1. A process entering ready queue is put in queue Q0 When it
gains CPU, the process receives 8 msec.
If it does not finish in 8 milliseconds, process is moved to
queue Q1.
2. At Q1, process again receives 16 additional milliseconds.
If it still does not complete, it is moved to queue Q2.
Q) Discuss First In First Out Page Replacement Algorithm with Example
This is the simplest page replacement algorithm. In this algorithm, the OS maintains a queue that keeps
track of all the pages in memory, with the oldest page at the front and the most recent page at the back.
When there is a need for page replacement, the FIFO algorithm, swaps out the page at the front of the
queue, that is the page which has been in the memory for the longest time.
For Example:
Consider the page reference string of size 12: 1, 2, 3, 4, 5, 1, 3, 1, 6, 3, 2, 3 with frame size 4(i.e.
maximum 4 pages in a frame).
Advantages
Simple and easy to implement.
Low overhead.
Disadvantages
Poor performance.
Doesn’t consider the frequency of use or last used time, simply replaces the oldest page.
Suffers from Belady’s Anomaly(i.e. more page faults when we increase the number of page
frames).
1. Main memory is divided into fixed-sized blocks called frames (size is power of 2)
2. logical memory is divided into blocks of same size called pages.
3. Logical address is divided into 2 parts
Page number (p) – Page table is indexed by page number.
Page offset (d) –
4. A page table is allocated to each process. (A pointer to page table is stored in PCB of process. )
Page table translates logical to physical addresses. Page 0 is in frame1, Page 1 is in frame 4 etc..
5. Internal fragmentation may occur due to paging.
As a process executes, it changes state. Each process can be in one of the following states
1. New: the process is being created.
2. Running: Instructions are being executed
3. Waiting: a process is waiting for some event to occur (such as I/O completion etc..)
4. Ready: process is waiting to be assigned to processor
5. Terminated: the process has finished execution
Each process is represented in the operating system by a process control block (PCB) or task control block.
It contains information about
1. Process state: the state may be new, ready, running, blocked or terminated.
2. Program Counter : this register stores the address of next instruction to be executed.
3. CPU registers: like accumulators, stack pointers, index register and general purpose
registers. The values of these registers are saved if the process is interrupted.
4. CPU scheduling information : like process priority, scheduling parameters etc.
5. Memory management information : include the values of base and limit register,
segment tables, page tables etc
6. Accounting Information: include amount of CPU used, time limits, job or process
number etc.
7. I/O status information : like list of I/O devices allocated to the process, list of open
files, and so on.
https://www.youtube.com/watch?v=By6lWjiPpVI&list=PLG9aCp4uE-s17rFjWM8KchGlffXgOzzVP