0% found this document useful (0 votes)
10 views

Os Lab File New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Os Lab File New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

OPERATING SYSTEMS

(KCS-451)

LABORATORY FILE

B.TECH, 2nd Year, Semester - IV

COMPUTER SCIENCE
&
ENGINEERING

Faculty Name: - Student Name:-


Roll No:-
Section:-
INDEX (For B1 Batch)
S. No Program Name Date Marks Signature
1 Study of hardware and software requirements of
28/04/2023
different operating systems (UNIX, LINUX,
WINDOWS XP, WINDOWS7/8)
2 To create a new child process using fork system
05/05/2023
call.
3.a Implement CPU Scheduling Policies: FCFS
12/05/2023
3.b Implement CPU Scheduling Policies: SJF
02/06/2023
3.c Implement CPU Scheduling Policies: Priority
02/06/2023
Scheduling
WAP in C to Implement sequential file storage
09/06/2023
allocation technique:
4 i. Contiguous(using array)
ii. Linked –list(using linked-list)
iii. Indirect allocation (indexing)
WAP in C to Implement best fit memory
16/06/2023
5 management scheme of contiguous allocation
technique.
WAP in C to Implement Banker‟s algorithm for 23/06/2023
6
finding out the safe sequence.
WAP in C to Implement resource allocation graph
7 23/06/2023
(RAG).
Conversion of resource allocation graph (RAG) to
30/06/2023
8 wait for graph (WFG) for each type of method used
for storing graph.
Implement the solution for Bounded Buffer 07/07/2023
9 (producer-consumer)problem using inter process
communication techniques-Semaphores
Implement the solutions for Readers-Writers problem
07/07/2023
10 using inter process communication technique
–Semaphore
INDEX (For B2 Batch)
S. No Program Name Date Marks Signature
1 Study of hardware and software requirements of 12/04/2023
different operating systems (UNIX, LINUX, &19/04/2023
WINDOWS XP, WINDOWS7/8)
2 To create a new child process using fork system 26/04/2023
call.
3.a Implement CPU Scheduling Policies: FCFS 03/05/2023

3.b Implement CPU Scheduling Policies: SJF 10/05/2023

3.c Implement CPU Scheduling Policies: Priority 17/05/2023


Scheduling
WAP in C to Implement sequential file storage
07/06/2023
allocation technique:
4 i. Contiguous(using array)
ii. Linked –list(using linked-list)
iii. Indirect allocation (indexing)
WAP in C to Implement best fit memory
14/06/2023
5 management scheme of contiguous allocation
technique.
WAP in C to Implement Banker‟s algorithm for 21/06/2023
6
finding out the safe sequence.
WAP in C to Implement resource allocation graph
7 28/06/2023
(RAG).
Conversion of resource allocation graph (RAG) to
28/06/2023
8 wait for graph (WFG) for each type of method used
for storing graph.
Implement the solution for Bounded Buffer 05/07/2023
9 (producer-consumer)problem using inter process
communication techniques-Semaphores
Implement the solutions for Readers-Writers problem
05/07/2023
10 using inter process communication technique
–Semaphore
EXPERIMENT NUMBER: 1
Objective: - Study of hardware and software requirements of different operating systems (UNIX,
LINUX, WINDOWS XP, WINDOW S7/8)
Content:
Hardware requirements: The most common set of requirements defined by any operating system or
software application is the physical computer resources, also known as hardware, A hardware
requirements list is often accompanied by a hardware compatibility list (HCL), especially in case of
operating systems.
Hardware and Software Minimum Requirements
1. Windows 10
● Processor: 1 gigahertz (GHz) or faster processor or SoC
● RAM: 1 gigabyte (GB) for 32-bit or 2 GB for 64-bit
● Hard disk space: 16 GB for 32-bit OS or 20 GB for 64-bit OS
● Graphics card: DirctX 9 or later with WDDM 1.0 driver
● Display: 800 x 6009 with WDDM driver
2. WINDOWS XP
The minimum hardware requirements for Windows XP Home Edition are:
● Pentium 233-megahertz (MHz) processor or faster (300 MHz is recommended)
● At least 64 megabytes (MB) of RAM (128 MB is recommended)
● At least 1.5 gigabytes (GB) of available space on the hard disk
● CD-ROM or DVD-ROM drive
● Keyboard and a Microsoft Mouse or some other compatible pointing device
● Video adapter and monitor with Super VGA (800 x 600)or higher resolution
● Sound card
● Speakers or headphones
3. UNIX OS
● RAM: 1 GB
● Processor: IBM 604e processor with a clock speed of 375 MHz or faster
● Free disk space: Must have 1 GB free disk space. If Tivoli Identity Manager installs WebSphere
Application Server, WAS_HOME must have 800 MB free disk space and must have 300 MB
free disk space.
4. LINUX
● RAM: 2 GB
✔ If installing the main installation package:

