Sie sind auf Seite 1von 13

K L University

Department of Computer Science & Engineering


nd
Course Handout for 2 Year B.Tech PROGRAM
nd
A.Y.2016-17, 2 Semester

Course Name : Operating Systems


Course Code : 15 CS 2206
Key :Test-1

1. A) Memorize about the evolution of operating systems (3)


The first computers used batch operating systems, in which the computer ran batches of jobs without stop.
Programs were punched into cards that were usually copied to tape for processing. When the computer finished
one job, it would immediately start the next one on the tape.
Professional operators, not the users, interacted with the machine. Users dropped jobs off, then returned to pick
up the results after their jobs had run. This was inconvenient for the users, but the expensive computer was kept
busy with a steady stream of jobs.
In the 1960s, time-shared operating systems began replacing batch systems. Users interacted directly with the
computer via a printing terminal like the Western Electric Teletype shown here.
Several users shared the computer at the same time, and it spent a fraction of a second on each one's job before
moving on to the next. A fast computer could work on many user's jobs at the same time, while creating the
illusion that they were receiving its full attention.
Printing terminals required that programs had character or command-line user interfaces (CLI), in which the
user typed responses to prompts or typed commands. The interaction scrolled down a roll of paper.
Printing terminals were later replaced by video terminals that could only display fixed size characters. Some
could be used to create forms on the screen, but many simply scrolled like a "glass Teletype."
Personal computers became affordable in the mid 1970s. The Altair 8800, shown here, was the first
commercially viable personal computer marketed to individuals. Beginning in January 1975, the Altair was sold
to hobbyists in kit form. The Altair did not have an operating system, since it had only toggle switches and light-
emitting diodes for input and output.
people soon connected terminals and floppy disk drives to Altairs. In 1976, Digital Research introduced the
CP/M operating system for the Altair and computers like it. CP/M and later DOS had CLIs that were similar to
those of the time-shared operating systems, but the computer was dedicated to a single user, not shared.
As hardware prices fell, personal computers with bit-mapped displays that could control individual pixels were
developed. These made personal computer with graphical user interfaces (GUIs) possible.
The first commercial success was the Apple Macintosh which was introduced in 1984. The initial Macintosh
pushed the state of the hardware art, and was restricted to a small, monochrome display.
As hardware continued to evolve, larger, color Macs were developed and Microsoft introduced Windows, their
GUI operating system.
The Macintosh operating system was based on decades of research on graphically-oriented personal computer
operating systems and applications.
B)Identify a system program using fork(). The child process should print hello; the parent process
should print goodbye. You should try to ensure that the child process always prints first; can you do
this without calling wait() in the parent (4)
No, we cant. We can use waitpid().
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
int status;
if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0) { // child (new process)
printf("Hello !, I am child (pid:%d)\n", (int) getpid());
} else { // parent goes down this path (main)
While( waitpid(rc, &status, WNOHANG) = = 0)
{
Sleep(1);
}
printf("Good Bye!, I am parent of %d (wc:%d) (pid:%d)\n",
}
return 0;
}
C) Discuss how Limited Direct Execution solves the Challenges faced by Direct Execution Protocol?(3)
Problem #1: Restricted Operations
Direct execution has the obvious advantage of being fast; the program runs natively on the hardware CPU and
thus executes as quickly as one would expect. But running on the CPU introduces a problem: what if the process
wishes to perform some kind of restricted operation, such as issuing an I/O request to a disk, or gaining access to
more system resources such as CPU or memory Thus, the approach we take is to introduce a new processor
mode, known as user mode; code that runs in user mode is restricted in what it can do. For example, when
running in user mode, a process cant issue I/O requests; doing so would result in the processor raising an
exception; the OS would then likely kill the process. In contrast to user mode is kernel mode, which the
operating system (or kernel) runs in. In this mode, code that runs can do what it likes, including privileged
operations such as issuing I/O requests and executing all types of restricted instructions.
Problem #2: Switching Between Processes
The next problem with direct execution is achieving a switch between processes. Switching between processes
should be simple. OS should just decide to stop one process and start another.
A Cooperative Approach: Wait For System Calls: One approach that some systems have taken in the
past (for example, early versions of the Macintosh operating system [M11], or the old Xerox Alto system
[A79]) is known as the cooperative approach. In this style, the OS trusts the processes of the system to
behave reasonably. Processes that run for too long are assumed to periodically give up the CPU so that
the OS can decide to run some other task.
A Non-Cooperative Approach: The OS Takes Control : Without some additional help from the
hardware, it turns out the OS cant do much at all when a process refuses to make system calls (or
mistakes) and thus return control to the OS. In fact, in the cooperative approach, your only recourse
when a process gets stuck in an infinite loop is to resort to the age-old solution to all problems in
computer systems: reboot the machine. Thus, we again arrive at a sub problem of our general quest to
gain control of the CPU.
Saving and Restoring Context: the OS has regained control, whether cooperatively via a system call, or
more forcefully via a timer interrupt, a decision has to be made: whether to continue running the
currently-running process, or switch to a different one. This decision is made by a part of the operating
system known as the scheduler.If the decision is made to switch, the OS then executes a low-level piece
of code which we refer to as a context switch. A context switch is conceptually simple: all the OS has to
do is save a few register values for the currently-executing process (onto its kernel stack, for example)
and restore a few for the soon-to-be-executing process.
2A) List the functions of File management (3)
A file system is normally organized into directories for easy navigation and usage. These directories may
contain files and other directions.
An Operating System does the following activities for file management
Keeps track of information, location, uses, status etc. The collective facilities are often known as file
system.
Creation and deletion of files/ directories.
Support of primitives for files/ directories manipulation.
Backing up of files on storage media.
Open,read,write,delete,close,file attributes(get/set)
B) Identify a system program that uses wait() to wait for the child process to finish in the parent. What
does wait() return What happens if you use wait() in the child(4)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[])


