r13 cse Os Lab Manual

Embed Size (px)

DESCRIPTION

Lab manual

Citation preview

OPERATING SYSTEMS LAB MANUAL

INDEX

1. Simulate the following CPU scheduling algorithmsi. Round Robinii. SJFiii. FCFSiv. Priority2. Loading executable programs into memory and execute system call implementation read(),write(), open() and close()3. Multiprogramming-Memory management implementation of fork(),wait(),exec() & exit() system calls4. Simulate all file allocation strategiesi. Sequentialii. Indexed iii. Linked5. Simulate MVT and MFT6. Simulate all File Organization Techniquesi. Single level directoryii. Two level iii. Hierarchical iv. DAG7. Simulate Bankers algorithm for deadlock avoidance8. Simulate Bankers Algorithm for Deadlock prevention9. Simulate all page replacement algorithmsi. FIFO ii. LRUiii. LFU Etc. 10. Simulate Paging Technique of memory management.

1) Simulate the following CPU scheduling algorithms

a) FCFSb) SJFc) Priorityd) Round Robin

Description:FCFS (first-come-first-serve) Scheduling First-come, First served is simplest scheduling algorithm Ready queue is a FIFO queue: Longest waiting process at the front of queue New ready processes join the rear Non-preemptive: executes until voluntarily gives up CPU finished or waits for some event Problem: CPU bound process may require a long CPU burst Other processes, with very short CPU bursts, wait in queue reduces CPU and I/O device utilization

SJF (shortest-job-first) Scheduling Assume the next burst time of each process is known SJF selects process which has the shortest burst time Optimal algorithm because it has the shortest average waiting time Impossible to know in advance OS knows the past burst times- make a prediction using an average Non-preemptive Or preemptive Shortest remaining time first Interrupts running process if a new process enters the queue New process must have shorter burst than remaining time

Round Robin Scheduling Similar to FCFS, but preemption to switch between processes Time quantum(time slice) is a small unit of time (10 to 100 ms) Process is executed on the CPU for at most one time quantum Implemented by using the ready queue as a circular queue Head process gets the CPU Uses less than a time quantum implies gives up the CPU voluntary Uses full time quantum implies timer will cause an interrupt

Context switch will be executed Process will be put at the tail of queue

Priority Scheduling Assume a priority is associated with each process Assume all processes arrive at the same time Select highest priority process from the ready queue Let T be the next CPU burst of a process SJF is a special case of priority scheduling Equal-priority processes are scheduled in FCFS order PRIORITY can be preemptive or Non-preemptive Priorities can be defined internally Memory requirements, number of open files, burst times

a) FCFS:

AIM: A program to simulate the FCFS CPU scheduling algorithm

PROGRAM:

#include#includestruct process{ int at,ts,st,ft,ta; float nta;};main(){ struct process p[20]; int n,i,j; float tamean=0,ntamean=0; clrscr(); printf("\nEnter Number of Processes:: "); scanf("%d",&n); for(i=0;i---------------------------------------------Do U want to continue ::(Y:n) n */

II. Index file Allocation Method

PROGRAM :

#include#include#includeint n;void main(){ int b[20],b1[20],i,j,blocks[20][20],sz[20]; char F[20][20],S[20],ch; clrscr(); printf("\n Enter no. of Files ::"); scanf("%d",&n); for(i=0;i---------------------------------------------Do U want to continue ::(Y:n)0

III. Linked file Allocation Method

PROGRAM

#include#include#includeint n;void main(){ int b[20],b1[20],i,j,blocks[20][20],sz[20]; char F[20][20],S[20],ch; int sb[20],eb[20],x; clrscr(); printf("\n Enter no. of Files ::"); scanf("%d",&n); for(i=0;i104->600->---------------------------------------------

Do U want to continue ::(Y:n)1

Enter the Filename ::sudee

Fname Fsize Bsize Nblocks Blocks---------------------------------------------sudee 1 1024 1 200->201->500->---------------------------------------------Do U want to continue ::(Y:n)0

5. Simulate the MVT and MFT.Description:Memory management1. CPU runs program instructions only when program is in memory. 1. Programs do I/O sometimes IMPLY CPU wasted.1. Solution : Multiprogramming1. Multiple programs share the memory1. One program at a time gets CPU1. Simultaneous resource possession1. Better performance

Multiple Programming with Fixed Number of Tasks (MFT):IBM in their Mainframe Operating system OS/MFT implements the MFT concept. OS/MFT uses fixed partitioning concept to load programs into main memory.Fixed Partitioning:1. In fixed partitioning concept, RAM is divided into set of fixed partitions of equal size.1. Programs having the size less than the partition size are loaded into memory1. Programs having size more than the size of partition size is rejected1. The program having the size less than the partition size will lead to internal fragmentation1. If all partitions are allocated and if a new program is to be loaded, the program that leads to maximum internal fragmentation can be replaced.

Multi-programming with variable number of tasks (MVT):IBM in their Mainframe Operating system OS/MVT implements the MVT concept. OS/MVT uses dynamic partitioning concept to load programs into main memory.

Dynamic Partitioning: Initially RAM is portioned according to the size of programs to be loaded into Memory till such time that no other program can be loaded. The left over memory is called a hole which is too small to fit any process When a new program is to be loaded into memory look for the partition, which leads to least external fragmentation and load the program. The space that is not used in a partition is called as external fragmentation

Multi-programming with variable number of tasks (MVT):

#include#includemain(){ static int jobs[20][20],flag[10]; int ch; static int i,k,nj,nb,tms; clrscr(); printf("Enter time"); /* reading time */ scanf("%d",&tms); printf("Enter no. of jobs"); /* reading no of jobs */ scanf("%d",&nj); printf("Enter job information 1.jobid 2.jobsize"); for(i=0;inc=0; if((*root)->nc==0) gap=rx-lx; else gap=(rx-lx)/(*root)->nc; for(i=0;inc;i++) create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);}else (*root)->nc=0;} } display(node *root) { int i; settextstyle(2,0,4); settextjustify(1,1); setfillstyle(1,BLUE); setcolor(14); if(root!=NULL) {for(i=0;inc;i++){line(root->x,root->y,root->link[i]->x,root->link[i]->y);} if(root->ftype==1)bar3d(root->x-20, root->y-10,root->x+20,root->y+10,0,0);elsefillellipse(root->x,root->y,20,20);outtextxy(root->x,root->y,root->name);for(i=0;inc;i++){display(root->link[i]);} } }

7. Simulate Bankers algorithm for deadlock avoidanceAim: To simulate bankers algorithm for deadlock avoidanceDescription:Deadlock DefinitionA set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause (including itself).

Waiting for an event could be: waiting for access to a critical section waiting for a resource Note that it is usually a non-preemptable (resource). Preemptable resources can be yanked away and given to another. Conditions for Deadlock Mutual exclusion: resources cannot be shared. Hold and wait: processes request resources incrementally, and hold on to what they've got. No preemption : resources cannot be forcibly taken from processes. Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain. Strategies for dealing with Deadlock ignore the problem altogether detection and recovery avoidance by careful resource allocation prevention by structurally negating one of the four necessary conditions. Deadlock AvoidanceAvoid actions that may lead to a deadlock. Think of it as a state machine moving from one state to another as each instruction is executed.

Safe StateSafe state is one where It is not a deadlocked state

BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE#include#includemain(){ int a[10][10],c[10][10],r[10],av[10],ca[10][10],i,j,k,n,m,temp=0,tem,ch; clrscr(); printf("enter no processes"); scanf("%d",&m); printf("enter no of resources"); scanf("%d",&n); printf("\nenter claim\n"); for(i=0;i