Operating System
Operating System
Disk storage is two orders of magnitude cheaper than RAM per bit and often two orders of magnitude
larger as well. The only problem is that the time to randomly access data on it is close to three orders of
magnitude slower. The reason is that a disk is a mechanical device, as shown in Fig.
A disk consists of one or more metal platters that rotate at 5400, 7200, 10,800 RPM or more. A
mechanical arm pivots over the platters from the corner, similar to the pickup arm on an old 33-RPM
phonograph for playing vinyl records. Information is written onto the disk in a series of concentric
circles. At any given arm position, each of the heads can read an annular region called a track.
Together, all the tracks for a given arm position form a cylinder.
Each track is divided into some number of sectors, typically 512 bytes per sector. On modern disks, the
outer cylinders contain more sectors than the inner ones. Moving the arm from one cylinder to the next
takes about 1 msec. Moving it to a random cylinder typically takes 5 to 10 msec, depending on the
drive. Once the arm is on the correct track, the drive must wait for the needed sector to rotate under
the head, an additional delay of 5 msec to 10 msec, depending on the drive’s RPM. Once the sector is
under the head, reading or writing occurs at a rate of 50 MB/sec on low-end disks to 160 MB/sec on
faster ones.
d. List various states of processes. Explain with neat diagram.
Three states a process may be in:
1. Running (actually using the CPU at that instant).
2. Ready (runnable; temporarily stopped to let another process run).
3. Blocked (unable to run until some external event happens).
Logically, the first two states are similar. In both cases the process is willing to run, only in the second
one, there is temporarily no CPU available for it. The third state is fundamentally different from the
first two in that the process cannot run, even if the CPU is idle and has nothing else to do
Four transitions are possible among these three states, as shown. Transition 1 occurs when the
operating system discovers that a process cannot continue right now. In some systems the process can
execute a system call, such as pause, to get into blocked state. In other systems, including UNIX, when
a process reads from a pipe or special file (e.g., a terminal) and there is no input available, the process
is automatically blocked.
Transitions 2 and 3 are caused by the process scheduler, a part of the operating system, without the
process even knowing about them. Transition 2 occurs when the scheduler decides that the running
process has run long enough, and it is time to let another process have some CPU time. Transition 3
occurs when all the other processes have had their fair share and it is time for the first process to get the
CPU to run again..
Many algorithms have been devised to try to balance the competing demands of efficiency for the
system as a whole and fairness to individual processes.
Transition 4 occurs when the external event for which a process was waiting (such as the arrival of
some input) happens. If no other process is running at that instant, transition 3 will be triggered and the
process will start running. Otherwise it may have to wait in ready state for a little while until the CPU
is available and its turn comes.
e. What is race condition? How mutual exclusion handles race condition?
In some operating systems, processes that are working together may share some common storage that
each one can read and write. The shared storage may be in main memory (possibly in a kernel data
structure) or it may be a shared file; the location of the shared memory does not change the nature of
the communication or the problems that arise. To see how interprocess communication works in
practice, let us now consider a simple but common example: a print spooler. When a process
wants to print a file, it enters the file name in a special spooler directory. Another process, the printer
daemon, periodically checks to see if there are any files to be printed, and if there are, it prints them
and then removes their names from the directory.
Mutual Exclusion handles it in various ways as
Disabling Interrupts
Lock Variables
Strict Alternation
f. With suitable example explain the shortest job first scheduling algorithm.
In an insurance company, for example, people can predict quite accurately how long it will take to run a
batch of 1000 claims, since similar work is done every day. When several equally important jobs are
sitting in the input queue waiting to be started, the scheduler picks the shortest job first.
Look at Fig. . Here we find four jobs A, B, C, and D with run times of 8, 4, 4,
and 4 minutes, respectively. By running them in that order, the turnaround time for A is 8 minutes, for
B is 12 minutes, for C is 16 minutes, and for D is 20 minutes for an average of 14 minutes.
Now let us consider running these four jobs using shortest job first, as shown in Fig. (b). The
turnaround times are now 4, 8, 12, and 20 minutes for an average of 11 minutes. Shortest job first is
provably optimal. Consider the case of four jobs, with execution times of a, b, c, and d, respectively.
The first job finishes at time a, the second at time a b, and so on. The mean turnaround time is
(4a 3b 2c d)/4. It is clear that a contributes more to the average than the other times, so it should
be the shortest job, with b next, then c, and finally d as the longest since it affects only its own
turnaround time. The same argument applies equally well to any number of jobs.
It is worth pointing out that shortest job first is optimal only when all the jobs are available
simultaneously. As a counterexample, consider fiv e jobs, A through E, with run times of 2, 4, 1, 1, and
1, respectively. Their arrival times are 0, 0, 3, 3, and 3. Initially, only A or B can be chosen, since the
other three jobs have not arrived yet. Using shortest job first, we will run the jobs in the order A, B, C,
D, E, for an average wait of 4.6. However, running them in the order B, C, D, E, A has an
av erage wait of 4.4.
When swapping creates multiple holes in memory, it is possible to combine them all into one big one
by moving all the processes downward as far as possible. This technique is known as memory
compaction.
c. Explain the first in first out page replacement algorithm. Give example.
consider a supermarket that has enough shelves to display exactly k different products. One day, some
company introduces a new convenience food—instant, freeze-dried, organic yogurt that can be
reconstituted in a microwave oven. It is an immediate success, so our finite supermarket
has to get rid of one old product in order to stock it. One possibility is to find the product that the
supermarket has been stocking the longest (i.e., something it began selling 120 years ago) and get rid of
it on the grounds that no one is interested any more. In effect, the supermarket maintains a
linked list of all the products it currently sells in the order they were introduced. The new one goes on
the back of the list; the one at the front of the list is dropped.
As a page replacement algorithm, the same idea is applicable. The operating system maintains a list of
all pages currently in memory, with the most recent arrival at the tail and the least recent arrival at the
head. On a page fault, the page at the head is removed and the new page added to the tail of the list.
When applied to stores, FIFO might remove mustache wax, but it might also remove flour, salt, or
butter. When applied to computers the same problem arises: the oldest page may still be useful. For this
reason, FIFO in its pure form is rarely used.
d. List and explain different file structures.
Three types of file structures
a) Byte sequence.
b) Record sequence.
c) Tree.
The file in Fig.(a) is an unstructured sequence of bytes. In effect, the operating system does not
know or care what is in the file. All it sees are bytes. Any meaning must be imposed by user-
level programs. Both UNIX and Windows use this approach.
Having the operating system regard files as nothing more than byte sequences provides the
maximum amount of flexibility. User programs can put anything they want in their files and
name them any way that they find convenient. The operating system does not help, but it also
does not get in the way. For users who want to do unusual things, the latter can be very
important. All versions of UNIX (including Linux and OS X) and Windows use this file
model.
The first step up in structure isillustrated in Fig.(b). In this model, a file is a sequence of fixed-
length records, each with some internal structure. Central to the idea of a file being a sequence
of records is the idea that the read operation returns one record and the write operation
overwrites or appends one record. As a historical note, in decades gone by, when the 80-
column punched card was king of the mountain, many (mainframe) operating systems based
their file systems on files consisting of 80-character records, in effect, card images. These
systems also supported files of 132-character records, which were intended for the line printer
(which in those days were big chain printers having 132 columns). Programs read input in
units of 80 characters and wrote it in units of 132 characters, although the final 52 could be
spaces, of course. No current general-purpose system uses this model as its primary file system
any more, but back in the days of 80-column punched cards and 132-character line printer
paper this was a common model on mainframe computers.
The third kind of file structure is shown in Fig. (c). In this organization, a file consists of a tree
of records, not necessarily all the same length, each containing a key field in a fixed position in
the record. The tree is sorted on the key field, to allow rapid searching for a particular key.
The basic operation here is not to get the ‘‘next’’ record, although that is also possible, but to
get the record with a specific key. For the zoo file of Fig. (c), one could ask the system to get
the record whose key is pony, for example, without worrying about its exact position in the
file. Furthermore, new records can be added to the file, with the operating system, and not the
user, deciding where to place them. This type of file is clearly quite different from the
unstructured byte streams used in UNIX and Windows and is used on some large mainframe
computers for commercial data processing.
Here, one copy of the operating system and its tables is present on CPU 1 and not on any of the others.
All system calls are redirected to CPU 1 for processing there. CPU 1 may also run user processes if
there is CPU time left over. This model is called master-slave since CPU 1 is the master and all the
others are slaves.
idle while another is overloaded. Similarly, pages can be allocated among all the processes dynamically
and there is only one buffer cache, so inconsistencies never occur.
The problem with this model is that with many CPUs, the master will become a bottleneck. After all, it
must handle all system calls from all CPUs. If, say, 10% of all time is spent handling system calls, then
10 CPUs will pretty much saturate the master, and with 20 CPUs it will be completely overloaded.
Thus this model is simple and workable for small multiprocessors, but for large ones it fails.
5. Attempt any three of the following: 15
a. List categories of Linux utility programs. Explain any two
The command-line (shell) user interface to Linux consists of a large number of standard utility
programs. Roughly speaking, these programs can be divided into
six categories, as follows:
1. File and directory manipulation commands.
2. Filters.
3. Program development tools, such as editors and compilers.
4. Text processing.
5. System administration.
6. Miscellaneous
b. Explain various process management system calls in Linux.