{
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);

}
else if (rc == 0) { // child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
} else { // parent goes down this path (main)

int wc = wait(NULL);
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
rc, wc, (int) getpid());
}
return 0;
}
return the pid of the terminated child. If using wait() in the child, immediately return -1.

C)Explain Short notes on Work Load Assumptions, Turn Around Time, response Time in CPU
Scheduling (3)

Work Load Assumptions: First make a number of simplifying assumptions about the processes running
in the system, sometimes collectively called the workload. We will make the following assumptions
about the processes, sometimes called jobs, that are running in the system:

1. Each job runs for the same amount of time.


2. All jobs arrive at the same time.
3. All jobs only use the CPU (i.e., they perform no I/O)
4. The run-time of each job is known.

Turn Around Time:

The turnaround time of a job is defined as the time at which the job completes minus the time at which
the job
arrived in the system. More formally, the turnaround time Tturnaround is: Tturnaround = Tcompletion
Tarrival
Because we have assumed that all jobs arrive at the same time, for now Tarrival = 0 and hence
Tturnaround = Tcompletion.
Response Time:

Response time is defined as the time from when the job arrives in a system to the first time it is
scheduled. More formally: Tresponse = Tfirstrun Tarrival
For example, if we had the schedule above (with A arriving at time 0, and B and C at time 10), the
response time of each job is as follows: 0 for job A, 0 for B, and 10 for C (average: 3.33).
3. a Repeat the activities of device management (3)
These resources are also thought of as devices. Devices may be physical ( e.g. disk drives ), or virtual /
abstract ( e.g. files, partitions, and RAM disks ). Some systems represent devices as special files in the file
system, so that accessing the "file" calls upon the appropriate device drivers in the OS.
User programs request the device, and when finished they release the device. Similar to files, we can read, write,
and reposition the device.
Devices communicate with the os through the interrupts.
OS Services for I/O device management
Managing the buffering, caching, and spooling
A general device-driver interface
Drivers for specific hardware devices
b Identify process states and explain transitions with a diagram (4)
Process is a program in execution.
As a process executes, it changes state

Process state transitions


Running: In the running state, a process is running on a processor. This means it is executing
instructions.
Ready: In the ready state, a process is ready to run but for some reason the OS has chosen not to run it
at this given moment.
Blocked: In the blocked state, a process has performed some kind of operation that makes it not ready to
run until some other event takes place.
Example: when a process initiates an I/O request to a disk, it becomes blocked and thus
some other process can use the processor
A process can be moved between the ready and running states at the discretion of the OS.
Being moved from ready to running means the process has been scheduled;
Being moved from running to ready means the process has been descheduled.
Once a process has become blocked (e.g., by initiating an I/O operation), the OS will keep it as such
until some event occurs (e.g., I/O completion);
At that point, the process moves to the ready state again (and potentially immediately to running again, if
the OS so decides.
c) Describe SJF and STCF .Whether it overcomes convoy effect (3)
The SJF algorithm associates with each process the length of its next CPU burst
When the CPU becomes available, it is assigned to the process that has the smallest next CPU burst (in
the case of matching bursts, FCFS is used)
Sjf suffers from convoy problem
Sjf is Nonpreemptive once the CPU is given to the process, it cannot be preempted until it
completes its CPU burst
Stcf is Preemptive if a new process arrives with a CPU burst length less than the remaining
time of the current executing process, preempt. This scheme is know as the Shortest-
Remaining-Time-First (SRTF) or Shortest time for completion first(STCF)
Example OF SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive)

