Operating Systems Lab Manual: A Practical Guide to Implementing Key Concepts, Lecture notes of Operating Systems

jntuk-jntuh and jntua lab programs with answers

Typology: Lecture notes

2016/2017

Uploaded on 10/23/2017

saipavan-seelamsetty
saipavan-seelamsetty 🇮🇳

5

(1)

2 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OPERATING SYSTEMS
LAB MANUAL
JNTU World
www.alljntuworld.in
JNTU World
Downloaded From JNTU World (http://www.alljntuworld.in)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Operating Systems Lab Manual: A Practical Guide to Implementing Key Concepts and more Lecture notes Operating Systems in PDF only on Docsity!

OPERATING SYSTEMS

LAB MANUAL

JNTU World

COMPUTER SCIENCE AND ENGINEERING

Program Outcomes PO1 Engineering knowledge : Apply the knowledge of mathematics, science, engineering fundamentals, and an engineering specialization to the solution of complex engineering problems. PO2 Problem analysis : Identify, formulate, review research literature, and analyze complex engineering problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and engineering sciences. PO3 Design/development of solutions : Design solutions for complex engineering problems and design system components or processes that meet the specified needs with appropriate consideration for the public health and safety, and the cultural, societal, and environmental considerations. PO4 Conduct investigations of complex problems : Use research-based knowledge and research methods including design of experiments, analysis and interpretation of data, and synthesis of the information to provide valid conclusions. PO5 Modern tool usage : Create, select, and apply appropriate techniques, resources, and modern engineering and IT tools including prediction and modeling to complex engineering activities with an understanding of the limitations. PO6 The engineer and society : Apply reasoning informed by the contextual knowledge to assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice. PO7 Environment and sustainability : Understand the impact of the professional engineering solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable development. PO8 Ethics : Apply ethical principles and commit to professional ethics and responsibilities and norms of the engineering practice. PO9 Individual and team work : Function effectively as an individual, and as a member or leader in diverse teams, and in multidisciplinary settings. PO10 Communication : Communicate effectively on complex engineering activities with the engineering community and with society at large, such as, being able to comprehend and write effective reports and design documentation, make effective presentations, and give and receive clear instructions. PO11 Project management and finance : Demonstrate knowledge and understanding of the engineering and management principles and apply these to one’s own work, as a member and leader in a team, to manage projects and in multidisciplinary environments. PO12 Life-long learning : Recognize the need for, and have the preparation and ability to engage in independent and life-long learning in the broadest context of technological change.

Program Specific Outcomes PSO1 Professional Skills: The ability to research, understand and implement computer programs in the areas related to algorithms, system software, multimedia, web design, big data analytics, and networking for efficient analysis and design of computer-based systems of varying complexity. PSO2 Problem-Solving Skills: The ability to apply standard practices and strategies in software project development using open-ended programming environments to deliver a quality product for business success. PSO3 Successful Career and Entrepreneurship: The ability to employ modern computer languages, environments, and platforms in creating innovative career paths, to be an entrepreneur, and a zest for higher studies.

JNTU World

ATTAINMENT OF PROGRAM OUTCOMES

& PROGRAM SPECIFIC OUTCOMES

Exp. No. Experiment

Program Outcomes Attained

Program Specific Outcomes Attained 1 Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find turnaround time and waiting time. a) FCFS b) SJF c) Round Robin d) Priority

PO1, PO2, PO
PSO

2 ***** Write a C program to simulate multi-level queue scheduling algorithm considering the following scenario. All the processes in the system are divided into two categories – system processes and user processes. System processes are to be given higher priority than user processes. Use FCFS scheduling for the processes in each queue.

PO1, PO2, PO3,
PO4, PO
PSO1, PSO

(^3) Write a C program to simulate the following file allocation strategies. a) Sequential b) Indexed c) Linked

PO1, PO2, PO4 PSO

(^4) Write a C program to simulate the MVT and MFT memory management techniques.

PO1, PO2, PO4 PSO