✔ 7 GB for the installation package and extraction

✔ 4 GB for the /tmp directory or other temporary directory

● Disk space
If installing the domain host installation package or resource installation package:
● 1.2 GB for the installation package and extraction 400 MB for the /tmp directory or other
temporary directory, if specified by setting the IATEMPDIR environment variable. (This space
is cleaned up and available after the installation completes.)
● 1.5 GB for the installation directory
Experiment No.2
Objective: To create a new child process using fork system call.
Programs using the Following system calls of Unix / Linux operating system: fork, getpid.

⮚ The fork system call is used to create a new process called child process.

● The return value is 0 for a child process.


● The return value is negative if process creation is unsuccessful.
● For the parent process, return value is positive

⮚ The child process is an exact copy of the parent process.

⮚ Both the child and parent continue to execute the instructions following fork call

⮚ The child can start execution before the parent or vice-versa.

⮚ getpid()and getppid()

● The getpid system call returns process ID of the calling process


● The getppid system call returns parent process ID of the calling process
Algorithm
1. Declare a variable x to be shared by both child and parent.
2. Create a child process using fork system call.
3. If return value is -1 then
a. Print "Process creation unsuccessful
b. Terminate using exit system call.
4. If return value is 0 then
a. Print "Child process"
b. Print process id of the child using getpid system call Print value of x
c. Print process id of the parent using getppid system call
5. Otherwise
a. Print "Parent process"
b. Print process id of the parent using getpid system call
c. Print value of x Print process id of the shell using getppid system call.
6. Stop

Program /* Process creation - fork.c */


#include<stdio.h>
#includ<stdlib.h>
#includ<unistd.h>
#include<sys/types.h>
main()
{
pid_t pid;
int x = 5;
pid = fork(); x++;
if (pid < 0)
{
printf("Process creation error");exit(-1);
}
else if (pid == 0)
{
printf("Child process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:"); printf("\nProcess id is %d",
getpid()); printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
}
Output
$ gcc fork.c
$ ./a.out Child
process:
Process id is 19499 Value of x is 6
Process id of parent is 19498
Parent process: Process id is 19498 Value of x is 6
Process id of shell is 3266

Experiment No.3.a
Objective: Implement CPU Scheduling Policies: FCFS
#include<stdio.h>
#include<conio.h>
void main()
{
int n,b[10],w[10],i,j,h;
float avg=0;
clrscr();
printf("\n\tJOB SCHEDULING ALGORITHM[FCFS]");
printf("\n\t********************************\n");
printf("\nEnter how many jobs:");
scanf("%d",&n);
printf("\nEnter burst time for corresponding job....\n");
for(i=0;i<n;i++)
{
printf("\nProcess %d:",i+1);
scanf("%d",&b[i]);
}
w[0]=0;
printf("\nprocess 1 waiting time is 0");
for(i=1;i<n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",i+1,w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\n\t");
for(i=0;i<n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
}
getch();
}

INPUT AND OUTPUT:

Enter how many jobs:5

Enter burst time for corresponding job....

Process 1 : 4

Process 2 : 2

Process 3 : 8

Process 4 : 2

Process 5 : 9

process 1 waiting time is 0


Process 2 waiting time: 4
Process 3 waiting time: 6
Process 4 waiting time: 14
Process 5 waiting time: 16
total waiting time:40.000000

The average waiting time is: 8.000000


Experiment No.3.b
Objective: Implement CPU Scheduling Policies: SJF
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int n,b[10],w[10],i,j,h,t,tt;
float avg=0;
clrscr();
printf("\n\tJOB SCHEDULING ALGORITHM[SJF]");
printf("\n\t*******************************\n");
printf("\nEnter howmany jobs:");
scanf("%d",&n);
printf("\nEnter burst time for corresponding job....\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d:",i);
scanf("%d",&b[i]); a[i]=i;
}
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if(b[i]>b[j])
{
t=b[i]; tt=a[i];
b[i]=b[j]; a[i]=a[j];
b[j]=t; a[j]=tt;
}
w[1]=0;
printf("\nprocess %d waiting time is 0",a[1]);
for(i=2;i<=n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",a[i],w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\n\t");
for(i=1;i<=n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
}
getch();
}

INPUT AND OUTPUT:

Enter how many jobs:3

Enter burst time for corresponding job....

Process 1:5

Process 2:2

Process 3:3

process 2 waiting time is 0


Process 3 waiting time: 2
Process 1 waiting time: 5
total waiting time:7.000000
the average waiting time is:2.333333

Experiment No.3.c
Objective: Implement CPU Scheduling Policies: Priority Scheduling
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int n,b[10],w[10],i,j,h,t,tt;
float avg=0;
clrscr();
printf("\n\tPRIORITY SCHEDULING ALGORITHM");
printf("\n\t*****************************\n");
printf("\nEnter howmany jobs:");
scanf("%d",&n);
printf("\nEnter burst time & priority for corresponding job....\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d:",i);
scanf("%d %d",&b[i],&p[i]); a[i]=i;
}
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if(p[i]<p[j])
{
t=b[i]; tt=a[i];
b[i]=b[j]; a[i]=a[j];
b[j]=t; a[j]=tt;
}
w[1]=0;
printf("\nprocess %d waiting time is 0",a[1]);
for(i=2;i<=n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",a[i],w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\n\t");
for(i=1;i<=n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
}
getch();
}
INPUT:
Enter how many jobs:3
Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
PRIORITY SCHEDULING ALGORITHM
**************************************
Enter how many jobs:3
Enter burst time & priority for corresponding job....
Process 1: 5 2
Process 2: 7 1
Process 3: 6 3
OUTPUT
process3 waiting time is 0
Process1 waiting time: 6
Process2 waiting time: 11
total waiting time:17.000000
the average waiting time is: 5.666667

Experiment No.4.i
Objective: WAP in C to implement sequential file storage allocation technique: Contiguous
Memory Allocation using array.
#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)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\n File 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 :3
Enter file name 1 : A
Enter starting block of file 1 :85 Enter no of blocks in file 1 :6

Enter file name 2 : B


Enter starting block of file 2 :102 Enter no of blocks in file 2 :4

Enter file name 3 : C


Enter starting block of file 3 :60 Enter no of blocks in file 3 :4
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


Experiment No.4.ii
Objective: WAP in C to implement sequential file storage allocation technique: Linked
Allocation using linked list.
#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);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
}
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("\n File name No of blocks Blocks occupied");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d ",temp->bno);
temp = temp->next;
}
}
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


Experiment No.4.iii
Objective: WAP in C to implement sequential file storage allocation technique: Indexed File
Allocation
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob, blocks[30];
} 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("\n File 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
Experiment No.5
Objective: WAP in C to implement best fit memory management scheme of contiguous
allocation technique.

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
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)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2

Enter the size of the blocks:- Block 1: 5


Block 2: 2
Block 3: 7

Enter the size of the files:- File 1: 1


File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
Experiment No.6
Objective: WAP in C to Implement Banker’s algorithm for finding out the safe sequence.
#include<stdio.h>
#include<conio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;

