Os Test 1 Key
Os Test 1 Key
Os Test 1 Key
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
}
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:
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
P1 P3 P2 P4
0 7 8 12 16
Example of SRTF/STCF
CPU selects the process for execution which has the smallest amount of time remaining until completion .
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
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
2. Kernel mode
-mode bit 0
- allows execution of privileged instructions like I/O control, interrupt management etc.
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.
Process Arrival Time Burst Time Completion Time Turn around Time
(AT) (BT) (CT) TAT=(CT-AT)
Gantt Chart:
P1 P2 P3
0 200 400 600
Response Time: Since FIFO follows Non-preemptive scheduling, the response time is the completion time.
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
P1 P2 P3
0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17