5 ***** Write a C program to simulate the following contiguous memory allocation techniques a) Worst-fit b) Best-fit c) First-fit

PO1, PO2, PO4,
PO
PSO

(^6) Write a C program to simulate paging technique of memory management.

PO1, PO2, PO4 PSO

(^7) Write a C program to simulate the following file organization techniques a) Single level directory b) Two level directory c) Hierarchical

PO1, PO2, PO4 PSO

(^8) Write a C program to simulate Bankers algorithm for the purpose of deadlock avoidance.

PO1, PO2, PO4 PSO

(^9) *Write a C program to simulate disk scheduling algorithms a) FCFS b) SCAN c) C-SCAN

PO1, PO2, PO4,
PO
PSO

(^10) Write a C program to simulate page replacement algorithms a) FIFO b) LRU c) LFU

PO1, PO2 PSO

(^11) *Write a C program to simulate page replacement algorithms a) Optimal

PO1, PO2, PO12 PSO

(^12) ***** Write a C program to simulate producer-consumer problem using semaphores.

PO1, PO2, PO3,
PO4, PO5, PO
PSO1, PSO

(^13) *Write a C program to simulate the concept of Dining-Philosophers problem.

PO1, PO2, PO3,
PO4, PO5, PO
PSO1, PSO

***** Content beyond the university prescribed syllabi

JNTU World

OPERATING SYSTEMS LABORATORY

OBJECTIVE:

This lab complements the operating systems course. Students will gain practical experience with designing and
implementing concepts of operating systems such as system calls, CPU scheduling, process management,
memory management, file systems and deadlock handling using C language in Linux environment.

OUTCOMES:

Upon the completion of Operating Systems practical course, the student will be able to:
1. Understand and implement basic services and functionalities of the operating system using system
calls.
2. Use modern operating system calls and synchronization libraries in software/ hardware interfaces.
3. Understand the benefits of thread over process and implement synchronized programs using
multithreading concepts.
4. Analyze and simulate CPU Scheduling Algorithms like FCFS, Round Robin, SJF, and Priority.
5. Implement memory management schemes and page replacement schemes.
6. Simulate file allocation and organization techniques.
7. Understand the concepts of deadlock in operating systems and implement them in multiprogramming
system.

JNTU World

for(i=0;i<n;i++) printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n); getch(); }

INPUT Enter the number of processes -- 3 Enter Burst Time for Process 0 -- 24 Enter Burst Time for Process 1 -- 3 Enter Burst Time for Process 2 -- 3

OUTPUT PROCESS BURST TIME WAITING TIME TURNAROUND TIME P0 24 0 24 P1 3 24 27 P2 3 27 30

Average Waiting Time-- 17. Average Turnaround Time -- 27.

1.3.2 SJF CPU SCHEDULING ALGORITHM #include<stdio.h> #include<conio.h> main() { int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg, tatavg; clrscr(); printf("\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n;i++) { p[i]=i; printf("Enter Burst Time for Process %d -- ", i); scanf("%d", &bt[i]);

for(i=0;i<n;i++) for(k=i+1;k<n;k++) if(bt[i]>bt[k]) { temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;

temp=p[i]; p[i]=p[k]; p[k]=temp; } wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++) { wt[i] = wt[i-1] +bt[i-1]; tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; } printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n"); for(i=0;i<n;i++)

JNTU World

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n); getch(); }

INPUT Enter the number of processes -- 4 Enter Burst Time for Process 0 -- 6 Enter Burst Time for Process 1 -- 8 Enter Burst Time for Process 2 -- 7 Enter Burst Time for Process 3 -- 3

OUTPUT PROCESS BURST TIME WAITING TIME TURNAROUND TIME P3 3 0 3 P0 6 3 9 P2 7 9 16 P1 8 16 24 Average Waiting Time -- 7. Average Turnaround Time -- 13.

1.3.3 ROUND ROBIN CPU SCHEDULING ALGORITHM #include<stdio.h> main() { int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max; float awt=0,att=0,temp=0; clrscr(); printf("Enter the no of processes -- "); scanf("%d",&n);