else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n Request Not Granted -- Deadlock Occurred");
printf("\n System is in Unsafe State");
goto y;
}
}
printf("\n System is in safe state");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}
INPUT
Enter number of processes – 5
Enter number of resources – 3
Enter details for P0
Enter allocation -- 0 1 0
Enter Max -- 7 5 3
Enter details for P1
Enter allocation -- 2 0 0
Enter Max -- 3 2 2
Enter details for P2
Enter allocation -- 3 0 2
Enter Max -- 9 0 2
Enter details for P3
Enter allocation -- 2 1 1
Enter Max -- 2 2 2
Enter details for P4
Enter allocation -- 0 0 2
Enter Max -- 4 3 3

Enter Available Resources -- 3 3 2


Enter pid -- 1
Enter New Request Details -- 1 0 2

OUTPUT
P1 is visited ( 5 3 2)
P3 is visited ( 7 4 3)
P4 is visited ( 7 4 5)
P0 is visited ( 7 5 5)
P2 is visited ( 10 5 7)
System is in safe state
The Safe Sequence is -- (P1 P3 P4 P0 P2)

Process Allocation Max Need

P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Experiment No.7
Objective: WAP in C to implement resource allocation graph (RAG). Detects whether a
deadlock exists or not (Check cycles in the graph).
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++){
for(j=0; j<np+nr;j++){
printf("%d ", rag[i][j]);
}
printf("\n ");
}
return 0;
}

Input:
Process ID Resource Handling Resource Requesting
Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1
Output:
P0 P1 P2 R0 R1 R2
P0 0 0 0 0 0 1
P1 0 0 0 1 0 0
P2 0 0 0 0 1 0
R0 1 0 0 0 0 0
R1 0 1 0 0 0 0
R2 0 0 1 0 0 0
Experiment No.8
Objective: Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each
type of method used for storing graph.
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
printf("%d ", rag[i][j]);
}
printf("\n ");
}
int wfg[np][np];
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
wfg[i][j]=0;
}
}
int k;
for(i=0;i<np;i++)
{
for(j=np;j<np + nr; j++)
{
if(rag[i][j] == 1)
{
for(k=0;k<np;k++)
{
if(rag[j][k] == 1)
wfg[i][k] = 1;
}
}
}
}
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
printf("%d ", wfg[i][j]);
}
printf("\n ");
}
return 0;
}

Input:
Process ID Resource Holding Resource Requesting
Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1
Output
WFG P0 P1 P2
P0 0 0 1
P1 1 0 0
P2 0 1 0
Experiment No.9
Objective: Implement the solution for Bounded Buffer (producer-consumer) problem using
inter process communication techniques-Semaphores.
Description of the producer-consumer problem
Producer-consumer problem is a common paradigm for cooperating processes. A producer process
produces information that is consumed by a consumer process. One solution to the
producer-consumer problem uses shared memory. To allow producer and consumer processes to run
concurrently, there must be available a buffer of items that can be filled by the producer and
emptied by the consumer. This buffer will reside in a region of memory that is shared by the
producer and consumer processes. A producer can produce one item while the consumer is
consuming another item. The producer and consumer must be synchronized, so that the consumer
does not try to consume an item that has not yet been produced.
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
Break;
case 2:
if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3.
Exit Enter your choice: 3
Experiment No.10
Objective: Implement the solutions for Readers-Writers problem using inter process
communication technique –Semaphore

This program provides a possible solution for first readers writers problem using mutex and
semaphore. In this program I have used 10 readers and 5 producers to demonstrate the solution.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
void *writer(void *wno)
{
sem_wait(&wrt);
cnt = cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1)
{
sem_wait(&wrt);
// If this id the first reader, then it will block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0)
{
sem_post(&wrt);
// If this is the last reader, it will wake up the writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10};
//Just used for numbering the producer and consumer
for(int i = 0; i < 10; i++) {
pthread_create(&read[i], NULL, (void *)reader, (void *)&a[i]);
}
for(int i = 0; i < 5; i++)
{
pthread_create(&write[i], NULL, (void *)writer, (void *)&a[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_join(read[i], NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_join(write[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt)
return 0;
}
Output

You might also like