Академический Документы
Профессиональный Документы
Культура Документы
com
Question 1:
A time sharing system is to be designed to support a large number of users. List all the
considerations which influence the choice of the time slice. Justify each consideration.
Ans. Time slicing is a scheduling mechanism/way used in time sharing systems. It is also
termed as Round Robin scheduling. The aim of Round Robin scheduling or time slicing
scheduling is to give all processes an equal opportunity to use CPU. In this type of
scheduling, CPU time is divided into slices that are to be allocated to ready processes. Short
processes may be executed within a single time quantum. Long processes may require
several quanta. The Duration of time slice or Quantum The performance of time slicing
policy is heavily dependent on the size/duration of the time quantum. When the time
quantum is very large, the Round Robin policy becomes a FCFS policy. Too short quantum
causes too many process/context switches and reduces CPU efficiency. So the choice of
time quanta is a very important design decision. Switching from one process to another
requires a certain amount of time to save and load registers, update various tables and lists
etc. Consider, as an example, process switch or context switch takes 5 m sec and time slice
duration be 20 m sec. Thus CPU has to spend 5 m sec on process switching again and again
wasting 20% of CPU time. Let the time slice size be set to say 500 m sec and 10 processes
are in the ready queue. If P1 starts executing for first time slice then P2 will have to wait for
1/2 sec; and waiting time for other processes will increase. The unlucky last (P10) will have
to wait for 5 sec, assuming that all others use their full time slices. To conclude setting the
time slice. 1.Too short will cause too many process switches and will lower CPU
efficiency. 2.Setting too long will cause poor response to short interactive
processes. 3.A quantum around 100 m sec is usually reasonable.
Question 2:
(a) List the conditions under which memory allocation decisions can delay the initiation or
scheduling of a program.
Ans.
• Batch systems work in terms of "jobs". Many modern process concepts are
still expressed in terms of jobs, ( e.g. job scheduling ), and the two terms are
often used interchangeably. 3.1.1 The Process
• Process memory is divided into four sections as shown in Figure 3.1 below:
• The text section comprises the compiled program code, read in from non-
volatile storage when the program is launched.
• The data section stores global and static variables, allocated and initialized
prior to executing main.
• The heap is used for dynamic memory allocation, and is managed via calls to
new, delete, malloc, free, etc.
• The stack is used for local variables. Space on the stack is reserved for local
variables when they are declared ( at function entrance or elsewhere,
depending on the language ), and the space is freed up when the variables go
out of scope. Note that the stack is also used for function return values, and
the exact mechanisms of stack management may be language specific.
• Note that the stack and the heap start at opposite ends of the process's free
space and grow towards each other. If they should ever meet, then either a
stack overflow error will occur, or else a call to new or malloc will fail due to
insufficient memory available.
• When processes are swapped out of memory and later restored, additional
information must also be stored and restored. Key among them are the
program counter and the value of all program registers.
(b) Consider the following set of processes with arrival time and CPU execution time given in
milliseconds. A process with a larger priority number has a higher priority. If any assumptions
made by you, state them.
Process Arrival Executi Priority
Time on
Time
P1 0 09 1
P2 2 06 2
P3 3 04 3
P4 3 03 4
P5 6 04 5
(i) Draw the Gantt charts illustrating the execution of these processes using the FCFS, SJF,
Round Robin(with quantum = 2).
Ans.
(ii) Also calculate the average turn around time, average waiting time, processor utilization and
throughput for each of the algorithms mentioned in (i).
Ans.
(c) Explain the trade-offs involved in choosing the size of a time slice for a round
robin algorithm.
Ans.
Round robin is the scheduling algorithm used by the CPU during execution of the process . Round
robin is designed specifically for time sharing systems . It is similar to first come first serve
scheduling algorithm but the preemption is the added functionality to switch between the
processes . A small unit of time also known as time slice or quantum is set/defined . The ready
queue works like circular queue .All processes in this algorithm are kept in the circular queue also
known as ready queue . Each New process is added to the tail of the ready/circular queue . By using
this algorithm , CPU makes sure, time slices ( any natural number ) are assigned to each process in
equal portions and in circular order , dealing with all process without any priority . It is also known
as cyclic executive . The main advantage of round robin algorithm over first come first serve
algorithm is that it
is starvation free . Every process will be executed by CPU for fixed interval of time (which is set as
time slice ) . So in this way no process left waiting for its turn to be executed by the CPU . Round
robin algorithm is simple and easy to implement . The name round robin comes from the principle
known as round robin in which every person takes equal share of something in turn .
Pseudo Code :
* CPU scheduler picks the process from the circular/ready queue , set a timer to interrupt it after
> CPU will proceed with the next process in the ready queue / circular queue .
> Executed process is then placed at the tail of the circular / ready querue by applying the context
switch
> CPU scheduler then proceeds by selecting the next process in the ready queue .
Here , User can calculate the average turnaround time and average waiting time along with the
starting and finishing time of each process
Turnaround time : Its the total time taken by the process between starting and the completion
Waiting time : Its the time for which process is ready to run but not executed by CPU scheduler.
Question 3:
Describe the following disk scheduling policies: First Come First Serve (FCFS), Shortest
Seek Time First, SCAN, C-SCAN, Look and C-Look. Show the disk arm movement and
calculate the number of tracks traversed using all of the policies if the disk has 200 tracks
and the requested tracks, in the order received, are 65, 48, 39, 08, 99, 164, 152, 38, 124.
Ans.
Question 4:
(a) The Cigarette-Smokers Problem: Consider a system with three smoker processes and one
agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and
smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One of the
smoker processes has paper, another has tobacco, and the third has matches. The agent has an
infinite supply of all the three materials. The agent places two of the ingredients on the table. The
smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent
on completion. The agent then puts out another two of the three ingredients, and the cycle
repeats. Write an interactive C/C++ program to synchronize the agent and the smokers, using
semaphores.
Ans.
sem_post(&agent_ready);
usleep(rand() % 50000);
} return NULL; }
// This semaphore gives the pusher exclusive access to the items on the table sem_t pusher_lock;
/** * The pusher is responsible for releasing the proper smoker semaphore when the * right item's
are on the table.
{ items_on_table[(pusher_id + 1) % 3] = false;
sem_post(&smoker_semaphors[(pusher_id + 2) % 3]);
Else
sem_post(&pusher_lock);
}
return NULL;
// Seed our random number since we will be using random numbers srand(time(NULL));
// There is only one agent semaphore since only one set of items may be on //
the table at any given time. A values of 1 = nothing on the table sem_init(&agent_ready, 0, 1);
// Initialize the semaphores for the smokers and pusher for (int i = 0; i < 3; ++i)
{
sem_init(&smoker_semaphors[i], 0, 0); sem_init(&pusher_semaphores[i], 0, 0);
}
// Smoker ID's will be passed to the threads. Allocate the ID's on the stack int smoker_ids[6];
pthread_t smoker_threads[6];
// Create the 6 smoker threads with IDs for (int i = 0; i < 6; ++i)
smoker_ids[i] = i;
return 0;
// Pusher ID's will be passed to the threads. Allocate the ID's on the stack int pusher_ids[6];
pthread_t pusher_threads[6];
{ pusher_ids[i] = i;
return 0;
// Agent ID's will be passed to the threads. Allocate the ID's on the stack int agent_ids[6];
pthread_t agent_threads[6];
{
agent_ids[i] =i;
return 0;
// Make sure all the smokers are done smoking for (int i = 0; i < 6; ++i)
{ pthread_join(smoker_threads[i], NULL);
return 0;
(b) The Sleeping Barber Problem: A barbershop consists of a waiting room with n chairs and the
barber room containing the barber chair. If there are no customers to be served, the barber goes to
sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the
shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If
the barber is asleep, the customer wakes up the barber.
i. Write a C / C++ program to coordinate the barber and the customers, using semaphores.
Ans.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *barberShop();
void *waitingRoom();
void checkQueue();
iseed=time(NULL);
srand(iseed);
pthread_t barber,customerM,timer_thread;
pthread_attr_t customerMAttr;
pthread_attr_init(&timerAttr);
pthread_attr_init(&barberAttr);
pthread_attr_init(&customerMAttr);
printf("\n");
pthread_join(customerM,NULL);
return 0;
void *customerMaker()
fflush(stdout);
pthread_t customer[seats+1];
pthread_attr_t customerAttr[seats+1];
while(i<(seats+1))
i++;
pthread_attr_init(&customerAttr[i]);
while(rand()%2!=1) { sleep(1); }
pthread_create(&customer[i],&customerAttr[i],waitingRoom,NULL);
064 pthread_exit(0);
void *waitingRoom()
checkQueue();
sleep(returnTime);
waitingRoom();
}
void *barberShop() { int loop=0;
fflush(stdout);
fflush(stdout);
pthread_mutex_lock(&sleep_mutex);
sleeping=1;
pthread_cond_wait(&barberSleep_cond,&sleep_mutex);
sleeping=0;
pthread_mutex_unlock(&sleep_mutex);
fflush(stdout);
Else
fflush(stdout);
sleep((rand()%20)/5);
current--;
pthread_exit(0);
void checkQueue()
current++;
fflush(stdout);
pthread_cond_signal(&barberSleep_cond);
fflush(stdout);
pthread_mutex_unlock(&queue_mutex);
pthread_mutex_lock(&wait_mutex);
pthread_cond_wait(&barberWorking_cond,&wait_mutex);
pthread_mutex_unlock(&wait_mutex);
return;
fflush(stdout); current--;
pthread_mutex_unlock(&queue_mutex);
return;
ii. Consider the Sleeping-Barber Problem with the modification that there are k barbers
and k barber chairs in the barber room, instead of just one. Write a program to coordinate
the barbers and the customers.
Ans.
There is one barber in the barber shop, one barber chair and n chairs for waiting customers. If there
are no customers, the barber sits down in the barber chair and takes a nap. An arriving customer must
wake the barber. Subsequent arriving customers take a waiting chair if any are empty or leave if all
chairs are full. This problem addresses race conditions. This solution uses three semaphores, one for
customers (counts waiting customers), one for the barber (idle - 0 or busy - 1) and a mutual exclusion
semaphore, mutex. When the barber arrives for work, the barber procedure is executed blocking the
barber on the customer semaphore until a customer arrives. When a customer arrives, the customer
procedure is executed which begins by acquiring mutex to enter a critical region. Subsequent arriving
customers have to wait until the first customer has released mutex. After acquiring mutex, a customer
checks to see if the number of waiting customers is less than the number of chairs. If not, mutex is
released and the customer leaves without a haircut. If there is an available chair, the waiting counter
is incremented, the barber is awaken, the customer releases mutex, the barber grabs mutex, and
begins the haircut. Once the customer's hair is cut, the customer leaves. The barber then checks to see
if there is another customer. If not, the barber takes a nap.
#define CHAIRS 5
Typedef
int semaphore;
semaphore customers = 0;
semaphore barbers=0;
semaphore mutex=1;
int waiting=0;
void barber(void)
while (TRUE)
P(customers);
P(mutex);
waiting=waiting-1;
V(barbers);
V(mutex);
cut_hair();
V(customers);
V(mutex);
P(barbers);
get_haircut();
Else
V(mutex);
Question 5:
Discuss in detail the features, Process management, Memory management, I/O Management, File
management and Security and Protection in Android Operating System (latest version).
Ans.
An operating system is a program that acts as an interface between the user and the computer
hardware and controls the execution of all kinds of programs.
• Memory Management
• Processor Management
• Device Management
• File Management
• Security
• Control over system performance
• Job accounting
• Error detecting aids
• Coordination between other software and users
Memory Management Memory management refers to management of Primary Memory or Main
Memory. Main memory is a large array of words or bytes where each word or byte has its own
address. Main memory provides a fast storage that can be accessed directly by the CPU. For a
program to be executed, it must in the main memory. An Operating System does the following
activities for memory management –
Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not in use.
• In multiprogramming, the OS decides which process will get memory when and how much.
• Allocates the memory when a process requests it to do so.
• De-allocates the memory when a process no longer needs it or has been terminated. Processor
Management In multiprogramming environment, the OS decides which process gets the
processor when and for how much time. This function is called process scheduling. An
Operating System does the following activities for processor management –
• Keeps tracks of processor and status of process. The program responsible for this task is
known as
traffic controller.
• Allocates the processor (CPU) to a process.
• De-allocates processor when a process is no longer required. Device Management An
Operating System manages device communication via their respective drivers. It does the
following activities for device management −
Keeps tracks of all devices. Program responsible for this task is known as the I/O controller.
• Decides which process gets the device when and for how much time.
• Allocates the device in the efficient way.
• De-allocates devices. File Management 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.
• Decides who gets the resources.
• Allocates the resources.
• De-allocates the resources. Other Important Activities Following are some of the important
activities that an Operating System performs –