for(i=0;i<n;i++) { printf("\nEnter Burst Time for process %d -- ", i+1); scanf("%d",&bu[i]); ct[i]=bu[i]; } printf("\nEnter the size of time slice -- "); scanf("%d",&t); max=bu[0]; for(i=1;i<n;i++) if(max<bu[i]) max=bu[i]; for(j=0;j<(max/t)+1;j++) for(i=0;i<n;i++) if(bu[i]!=0) if(bu[i]<=t) { tat[i]=temp+bu[i]; temp=temp+bu[i]; bu[i]=0; } else { bu[i]=bu[i]-t; temp=temp+t; } for(i=0;i<n;i++) { wa[i]=tat[i]-ct[i]; att+=tat[i];

JNTU World

for(i=1;i<n;i++) { wt[i] = wt[i-1] + bt[i-1]; tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; }

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME"); for(i=0;i<n;i++) printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage Turnaround Time is --- %f",tatavg/n); getch(); }

INPUT

Enter the number of processes -- 5 Enter the Burst Time & Priority of Process 0 --- 10 3 Enter the Burst Time & Priority of Process 1 --- 1 1 Enter the Burst Time & Priority of Process 2 --- 2 4 Enter the Burst Time & Priority of Process 3 --- 1 5 Enter the Burst Time & Priority of Process 4 --- 5 2

OUTPUT PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME 1 1 1 0 1 4 2 5 1 6 0 3 10 6 16 2 4 2 16 18 3 5 1 18 19 Average Waiting Time is --- 8. Average Turnaround Time is --- 12.

JNTU World

EXPERIMENT 2

2.1 OBJECTIVE

***** Write a C program to simulate multi-level queue scheduling algorithm considering the following scenario. All the processes in the system are divided into two categories – system processes and user processes. System processes are to be given higher priority than user processes. The priority of each process ranges from 1 to 3. Use fixed priority scheduling for all the processes.

2.2 DESCRIPTION

Multi-level queue scheduling algorithm is used in scenarios where the processes can be classified into groups based on property like process type, CPU time, IO access, memory size, etc. In a multi-level queue scheduling algorithm, there will be 'n' number of queues, where 'n' is the number of groups the processes are classified into. Each queue will be assigned a priority and will have its own scheduling algorithm like round-robin scheduling or FCFS. For the process in a queue to execute, all the queues of priority higher than it should be empty, meaning the process in those high priority queues should have completed its execution. In this scheduling algorithm, once assigned to a queue, the process will not move to any other queues.

2.3 PROGRAM

main() { int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp; float wtavg, tatavg; clrscr(); printf("Enter the number of processes --- "); scanf("%d",&n);

for(i=0;i<n;i++) { p[i] = i; printf("Enter the Burst Time of Process %d --- ", i); scanf("%d",&bt[i]); printf("System/User Process (0/1)? --- "); scanf("%d", &su[i]);

} for(i=0;i<n;i++) for(k=i+1;k<n;k++) if(su[i] > su[k]) { temp=p[i]; p[i]=p[k]; p[k]=temp;

temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;

temp=su[i]; su[i]=su[k]; su[k]=temp;

} wtavg = wt[0] = 0; tatavg = tat[0] = bt[0];

for(i=1;i<n;i++) { wt[i] = wt[i-1] + bt[i-1]; tat[i] = tat[i-1] + bt[i];

JNTU World

EXPERIMENT 3

3.1 OBJECTIVE

Write a C program to simulate the following file allocation strategies. a) Sequential b) Linked c) ) Indexed

3.2 DESCRIPTION

A file is a collection of data, usually stored on disk. As a logical entity, a file enables to divide data into meaningful groups. As a physical entity, a file should be considered in terms of its organization. The term "file organization" refers to the way in which data is stored in a file and, consequently, the method(s) by which it can be accessed.

3.2.1 SEQUENTIAL FILE ALLOCATION In this file organization, the records of the file are stored one after another both physically and logically. That is, record with sequence number 16 is located just after the 15th record. A record of a sequential file can only be accessed by reading all the previous records.

