OSFinalFully
OSFinalFully
EXPERIMENT 1
Aim-: Study of hardware and software requirements of different
operating systems(UNIX, LINUX, WINDOWS XP, WINDOWS7/8.
OPERATING SYSTEM
An operating system acts as an interface between the software and different parts of the
computer or the computer hardware. The operating system is designed in such a way that
it can manage the overall resources and operations of the computer. It is a fully
integrated set of specialized programs that handle all the operations of the computer.
Disadvantages
● Security Concerns
● Paid software
● Infection assults
● Rebooting a framework
● High Performance System Required
2. LINUX
Advantages
● Open Source
● No antivirus software needed
● Low system specifications
● No reboot needed for installing/uninstalling
● Less disk space needed
Disadvantages
● Software compatibility
● Gaming
● Limited software Availability
● Hardware Compatibility
● Learning curve
3. UNIX Advantages
● Very stable
● Less code to execute
● Ideal for web hosting
● Safe and secure
● Multitasking
EXPERIMENT 2
AIM- Execute various UNIX system calls for
1. Process Management
2. File Management
3. Input/Output System Calls
SYSTEM CALL
The interface between a process and an operating system is provided by system
calls. In general, system calls are available as assembly language instructions.
They are also included in the manuals used by the assembly-level programmers.
Process Control:-
These system calls deal with processes such as process creation, process
termination etc.
File Management:-
These system calls are responsible for file manipulation such as creating a file,
reading a file, writing into a file etc.
Input/Output Management:-
These system calls are responsible for device manipulation such as reading
from device buffers, writing into device buffers etc.
Some of the examples of all the above types of system calls in Windows and UNIX
are given as follows −
COMMONLY USED UNIX SYSTEM CALLS FOR PROCESS MANAGEMENT:
fork(): Creates a new process by duplicating the existing process.
wait(): Suspends the execution of the current process until one of its child processes
terminates.
waitpid(): Suspends the execution of the current process until a specific child
process terminates.
waitid(): Suspends the execution of the current process until a specific child process
or group of child processes terminates.
exit(): Terminates the current process and returns the exit status to the parent process.
kill(): Sends a signal to a specified process.
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA
Experiments 3.1
Source Code:
#include <stdio.h>
void findwt(int wt[],char p[],int bt[],int n)
{
wt[0]=0; for(int i=1;i<n;i++)
wt[i]=bt[i- 1]+wt[i-1];
}
int main()
{
int n; printf("give the number of processes ");
scanf("%d",&n);
char p[n];
int
bt[n],wt[n],tat[n
]; for(int
i=0;i<n;i++)
{ printf("enter process and burst time ");
scanf("%s %d",&p[i],&bt[i]);
}
findwt(wt,p,bt,n);
findtat(wt,p,bt,n,tat);
printf("TABLE\n");
printf("Process\tBurst\tWait\
tTAT"); for(int i=0;i<n;i++)
{ printf("\n");
printf("%c \t%d \t
%d
t
%d",p[i],bt[i],wt[i]
,tat[i]); printf("\
n");
}
printf("avg turn around time : %f\
n",avgtat(tat,n)); printf("avg wait time :
%f",avgwt(wt,n));
return 0;
}
OUTPUT
EXPERIMENT 3.2
Source Code:
#include<stdio.h
> int main(){
int
b[20],p[20],w[20],t[20],i,j,n,total=0,totalT=0,pos,temp;
float aW,aT;
printf("Enter the number of process");
scanf("%d",&n);
printf("\nEnter burst time:\n");
for(int i=0;i<n;i++){ printf("p
%d\t",i+1); scanf("%d",&b[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++)
{ if(b[j]<b[pos]){
pos=j;
}
}
temp=b[i];
b[i]=b[pos];
b[pos]=temp
:
temp=p[i];
p[i]=p[pos];
p[pos]=temp
;
}
w[0]=0;
for(i=1;i<n;i++)
{ w[i]=0;
for(j=0;j<i;j++)
{ w[i]+=b[j];
}
total+=w[i];
}
aW=(float)total/n; printf("\nProcess\tBurstTime\tWaitingTime\
tTurnaroundTime"); for(int i=0;i<n;i++){
Operating System Lab (KCS-451) PRASHANT YADAV 2100910100131
JSS Academy of Technical Education-NOIDA
OUTPUT
EXPERIMENT 3.3
AIM: Implement SRTN
SOURCE CODE:
#include
<stdio.h> int
main()
{
int
a[10],b[10],x[10],i,j,smallest,count=0,ti
me,n; double avg=0,tt=0,end;
printf("enter the number of Processes:
"); scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter process p%d arrival time and burst time
",i+1); scanf("%d %d",&a[i],&b[i]);
}
for(i=0;i<n;i
++)
x[i]=b[i];
b[9]=9999;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i
++)
{
if(a[i]<=time && b[i]<b[smallest] &&
b[i]>0 ) smallest=i;
}
b[smallest]--;
if(b[smallest]=
=0)
{ count++;
end=time
+1;
avg=avg+end-a[smallest]-
x[smallest]; tt= tt+end-
a[smallest];
}}
printf("\n\nAverage waiting time = %lf\
n",avg/n); printf("Average Turnaround
time = %lf",tt/n); return 0;}
OUTPUT:
SOURCE CODE:
#include
<stdio.h> int
main()
{
int
a[10],b[10],x[10],i,j,smallest,count=0,time,n,
p[10]; int wt[10],tat[10];
double avg=0,tt=0,end;
printf("enter the number of Processes:
"); scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter process p%d arrival time and burst time and priority
",i+1); scanf("%d %d
%d",&a[i],&b[i],&p[i]);
}
for(i=0;i<n;i
++) x[i]=b[i];
b[9]=99
99;
p[9]=99
99;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i
++)
{
if(a[i]<=time && p[i]<p[smallest] &&
b[i]>0 ) smallest=i;
}
b[smallest]--;
if(b[smallest]=
=0)
{count++;
end=time+1;
wt[i]=time+1-a[smallest]-
x[smallest]; tat[i]=time+1-
a[smallest]; avg=avg+end-
a[smallest]-x[smallest]; tt=
tt+end-a[smallest];
}}
printf("ID WT TAT\
n"); for(i=0;i<n;i++)
{ printf("%d %d\t%d\n",i+1,wt[i],tat[i]);}
printf("\n\nAverage waiting time = %lf\
n",avg/n); printf("Average Turnaround
time = %lf",tt/n); return 0;
EXPERIMENT 3.5
AIM: Implement Round Robin
SOURCE CODE:
#include<stdio.h
> int main(){
int i, n, total = 0, x, counter = 0, tq;
int wt = 0, tat = 0, a[10], b[10], temp[10];
float aW, aTat;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
x =n;
printf("Enter arrivelTime BurstTime:\n");
for(i = 0; i <n; i++){
scanf("%d%d", &a[i],&b[i]);
temp[i] = b[i];
}
printf("Enter Time Quantum:");
scanf("%d", &tq);
printf("\nProcess\t BurstTime\tTurnaroundTime\tWaitingTime\n");
for(total = 0, i = 0; x != 0;){
if(temp[i] <= tq && temp[i] > 0){
total = total + temp[i];
temp[i] = 0;
counter = 1;
else if(temp[i] > 0)
{ temp[i] = temp[i]
- tq; total = total
+ tq;
}
if(temp[i] == 0 && counter == 1)
{ x--;
printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, b[i], total - a[i], total - a[i] - b[i]);
wt= wt + total - a[i] - b[i];
tat = tat+ total -
a[i]; counter = 0;
}
if(i == n- 1)
{ i = 0;
}else if(a[i + 1] <= total){
i++;
}else
{i=
0;
}}
aW= wt * 1.0 / n;
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
OUTPUT
EXPERIMENT 5
SOURCE CODE:
#include
<stdio.h> int
main(){
int n,m;
printf("enter number of processes");
scanf("%d",&n);
printf("enter number of types of resources");
scanf("%d",&m);
int alloc[n][m];
int max[n]
[m]; int
avail[m]; int
ind=0;
printf("enter allocation matrix elements");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&alloc[i]
[j]);
}
}
printf("enter maximum matrix elements");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&max[i]
[j]);
}
}
printf("enter available matrix elements");
for(int i=0;i<m;i++){
scanf("%d",&avail[i]);
}
int need[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
need[i][j]=max[i][j]-alloc[i][j];
}
}
int f[n];
for(int i=0;i<n;i++)
{ f[i]=0;
}
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
if(f[i]==0){
int c=0;
for(int j=0;j<m;j++){
if(need[i][j]>avail[j])
{ c=1;
break;
}
}
if(c==0){
ans[ind+
+]=i;
for(int v=0;v<m;v++){ avail[v]+=alloc[i]
[v];
}
f[i]=1
;
}}
}}
int c=1;
for(int i=0;i<n;i++)
{ if(f[i]==0){
c=0;
printf("not in safe state");
break;
}
}
if(c==1){
printf("Following is the safe Sequence\n");
for (int i = 0; i < n-1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return 0;
}
OUTPUT:-
EXPERIMENT 6.1
SOURCE CODE:
#include<stdio.h>
void First_Fit(int process_size[],int n,int block_size[],int m)
{ int i,j;
int allocation[n];
for(i=0;i<n;i++)
{allocation[i]=-1;
}
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
{if(block_size[j]>process_size[i])
{allocation[i]=j;
block_size[j]=block_size[j]-
process_size[i]; break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{ printf(" P%i\t\t", i+1);
printf("%i\t\t", process_size[i]);
if (allocation[i] != -1)
printf("%i", allocation[i] + 1);
else
printf("Not
Allocated"); printf("\n");
}
}
int main()
{int
process_size[]={100,300,500,200,600}; int
n=5;
int block_size[]={212,417,112,426};
int m=4;
First_Fit(process_size,n,block_size,m)
; return 0;
}
OUTPUT:-
EXPERIMENT 6.2
AIM: Implement Worst Fit Contiguous Memory Allocation Technique
SOURCE CODE:
#include <stdio.h>
void WorstFit(int blockSize[], int blocks, int processSize[], int processes)
{int
allocation[processes];
int occupied[blocks];
for(int i = 0; i < processes; i++)
{allocation[i] = -1;
}
for(int i = 0; i < blocks; i++)
{occupied[i] = 0;
}
for (int i=0; i < processes; i++)
{int indexPlaced = -1;
for(int j = 0; j < blocks; j++)
{if(blockSize[j] >= processSize[i] && !occupied[j])
{if (indexPlaced == -1)
{indexPlaced = j;
}
else if (blockSize[indexPlaced] <
blockSize[j]) indexPlaced = j;
}
}
if (indexPlaced != -1)
{allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
blockSize[indexPlaced] -=
processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock
no.\n"); for (int i = 0; i < processes; i++)
{printf("P%d \t\t\t %d \t\t\t", i+1,
processSize[i]); if (allocation[i] != -1)
printf("%d\n",allocation[i]
+ 1); else
printf("Not Allocated\n");
}
}
int main()
{int processSize[] = {100, 300, 500, 200, 600};
int blockSize[] = {212, 417, 112,
426}; int blocks = 4;
int processes = 5;
WorstFit(blockSize, blocks, processSize,
processes); return 0;
}
OUTPUT:-
EXPERIMENT 6.3
AIM: Implement Best Fit Contiguous Memory Allocation Technique
SOURCE CODE:
#include <stdio.h>
void BestFit(int blockSize[], int blocks, int processSize[], int proccesses)
{intallocation[
proccesses];
int
occupied[bloc
ks];
for(int i = 0; i < proccesses; i++)
{allocation[i] = -1;
}
for(int i = 0; i < blocks; i++)
{occupied[i] = 0;
}
for (int i = 0; i < proccesses; i++)
{int indexPlaced = -1;
for (int j = 0; j < blocks; j++)
{if (blockSize[j] >= processSize[i] && !occupied[j])
{if
(indexPlac
ed == -1)
{indexPlac
ed = j;
}
else if (blockSize[j] <
blockSize[indexPlaced])
{indexPlaced = j;
}
}
}
if (indexPlaced != -1)
{allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
}
}
printf("\nProcess No.\tProcess
Size\tBlock no.\n"); for (int i = 0;
i < proccesses; i++)
{printf("%d \t\t\t %d \t\t\t",
i+1, processSize[i]); if
(allocation[i] != -1)
printf("%d\
n",allocation[i] +
1); else
printf("Not Allocated\n");
}
}int main()
{int blockSize[] = {212, 417, 112, 426};
int processSize[] = {100, 300,
500, 200, 600}; int blocks =
4;
int proccesses = 5;
BestFit(blockSize,
blocks,processSize,proccesses);
return 0 ;
} OUTPUT:-
EXPERIMENT 4.1
AIM: Implementation of contiguous file storage allocation technique.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int start[10],num=0,count=0,length[10],j,f=1,i=0;
char name[20][10];
char del[2];
int ch=0;
while(1){
printf("Contiguous file allocation\n");
printf("1.File Creation\n");
printf("2.File Deletion\n");
printf("3.Display File Allocation Table\n");
printf("4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the name of the file\n");
scanf("%s",&name[i][0]);
printf("Enter the start block of the file\n");
scanf("%d",&start[i]);
printf("Enter the length of the file\n");
scanf("%d",&length[i]);
num++;
i++;
if(f==1){
f++;
}
for(j=0;j<num;j++){
if(start[j+1]<=start[j] || start[j+1]>=length[j]){
}
else{
count++;
}
}
if(count==1){
printf("%s cannot be allocated disk space\n",name[i-1]);
}
else{
printf("file %s allocated disk space\n",name[i-1]);
}
break;
case 2:
printf("Enter the name of the file to be deleted\n");
Operating System Lab (KCS-451)
scanf("%s",&del[0]); Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
printf("file %s deleted\n",&del[0]);
f--;
f--;
break;
case 3:
printf("File Allocation Table\n");
printf("File Name Start Block Length\n");
if(f==2){
printf("%s %d %d\n",name[0],start[0],length[0]);
}
for(int k=0,n=1;k<num && n<num ;k++,n++){
if(start[k+1]<=start[k] || start[k+1]>=length[k]){
printf("%s %d %d\n",name[n],start[n],length[n]);
}
}
break;
case 4:
exit(1);
default:
printf("invalid");
}
getchar();
}
return 0;
}
OUTPUT:
EXPERIMENT 4.2
AIM-Write a program to implement Linked list file storage allocation
technique
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
void recursivePart(int pages[])
{ int st,len, k, c, j;
printf("Enter the index of the starting block and its length: ");
scanf("%d%d", &st, &len);
k = len;
if (pages[st] == 0){
for (j = st; j < (st + k); j++){
if (pages[j] == 0)
{ pages[j] = 1;
printf("%---->%d\n", j, pages[j]);
}
else {
printf("The block %d is already allocated \n", j);
k++;
}}}
else
printf("The block %d is already allocated \n", st);
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursivePart(pages);
else
exit(0);
return;}
int main(){
int pages[50], p, a;
for (int i = 0; i < 50; i++)
pages[i] = 0;
printf("Enter the number of blocks already allocated: ");
scanf("%d", &p);
printf("Enter the blocks already allocated: ");
for (int i = 0; i < p; i++)
{
scanf("%d", &a);
pages[a] = 1;}
recursivePart(pages);
getch();
return 0;
}
OUTPUT
EXPERIMENT 4.3
AIM: Write a program to implement Indirect file storage allocation techniques.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
EXPERIMENT 7
AIM Calculation of external and internal fragmentation
1. Free space list of blocks from system
2. List process file from the system
SOURCE CODE:
#include <stdio.h>
#define DISK_SIZE 100
int disk[DISK_SIZE];
#Blocks
struct Block {
int start;
int size;
};
#Process File
struct ProcessFile {
int start;
int size;
};
int calculateExternalFragmentation(struct Block* blocks, int num_blocks) {
int total_gap_size = 0;
int last_block_end = 0;
for (int i = 0; i < num_blocks; i++) {
int block_start = blocks[i].start;
int block_size = blocks[i].size;
if (block_start > last_block_end) {
int gap_size = block_start - last_block_end;
total_gap_size += gap_size;
}
last_block_end = block_start + block_size;
}
int last_block_size = DISK_SIZE - last_block_end;
total_gap_size += last_block_size;
return total_gap_size;
}
int calculateInternalFragmentation(struct ProcessFile* files, int num_files) {
int total_internal_fragmentation = 0;
for (int i = 0; i < num_files; i++) {
int file_size = files[i].size;
int block_size = disk[files[i].start];
int internal_fragmentation = block_size - file_size;
total_internal_fragmentation += internal_fragmentation;
}
return total_internal_fragmentation;
}
int main() {
struct Block blocks[3] = {
{0, 30},
{40, 20},
{70, 10}
};
struct ProcessFile files[2] = {
{0, 15},
{40, 5}
};
for (int i = 0; i < DISK_SIZE; i++) {
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
disk[i] = 0;
}
for (int i = 0; i < sizeof(blocks) / sizeof(blocks[0]); i++) {
for (int j = blocks[i].start; j < blocks[i].start + blocks[i].size; j++) {
disk[j] = 1;
}
}
int external_fragmentation = calculateExternalFragmentation(blocks, sizeof(blocks) /sizeof(blocks[0]));
printf("External Fragmentation: %d\n", external_fragmentation);
int internal_fragmentation = calculateInternalFragmentation(files, sizeof(files) / sizeof(files[0]));
printf("Internal Fragmentation: %d\n", internal_fragmentation);
return 0;
}
OUTPUT
EXPERIMENT 8
AIM Implementation of compaction for the continually changing memory layout and calculate total movement of
data
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#define MEMORY_SIZE 100
struct AllocatedBlock {
int start;
int size;
};
struct AllocatedBlock allocated_blocks[MEMORY_SIZE];
int num_allocated_blocks = 0;
int total_movement = 0;
void allocateBlock(int size) {
int start = -1;
for (int i = 0; i < MEMORY_SIZE; i++) {
if (allocated_blocks[i].size == 0) {
start = i;
break;
}
}
if (start != -1) {
allocated_blocks[num_allocated_blocks].start = start;
allocated_blocks[num_allocated_blocks].size = size;
num_allocated_blocks++;
} else {
printf("No space available for allocation.\n");
}
}
void deallocateBlock(int index) {
int start = allocated_blocks[index].start;
int size = allocated_blocks[index].size;
for (int i = start; i < start + size; i++) {
allocated_blocks[i].size = 0;
}
}
void compactMemory() {
int new_start = 0;
total_movement = 0;
for (int i = 0; i < num_allocated_blocks; i++) {
if (allocated_blocks[i].start != new_start) {
int movement = allocated_blocks[i].start - new_start;
total_movement += abs(movement);
for (int j = 0; j < allocated_blocks[i].size; j++) {
allocated_blocks[new_start + j].size = allocated_blocks[i].size;
allocated_blocks[allocated_blocks[i].start + j].size = 0;
}
}
new_start += allocated_blocks[i].size;
}
}
void printMemoryLayout() {
printf("Memory Layout:\n");
for (int i = 0; i < MEMORY_SIZE; i++) {
if (allocated_blocks[i].size == 0) {
printf("[%d: Free]\n", i);
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
} else {
printf("[%d: Allocated (%d bytes)]\n", allocated_blocks[i].start, allocated_blocks[i].size);
}
}
}
int main() {
allocateBlock(10);
allocateBlock(5);
allocateBlock(7);
allocateBlock(3);
printMemoryLayout();
compactMemory();
return 0;
}
Output:
EXPERIMENT 9
AIM Implementation of resource allocation graph (RAG).
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
bool isSafeState(int processes[], int available[], int max[][MAX_RESOURCES], int allocation[][MAX_RESOURCES], int num_processes, int
num_resources) {
bool finish[MAX_PROCESSES] = { false };
int work[MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
for (int i = 0; i < num_resources; i++) {
work[i] = available[i];
}
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
int count = 0;
while (count < num_processes) {
bool found = false;
for (int i = 0; i < num_processes; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < num_resources; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == num_resources) {
for (int k = 0; k < num_resources; k++) {
work[k] += allocation[i][k];
}
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
break;
}
}
for (int i = 0; i < num_processes; i++) {
if (!finish[i]) {
return false;
}
}
return true;
}
int main() {
int num_processes = 5;
int num_resources = 3;
int available[MAX_RESOURCES] = { 3, 3, 2 };
Operating System Lab (KCS-451) Abhinav Airachia
2100910310005
JSS Academy of Technical Education-NOIDA
int max[MAX_PROCESSES][MAX_RESOURCES] = {
{ 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 }
};
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{ 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 }
};
bool safe = isSafeState(NULL, available, max, allocation, num_processes, num_resources);
if (safe) {
printf("System is in a safe state.\n");
} else {
printf("System is in an unsafe state.\n");
}
return 0;
}
Output
EXPERIMENT 10
AIM Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of method
used for storing graph
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_RESOURCES 10
typedef struct Node {
int process;
struct Node* next;
} Node;
void addEdge(Node* adj[], int src, int dest) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->process = dest;
newNode->next = adj[src];
adj[src] = newNode;
}
void printGraph(Node* adj[], int num_processes) {
for (int i = 0; i < num_processes; i++) {
Node* temp = adj[i];
printf("Process %d -> ", i);
while (temp != NULL) {
printf("%d ", temp->process);
temp = temp->next;
}
printf("\n");
}
}
void convertRAGtoWFG(int max[][MAX_RESOURCES], int allocation[][MAX_RESOURCES], int num_processes, int num_resources) {
Node* adj[num_processes];
for (int i = 0; i < num_processes; i++) {
adj[i] = NULL;
}
for (int i = 0; i < num_processes; i++) {
for (int j = 0; j < num_resources; j++) {
if (max[i][j] - allocation[i][j] > 0) {
for (int k = 0; k < num_processes; k++) {
if (allocation[k][j] > 0) {
addEdge(adj, i, k);
}
}
}
}
}
printGraph(adj, num_processes);
}
int main() {
int num_processes = 5;
int num_resources = 3;
int max[MAX_RESOURCES][MAX_RESOURCES] = {
{ 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 }
};
int allocation[MAX_RESOURCES][MAX_RESOURCES] = {
{ 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 }
};
return 0;
}
OUTPUT:
EXPERIMENT 11
AIM C program to implement the solution for Bounded Buffer problem using inter
process communication techniques-Semaphores
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{ int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{ printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{return (--s);
}
int signal(int s)
{return(++s);
}
void producer()
{mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
OUTPUT:
EXPERIMENT 12
AIM C program to implement the solution for Readers-Writers problem using inter
process communication techniques-Semaphores
SOURCE CODE:
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0;
void *reader(void* param)
{sem_wait(&x);
readercount++;
if(readercount==1)
sem_wait(&y);
sem_post(&x);
printf("%d reader is inside\n",readercount);
usleep(3);
sem_wait(&x);
readercount--;
if(readercount==0)
{sem_post(&y);
}
sem_post(&x);
printf("%d Reader is leaving\n",readercount+1);
return NULL;
}
void *writer(void* param)
{ printf("Writer is trying to enter\n");
sem_wait(&y);
printf("Writer has entered\n");
sem_post(&y);
printf("Writer is leaving\n");
return NULL;
}
int main()
{int n2,i;
printf("Enter the number of readers:");
scanf("%d",&n2);
printf("\n");
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}
OUTPUT: