Вы находитесь на странице: 1из 22

www.ignousite.blogspot.

com

Course Code : MCS-041


Course Title : Operating Systems
Assignment Number : MCA(IV)/041/Assignment/2018-19
Maximum Marks : 100
Weightage : 25%
Last Date of Submission : 15th October, 2018 (for July session)
15th April, 2019(for January session)

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.

• A process is an instance of a program in execution.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

• 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.

Figure 3.1 - A process in memory

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

3.1.2 Process State


• Processes may be in one of 5 states, as shown in Figure 3.2 below.
• New - The process is in the stage of being created.
• Ready - The process has all the resources available that it needs to run, but the
CPU is not currently working on this process's instructions.
• Running - The CPU is working on this process's instructions.
• Waiting - The process cannot run at the moment, because it is waiting for some
resource to become available or for some event to occur. For example the
process may be waiting for keyboard input, disk access request, inter-process
messages, a timer to go off, or a child process to finish.
• Terminated - The process has completed.
• The load average reported by the "w" command indicate the average number
of processes in the "Ready" state over the last 1, 5, and 15 minutes, i.e.
processes who have everything they need to run but cannot because the CPU
is busy doing something else.
• Some systems may have other states besides the ones listed here.

(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

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

(i) Draw the Gantt charts illustrating the execution of these processes using the FCFS, SJF,
Round Robin(with quantum = 2).
Ans.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

(ii) Also calculate the average turn around time, average waiting time, processor utilization and
throughput for each of the algorithms mentioned in (i).
Ans.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

(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

1 time slice / quantum and dispatches it .

* If process has burst time less than 1 time slice/quantum

> Process will leave the CPU after the completion

> CPU will proceed with the next process in the ready queue / circular queue .

else If process has burst time longer than 1 time slice/quantum

> Timer will be stopped . It cause interruption to the OS .

> 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

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

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.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

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.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

printf("\033[0;37mSmoker %d \033[0;32m<<\033[0m Now making the a cigarette\n", smoker_id);


usleep(rand() % 50000);

sem_post(&agent_ready);

// We're smoking now printf("\033[0;37mSmoker %d \033[0;37m--\033[0m Now smoking\n",


smoker_id);

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.

*/ void* pusher(void* arg)

{ int pusher_id = *(int*) arg;

for (int i = 0; i < 12; ++i)

{ // Wait for this pusher to be needed sem_wait(&pusher_semaphores[pusher_id]);


sem_wait(&pusher_lock);

// Check if the other item we need is on the table if (items_on_table[(pusher_id + 1) % 3])

{ items_on_table[(pusher_id + 1) % 3] = false;

sem_post(&smoker_semaphors[(pusher_id + 2) % 3]);

else if (items_on_table[(pusher_id + 2) % 3]) { items_on_table[(pusher_id + 2) % 3] = false;


sem_post(&smoker_semaphors[(pusher_id + 1) % 3]);

Else

// The other item's aren't on the table yet items_on_table[pusher_id] = true;

sem_post(&pusher_lock);

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

}
return NULL;

/** * The agent puts items on the table */

void* agent(void* arg)

int agent_id = *(int*) arg;

for (int i = 0; i < 6; ++i) { usleep(rand() % 200000);

// Wait for a lock on the agent sem_wait(&agent_ready);

// Release the items this agent gives out sem_post(&pusher_semaphores[agent_id]);


sem_post(&pusher_semaphores[(agent_id + 1) % 3]);

// Say what type of items we just put on the table

printf("\033[0;35m==> \033[0;33mAgent %d giving out %s\033[0;0m\n",


agent_id, smoker_types[(agent_id + 2) % 3]);
}
return NULL;

/** * The main thread handles the agent's arbitration of items. */

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

// 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);

// Initalize the pusher lock semaphore sem_init(&pusher_lock, 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);
}

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

// 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;

if (pthread_create(&smoker_threads[i], NULL, smoker, &smoker_ids[i]) == EAGAIN)

perror("Insufficient resources to create thread");

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];

for (int i = 0; i < 3; ++i)

{ pusher_ids[i] = i;

if (pthread_create(&pusher_threads[i], NULL, pusher, &pusher_ids[i]) == EAGAIN)

perror("Insufficient resources to create thread");

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];

for (int i = 0; i < 3; ++i)

{
agent_ids[i] =i;

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

if (pthread_create(&agent_threads[i], NULL, agent, &agent_ids[i]) == EAGAIN) { perror("Insufficient


resources to create thread");

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>

#define seats 6 void *customerMaker();

void *barberShop();

void *waitingRoom();

void checkQueue();

pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

pthread_cond_t barberSleep_cond = PTHREAD_COND_INITIALIZER;

pthread_cond_t barberWorking_cond = PTHREAD_COND_INITIALIZER; int returnTime=5,current=0,


sleeping=0, iseed;

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

iseed=time(NULL);

srand(iseed);

//declare barber thread;

pthread_t barber,customerM,timer_thread;

pthread_attr_t barberAttr, timerAttr;

pthread_attr_t customerMAttr;

//define barber, and cutomerMaker default attributes

pthread_attr_init(&timerAttr);

pthread_attr_init(&barberAttr);

pthread_attr_init(&customerMAttr);

printf("\n");

//create cutomerMaker pthread_create(&customerM,&customerMAttr,customerMaker,NULL);


//create barber pthread_create(&barber,&barberAttr,barberShop,NULL);
pthread_join(barber,NULL);

pthread_join(customerM,NULL);

return 0;

void *customerMaker()

049 int i=0;

printf("*Customer Maker Created*\n\n");

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

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()

//take seat pthread_mutex_lock(&queue_mutex);

checkQueue();

sleep(returnTime);

waitingRoom();

}
void *barberShop() { int loop=0;

printf("The barber has opened the store.\n");

fflush(stdout);

while(loop==0) { if(current==0) { printf("\tThe shop is empty, barber is sleeping.\n");

fflush(stdout);

pthread_mutex_lock(&sleep_mutex);

sleeping=1;

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

pthread_cond_wait(&barberSleep_cond,&sleep_mutex);

sleeping=0;

pthread_mutex_unlock(&sleep_mutex);

printf("\t\t\t\tBarber wakes up.\n");

fflush(stdout);

Else

printf("\t\t\tBarber begins cutting hair.\n");

fflush(stdout);

sleep((rand()%20)/5);

current--;

printf("\t\t\t\tHair cut complete, customer leaving store.\n");


pthread_cond_signal(&barberWorking_cond);

pthread_exit(0);

void checkQueue()

current++;

printf("\tCustomer has arrived in the waiting room.\t\t\t\t\t\t\t%d Customers in store.\n",current);


fflush(stdout);

printf("\t\tCustomer checking chairs.\n");

fflush(stdout); if(current<seats) { if(sleeping==1) { printf("\t\t\tBarber is sleeping, customer wakes


him.\n");

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

fflush(stdout);

pthread_cond_signal(&barberSleep_cond);

printf("\t\tCustomer takes a seat.\n");

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;

if(current>=seats) { printf("\t\tAll chairs full, leaving store.\n");

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

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

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);

/*go to sleep if no customers*/

P(mutex);

waiting=waiting-1;

V(barbers);

V(mutex);

cut_hair();

void customer(void) { P(mutex);

if (waiting lessthan CHAIRS) { waiting=waiting+1;

V(customers);

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

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.

Following are some of important functions of an operating System.

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

• 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 –

Ignou Study Helper – Sunil Poonia


www.ignousite.blogspot.com

• Security − By means of password and similar other techniques, it prevents unauthorized


access to programs and data.
• Control over system performance − Recording delays between request for a service and
response from the system.
• Job accounting − Keeping track of time and resources used by various jobs and users.
• Error detecting aids − Production of dumps, traces, error messages, and other debugging
and error detecting aids.
• Coordination between other softwares and users − Coordination and assignment of
compilers, interpreters, assemblers and other software to the various users of the computer
systems.

Ignou Study Helper – Sunil Poonia

Вам также может понравиться