3.2.2 LINKED FILE ALLOCATION With linked allocation, each file is a linked list of disk blocks; the disk blocks may be scattered anywhere on the disk. The directory contains a pointer to the first and last blocks of the file. Each block contains a pointer to the next block.

3.2.3 INDEXED FILE ALLOCATION

Indexed file allocation strategy brings all the pointers together into one location: an index block. Each file has its own index block, which is an array of disk-block addresses. The ith^ entry in the index block points to the ith^ block of the file. The directory contains the address of the index block. To find and read the ith^ block, the pointer in the ith^ index-block entry is used.

3.3 PROGRAM
3.3.1 SEQUENTIAL FILE ALLOCATION

#include<stdio.h> #include<conio.h>

struct fileTable { char name[20]; int sb, nob; }ft[30];

void main() { int i, j, n; char s[20]; clrscr(); printf("Enter no of files :"); scanf("%d",&n);

for(i=0;i<n;i++) { printf("\nEnter file name %d :",i+1); scanf("%s",ft[i].name); printf("Enter starting block of file %d :",i+1); scanf("%d",&ft[i].sb); printf("Enter no of blocks in file %d :",i+1); scanf("%d",&ft[i].nob); } printf("\nEnter the file name to be searched -- "); scanf("%s",s); for(i=0;i<n;i++) if(strcmp(s, ft[i].name)==0)

JNTU World

break; if(i==n) printf("\nFile Not Found"); else { printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED\n"); printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob); for(j=0;j<ft[i].nob;j++) printf("%d, ",ft[i].sb+j); } getch(); }

INPUT: Enter no of files :

Enter file name 1 :A Enter starting block of file 1 : Enter no of blocks in file 1 :

Enter file name 2 :B Enter starting block of file 2 : Enter no of blocks in file 2 :

Enter file name 3 :C Enter starting block of file 3 : Enter no of blocks in file 3 : Enter the file name to be searched -- B

OUTPUT:
FILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED
B 102 4 102, 103, 104, 105
3.3.2 LINKED FILE ALLOCATION

#include<stdio.h> #include<conio.h>

struct fileTable { char name[20]; int nob; struct block *sb; }ft[30];

struct block { int bno; struct block *next; };

void main() { int i, j, n; char s[20]; struct block *temp; clrscr(); printf("Enter no of files :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter file name %d :",i+1); scanf("%s",ft[i].name);

JNTU World

}ft[30];

void main() { int i, j, n; char s[20]; clrscr(); printf("Enter no of files :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter file name %d :",i+1); scanf("%s",ft[i].name); printf("Enter no of blocks in file %d :",i+1); scanf("%d",&ft[i].nob); printf("Enter the blocks of the file :"); for(j=0;j<ft[i].nob;j++) scanf("%d",&ft[i].blocks[j]); }

printf("\nEnter the file name to be searched -- "); scanf("%s",s); for(i=0;i<n;i++) if(strcmp(s, ft[i].name)==0) break; if(i==n) printf("\nFile Not Found"); else { printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED"); printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob); for(j=0;j<ft[i].nob;j++) printf("%d, ",ft[i].blocks[j]); } getch(); }

INPUT:

Enter no of files : 2

Enter file 1 : A Enter no of blocks in file 1 : 4 Enter the blocks of the file 1 : 12 23 9 4

Enter file 2 : G Enter no of blocks in file 2 : 5 Enter the blocks of the file 2 : 88 77 66 55 44 Enter the file to be searched : G

OUTPUT: FILE NAME NO OF BLOCKS BLOCKS OCCUPIED G 5 88, 77, 66, 55, 44

JNTU World

EXPERIMENT 4

4.1 OBJECTIVE

Write a C program to simulate the MVT and MFT memory management techniques

4.2 DESCRIPTION

MFT (Multiprogramming with a Fixed number of Tasks) is one of the old memory management techniques in which the memory is partitioned into fixed size partitions and each job is assigned to a partition. The memory assigned to a partition does not change. MVT (Multiprogramming with a Variable number of Tasks) is the memory management technique in which each job gets just the amount of memory it needs. That is, the partitioning of memory is dynamic and changes as jobs enter and leave the system. MVT is a more ``efficient'' user of resources. MFT suffers with the problem of internal fragmentation and MVT suffers with external fragmentation.

4.3 PROGRAM
4.3.1 MFT MEMORY MANAGEMENT TECHNIQUE

#include<stdio.h> #include<conio.h>

main() { int ms, bs, nob, ef,n, mp[10],tif=0; int i,p=0;

clrscr(); printf("Enter the total memory available (in Bytes) -- "); scanf("%d",&ms); printf("Enter the block size (in Bytes) -- "); scanf("%d", &bs); nob=ms/bs; ef=ms - nob*bs; printf("\nEnter the number of processes -- "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter memory required for process %d (in Bytes)-- ",i+1); scanf("%d",&mp[i]); }

printf("\nNo. of Blocks available in memory -- %d",nob); printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION"); for(i=0;i<n && p<nob;i++) { printf("\n %d\t\t%d",i+1,mp[i]); if(mp[i] > bs) printf("\t\tNO\t\t---"); else { printf("\t\tYES\t%d",bs-mp[i]); tif = tif + bs-mp[i]; p++; } } if(i<n) printf("\nMemory is Full, Remaining Processes cannot be accomodated");

printf("\n\nTotal Internal Fragmentation is %d",tif); printf("\nTotal External Fragmentation is %d",ef); getch();

JNTU World

getch(); }

INPUT Enter the total memory available (in Bytes) -- 1000

Enter memory required for process 1 (in Bytes) -- 400 Memory is allocated for Process 1 Do you want to continue(y/n) -- y

Enter memory required for process 2 (in Bytes) -- 275 Memory is allocated for Process 2 Do you want to continue(y/n) -- y

Enter memory required for process 3 (in Bytes) -- 550

OUTPUT Memory is Full Total Memory Available -- 1000

PROCESS MEMORY ALLOCATED 1 400 2 275

Total Memory Allocated is 675 Total External Fragmentation is 325

JNTU World

EXPERIMENT 5

5.1 OBJECTIVE

***** Write a C program to simulate the following contiguous memory allocation techniques a) Worst-fit b) Best-fit c) First-fit

5.2 DESCRIPTION

One of the simplest methods for memory allocation is to divide memory into several fixed-sized partitions. Each partition may contain exactly one process. In this multiple-partition method, when a partition is free, a process is selected from the input queue and is loaded into the free partition. When the process terminates, the partition becomes available for another process. The operating system keeps a table indicating which parts of memory are available and which are occupied. Finally, when a process arrives and needs memory, a memory section large enough for this process is provided. When it is time to load or swap a process into main memory, and if there is more than one free block of memory of sufficient size, then the operating system must decide which free block to allocate. Best-fit strategy chooses the block that is closest in size to the request. First-fit chooses the first available block that is large enough. Worst-fit chooses the largest available block.

5.3 PROGRAM
5.3.1 WORST-FIT

#include<stdio.h> #include<conio.h> #define max 25

void main() { int frag[max],b[max],f[max],i,j,nb,nf,temp; static int bf[max],ff[max]; clrscr();

printf("\n\tMemory Management Scheme - First Fit"); printf("\nEnter the number of blocks:"); scanf("%d",&nb); printf("Enter the number of files:"); scanf("%d",&nf); printf("\nEnter the size of the blocks:-\n"); for(i=1;i<=nb;i++) { printf("Block %d:",i); scanf("%d",&b[i]); } printf("Enter the size of the files :-\n"); for(i=1;i<=nf;i++) { printf("File %d:",i); scanf("%d",&f[i]); } for(i=1;i<=nf;i++) { for(j=1;j<=nb;j++) { if(bf[j]!=1) { temp=b[j]-f[i]; if(temp>=0) { ff[i]=j; break; } } }

JNTU World