P1 P3 P2 P4
0 7 8 12 16

Average waiting time = [0 +(8-2)+(7-4) +(12-5)] /4 =4

Example of SRTF/STCF
CPU selects the process for execution which has the smallest amount of time remaining until completion .

Proces Arrival Time Burst Time


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)

P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16

Average waiting time = (9 + 1 + 0 +2)/4 =3

Convoy effect; all the other processes wait for one long-running process to finish using the CPU.
SJF overcomes convoy effect
The idea behind the SJF algorithm is to pick the quickest fastest little job that needs to be done, get it out
of the way first, and then pick the next smallest fastest job to do next.
4. (a). State dual mode of operation (3)

Ans: To distinguish between Operating system code and user defined code,computer can be operated in two
modes through mode bit, that is added to the hardware of the computer to indicated the current mode.They are:

1. User mode

- mode bit 1

- user can interact with the system.

- to execute user application

- No directr access to hardware (or) memory.

2. Kernel mode

-mode bit 0

- user cannot interact with the system

- At system boot ,the hardware starts in kernel mode.

- allows execution of privileged instructions like I/O control, interrupt management etc.

- Complete access to hardware.

os dual mode operation occurs either user program invokes system call or context switch during execution.

(b). Explain what user can do with process API available on OS? (4)

Ans: - An application programming interface (API) is a set of subroutine definitions, protocols, and tools for
building application software. APIs, in some form, are available on any modern operating system.

Create: An operating system must include some method to create new processes. When you type a command
into the shell, or double-click on an application icon, the OS is invoked to create a new process to run the
program you have indicated.
Destroy: As there is an interface for process creation, systems also provide an interface to destroy processes
forcefully. Of course, many processes will run and just exit by themselves when complete; when they dont,
however, the user may wish to kill them, and thus an interface to halt a runaway process is quite useful.
Wait: Sometimes it is useful to wait for a process to stop running; thus some kind of waiting interface is often
provided.
Miscellaneous Control: Other than killing or waiting for a process, there are sometimes other controls that are
possible. For example, most operating systems provide some kind of method to suspend a process (stop it from
running for a while) and then resume it (continue it running).
Status: There are usually interfaces to get some status information about a process as well, such as how long it
has run for, or what state it is in.
(c).Report the response time and turn around time when running three jobs of length 200 with SJF and
FIFO schedulers. (3)

Ans: Inference:

1. Since all jobs have the same burst time/execution time, SJF will be similar to FIFO for process scheduling.

Ready Queue: P1 , P2, P3

Process Arrival Time Burst Time Completion Time Turn around Time
(AT) (BT) (CT) TAT=(CT-AT)

P1 0 200 200 200

P2 0 200 400 400

P3 0 200 600 600

Gantt Chart:

P1 P2 P3
0 200 400 600

Average Turn Around Time = 200+400+600/3= 400

Response Time: Since FIFO follows Non-preemptive scheduling, the response time is the completion time.

5 A) Memorize Time-Sharing Systems (3)


To virtualize the CPU, the operating system needs to somehow share the physical CPU among many jobs
running seemingly at the same time. The basic idea is simple: run one process for a little while, then run another
one, and so forth. By time sharing the CPU in this manner, virtualization is achieved.

B) Identify a system program using execl() system call (4)


The following example program uses execl() to execute the simple command - ls -l:
#inciude <stdio.h>

main()
{
execl("/bin/ls", "ls", "-l", 0);
printf("Can only get here on error\n");
}
C) Identify a system program to Read a file in reverse - uses lseek()and error handling(3)
#include <fcntl.h>
#include <unistd.h> /* For STDOUT_FILENO */
#include <stdio.h>
int main(int argc, char **argv) {
int size, fd;
char buf; /* Single-character buffer */
char *mesg = "Single filename required\n";
if ((fd = open(argv[1], O_RDONLY)) == -1)
perror("open");
lseek(fd, 1, SEEK_END); /* Pointer taken to EOF + 1 first */
while (lseek(fd, -2, SEEK_CUR) >= 0) { /* and then back by two bytes */
if (read(fd, &buf, 1) != 1)
perror("read");
if (write(STDOUT_FILENO, &buf, 1) != 1)
perror("write");
}
close(fd); /* Can have error here too */
exit(0); /* exit doesn't return - hence no error */
}
6. (a) List the different features of an operating system (3M)
Some common features, some of which are more important than others depending on the type of operating
system
Scheduling
The task of handling how active processes are making efficient use of the CPU processing cycles is called
scheduling. There are many ways of doing this, which is covered in another mini-website.
Memory Management
The operating system has to make sure that applications are able to run in the amount of memory available and
that they do not interfere with one another. There is a separate mini-website on this topic.
Allocation of resources
The operating system will provide a working area for each user. This includes
Disk space quota for their files ( especially on shared network drives)
A personal GUI set up for each user (multi-user operating systems)
Perhaps how many processing cycles they are allowed to use (especially on mainframe)
How much printer output they are allowed (networked and mainframe)
How high a priority they can assign to a job (mainframe)
Keeping track of usage
The cost of using large computers is shared amongst the users. So the operating system will have an accounting /
tracking system in place that :
Counts the processing cycles used per user
Print out jobs completed
Batch jobs completed
Time spent logged in
Other resources used
And so on. A regular bill is then sent to the user account providing an itemised charge.
Data and User security
Each user has to be authenticated with an username and password (network and multi-user operating system).
Their data and files will be kept private from other users, unless they choose to make some shareable with others.
The operating system will only allow administrators ('super users') to change parts of the operating system and
install applications.
Providing system services such as print spooling
Printing out is a time consuming process, so it makes sense to allow users to hand-off a print job to the operating
system so they can get on with other things. This is called 'print spooling' and is common on multi-user and
networked operating systems.
Managing input / output
Data and applications are stored on secondary storage devices such as hard disks, optical drives, magnetic tape
when not in use. The operating system has a file management system that allows the user to organise their files,
to move, delete and copy files as they wish.
Specialised input devices such as graphics tablets and scanners are also handled by the operating system.
Handling Network communication
Data packets travelling to and from the connected computers on the network are handled by the operating
system. When an user drags a file from their hard disk to a shared networked drive, they do not care how it
happens - the operating system takes care of all the details.
b) Explain the importance of exec() (4M)
The exec() System Call:
A final and important piece of the process creation API is the exec() system call. This system call is useful when
you want to run a program that is different from the calling program. For example, calling fork() is only useful if
you want to keep running copies of the same program.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int
main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
char *myargs[3];
myargs[0] = strdup("wc"); // program: "wc" (word count)
myargs[1] = strdup("p3.c"); // argument: file to count
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); // runs word count
printf("this shouldn't print out");
} else {
// parent goes down this path (original process)
int wc = wait(NULL);
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid());
}
return 0;
}

The fork() system call is strange; its partner in crime, exec(), is not so normal either. What it does: given the
name of an executable and some arguments, it loads code (and static data) from that executable and overwrites
its current code segment (and current static data) with it; the heap and stack and other parts of the memory space
of the program are re-initialized. Then the OS simply runs that program, passing in any arguments as the argv of
that process. Thus, it does not create a new process; rather, it transforms the currently running program into a
different running program. After the exec() in the child, a successful call to exec() never returns.

c) Explain about FCFS CPU Scheduling Algorithm, with the help of an Example (4M)

First-Come-First-Served algorithm is the simplest scheduling algorithm is the simplest scheduling algorithm.
Processes are dispatched according to their arrival time on the ready queue. Being a nonpreemptive discipline,
once a process has a CPU, it runs to completion. The FCFS scheduling is fair in the formal sense or human sense
of fairness but it is unfair in the sense that long jobs make short jobs wait and unimportant jobs make important
jobs wait.
FCFS is more predictable than most of other schemes since it offers time. FCFS scheme is not useful in
scheduling interactive users because it cannot guarantee good response time. The code for FCFS scheduling is
simple to write and understand. One of the major drawback of this scheme is that the average time is often quite
long.
The First-Come-First-Served algorithm is rarely used as a master scheme in modern operating systems but it is
often embedded within other schemes.
Example: Process Burst Time
P1 24
P2 3
P3 3

Suppose that the processes arrive in the order: P1 , P2 , P3


The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17

Das könnte Ihnen auch gefallen