0% found this document useful (0 votes)
4 views61 pages

Os Final Record

The document provides a comprehensive overview of basic Unix commands, including general, directory, and file commands, along with their functions. It also includes examples of command outputs and describes a program implementation for process management using Unix system calls such as fork, exec, and wait. Additionally, it outlines an algorithm for implementing the First Come First Serve scheduling algorithm in C.

Uploaded by

rprithiviraajj
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)
4 views61 pages

Os Final Record

The document provides a comprehensive overview of basic Unix commands, including general, directory, and file commands, along with their functions. It also includes examples of command outputs and describes a program implementation for process management using Unix system calls such as fork, exec, and wait. Additionally, it outlines an algorithm for implementing the First Come First Serve scheduling algorithm in C.

Uploaded by

rprithiviraajj
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/ 61

Ex.

No:1
DATE : BASICS OF UNIX COMMANDS

AIM:

TostudyandexecuteUnixcommands.

GENERALCOMMANDS
Command Function
Date Used to display the current system date and time.
date+%D Displays date only
date+%T Displays time only
date+%Y Displays the year part of date
date+%H Displays the hour part of time
Cal Calendar of the current month
Calyear Displays calendar for all months of the specified year
calmonthyear Displays calendar for the specified month of the year
Who Login detailsof all users such as their IP,TerminalNo,
User name,
whoami Used to display the login details of the user
Tty Used to display the terminal name
Uname Displays the Operating System
uname–r Shows version number of the OS(kernel).
uname–n Displays domain name of the server
echo"txt" Displays the given text on the screen
echo$HOME Displays the user's home directory
Bc Basic calculator.Press Ctrl+d to quit
Lpfile Allows the user to spool a job along with others in a print queue.

mancmdname Manual for the given command .Press q to exit


History To display the commands used by the users in celogon.
Exit Exit from a process. If shell is the only process then goes out

DIRECTORYCOMMANDS
Command Function
Pwd Path of the present working directory
Mkdirdir A directory is created in the given name under the current
Directory
mkdirdir1dir2 A number of sub-directories can be created under one stroke
cdsubdir Change Directory. If the subdir starts with / then path starts from
root(absolute) otherwise from current working directory.

Cd To switch to the home directory.


cd/ To switch to the root directory.
cd.. To move back to the parent directory
Rmdirsubdir Removes an empty sub-directory.

FILECOMMANDS
Command Function
cat>filename To create a file with some contents. To end typing press
Ctrl+d.
The > symbol means redirecting out put to a file.(<forinput)
catfilename Displays the file contents.
cat>>filename Used to append contents to a file
cpsrcdes Copy files to given location. If already exists, it
will be Overwritten
cp–isrcdes Warns the user prior to over writing the destination file
cp–rsrcdes Copies the entire directory, all its sub-directories and files.
mvoldnew To rename an existing file or directory. –i option
can also be Used
mvf1f2f3dir To move a group offiles to a directory.
mv–voldnew Display name of each file as it is moved.
Rmfile Used to delete a file or group of files.– I option can also be used
rm* To delete all the files in the directory.
rm–r* Deletes all files and sub-directories
rm–f* To forcibly remove even write-protected files
Ls Lists all files and subdirectories(bluecolored) insorted
manner.
Lsname To check whether a file or directory exists.
lsname* Short-hand notation to list out filenames of a specific pattern.
ls–a Lists all files including hidden files(files beginning with.)
ls–xdirname To have specific list in go f a directory.
ls–R Recursive list in go fall files in thes ubdirectories
ls–l Longl isting showing file access rights(read/write/execute-rwx
foruser/group/others-ugo).
cmpfile1file2 Used to compare two files. Displays nothing if files
are identical.
Wcfile It produces a statisticsoflines(l),words(w),andcharacters(c).
chmodpermfile Changes permission for the specified file. (r=4, w=2,x=1)
chmod 740 file sets all rights for user, read only for groups
and no rights for others
OUTPUT

GENERAL COMMANDS

[student ~]date
Sat May 16 06:10:34 UTC 2020
[student~]date +%D
05/16/20
[student~]date +%T
10:13:11

[student~]date +%Y
2020
[student~]date +%H
10
[student~]cal
May 2020

Su Mo Tu We Th Fr
1 Sa
2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[student~]cal 2020
2020
January February March

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
Sa Su Mo Tu We Th Fr Sa
1 2 3 4
1 1 2 3 4 5 6 7
5 6 7 8 9 10 11 2 3 4 5 6 7
8 8 9 10 11 12 13 14
12 13 14 15 16 17 18 9 10 11 12 13 14
15 15 16 17 18 19 20 21
19 20 21 22 23 24 25 16 17 18 19 20 21
22 22 23 24 25 26 27 28
26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31

April May June

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1
2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8
9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15
16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22
23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29
30 28 29 30
31

July August September


Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
Sa Su Mo Tu We Th Fr Sa
1 2 3 4
1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7
8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14
15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21
22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28
29 27 28 29 30
30 31

October November
December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6
7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13
14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20
21 13 14 15 16 17 18 19

18 19 20 21 22 23 24 22 23 24 25 26 27
28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30
27 28 29 30 31

[student~]cal 2020
July 2020

Su Mo Tu We
1 Th
2 Fr
3 Sa
4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

[student~]who
studentpts/1 May 16 10:05 (172.16.1.14)
[student~]who am i
studentpts/1 May 16 10:05 (172.16.1.14)
[student~]tty
/dev/pts/1 [student~]uname Linux
[student~]echo "hello"
hello
[student~]echo $HOME
/home/student

DIRECTORY COMMANDS
[student~]$pwd
/home/student
[student~]mkdirsan [student~]mkdir s1 s2[student~]ls
s1 s2 san [student~]cd s1 [student s1]$ cd / [student /]$ cd . . [student /]$rmdir s1 [student ~]$ ls
s2 san

FILE COMMANDS
[student ~]$ cat>test
hi welcome operating systems lab
[student ~]$ cat test
hi welcome operating systems lab

[student ~]$ cat>>test fourth semester[student ~]$ cat test


hi welcome operating systems lab fourth semester
[student ~]$ cat>test1

[student ~]$ cp test test1[student ~]$ cat test1


hi welcome operating systems lab fourth semester [student ~]$ cp -i test test1 cp: overwrite
`test1'? y [student ~]$ cp -r test test1
[student ~]$ ls
s s2 san swap.sh temp.sh test TEST test1[student ~]$ mv san san1
[student ~]$ ls
s s2 san1 swap.sh temp.sh test TEST test1[student ~]$ mv test test1 san1
[student ~]$ mv -v san1 sannew
`san1' -> `sannew'
[student ~]$ ls
s s2 sannew swap.sh temp.sh TEST [student
~]$cmp test test1cmp: test: No such file or directory
RESULT
Thus the study and execution of Unix commands has been completed successfully.
EX.NO.2 IMPLEMENTATION OF FORK, EXEC, GETPID, EXIT, WAIT,AND
CLOSE SYSTEM CALLS.
DATE :

AIM:

To write a program for implementing process management using the following


system calls of UNIX operating system: fork, exec, getpid, exit, wait, close.

ALGORITHM:

1.Start the program.


2.Read the input from the command line.
3.Use fork() system call to create process, getppid() system call used to get theparent process ID
and getpid() system call used to get the current process ID
4.execvp() system call used to execute that command given on that command lineargument
5.execlp() system call used to execute specified command.
6.Open the directory at specified in command line input.
7.Display the directory contents.
8.Stop the program.
PROGRAM:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>

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


int pid;
char s[100];

pid = fork();

if (pid < 0) {
perror("Fork failed");
exit(1);
}
else if (pid > 0) {
// Parent process
wait(NULL);
printf("\nParent Process:\n");
printf("\n\tParent Process ID: %d\n", getpid());

if (execlp("cat", "cat", argv[1], (char *)0) == -1) {


perror("Can't execute cat");
exit(1);
}
}
else {
// Child process
printf("\nChild Process:");
printf("\n\tChild's Parent Process ID: %d", getppid());
sprintf(s, "\n\tChild Process ID: %d\n", getpid());
write(1, s, strlen(s));

if (execvp(argv[2], &argv[2]) == -1) {


perror("Can't execute command");
exit(1);
}
}

return 0;
}
OUTPUT:

[root@localhost~]# ./a.outtst date Child process:


Child process id :
3137 Sat Apr 10 02:45:32 IST 2010
Parent Process:
Parent Process id:3136 sddsaASD[root@localhost ~]# cat tst sddsaASD
RESULT:

Thus the program for process management was written and successfully executed.
EX.NO.3A: IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM
DATE:

AIM
To write a C program to implement First Come First Serve scheduling algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on order in which it requests CPU.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
Step 7: Stop the program.
PROGRAM :

#include<stdio.h>
Void main()
{
int bt[50],wt[80],at[80],wat[30],ft[80],tat[80];
int i,n;
float awt,att,sum=0,sum1=0;
charp[10][5];
printf("\nenter the number of process ");
scanf("%d",&n);
printf("\nEnter the process name and burst-time:");
for(i=0;i<n;i++)scanf("%s%d",p[i],&bt[i]);
printf("\nEnter the arrival- time:");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
wt[0]=0;
for(i=1;i<=n;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<=n;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n \n");
for(i=0;i<n;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d",wt[n]+bt[n]); printf("\n \n");
printf("\n"); for(i=0;i<n;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<n;i++)
tat[i]=wat[i]-at[i];
printf("\n FIRST COME FIRST SERVE\n");
printf("\n Process Burst-time Arrival-time Waiting-time Finish-time Turnaround-time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d
\t\t%d",i+1,p[i],bt[i],at[i],wat[i],ft[i],tat[i]);
for(i=0;i<n;i++)
sum=sum+wat[i];
awt=sum/n;
for(i=0;i<n;i++)
sum1=sum1+bt[i]+wt[i];
att=sum1/n;
printf("\n\nAverage waiting time:%f",awt);
printf("\n\nAverage turnaround time:%f",att);
}
}
OUTPUT:

enter the number of process 3


Enter the process name and burst-time:p1 2 p2 3
p3 4
Enter the arrival-time:0 1 2
GANTT CHART
| p1 | p2 | p3 |
0 2 5 9

FIRST COME FIRST SERVE


Process Burst-time Arrival-time Waiting-time Finish-time Turnaround-
time
p1 2 0 0 2 2
p2 3 1 1 5 4
p3 4 2 3 9 7
Average waiting time:1.333333
Average turnaround time:5.333333
RESULT:

The FCFS scheduling algorithm has been implemented in C.


EX.NO.3B : IMPLEMENTATION OF SJF SCHEDULING ALGORITHM
DATE:

AIM:

To write a C program to implement shortest job first (non-pre-emptive) scheduling algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on burst time.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
Step 7: Stop the program.
PROGRAM:

#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i< n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i< n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i< n; i++) {
A[i][2] = 0;
for (j = 0; j <i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i< n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
OUTPUT:
RESULT:

The SJF scheduling algorithm has been implemented in C.


EX.NO.3C IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM
DATE:

AIM:

To write a C program to implement Round Robin scheduling algorithm.

ALGORITHM:

1. Start the program.


2. Get the input process and their burst time.
3. Sort the processes based on priority.
4. Step 4: Compute the waiting time and turnaround time for each process.
5. Step 5: Calculate the average waiting time and average turnaround time.
6. Step 6: Print the details about all the processes.
7. Step 7: Stop the program.
PROGRAM:

//Write a C Program to implement Round Robin Schedulling algorithm


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

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
clrscr();
printf("\n\n Round Robin Schedulling algorithm");
printf("\n *************************************\n");
printf(" \n Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
OUTPUT:
RESULT:

The Round Robin scheduling algorithm has been implemented in C.


EX.NO.3D: IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM
DATE:

AIM:

To write a C program to implement Priority Scheduling algorithm.

ALGORITHM:

1. Start the program.


2. Get the input process and their burst time.
3. Sort the processes based on priority.
4. Compute the waiting time and turnaround time for each process.
5. Calculate the average waiting time and average turnaround time.
6. Print the details about all the processes.
7. Stop the program.
PROGRAM:

#include<stdio.h>
#include<string.h>
void main()
{
int bt[30],pr[30],np; intwt[30],tat[30],wat[30],at[30],ft[30];
int i,j,x,z,t;
float sum1=0,sum=0,awt,att;
charp[5][9],y[9];
printf("\nenter the number of process");
scanf("%d",&np);
printf("\nEnter the process,burst-time and priority:");
for(i=0;i<np;i++)
scanf("%s%d%d",p[i],&bt[i],&pr[i]);
printf("\nEnter the arrival-time:");
for(i=0;i<np;i++)
scanf("%d",&at[i]);
for(i=0;i<np;i++)
for(j=i+1;j<np;j++)
{
if(pr[i]>pr[j])
{
x=pr[j]; pr[j]=pr[i]; pr[i]=x; strcpy(y,p[j]);
strcpy(p[j],p[i]);
strcpy(p[i],y);
z=bt[j]; b t[j]=bt[i]; bt[i]=z;
}
} wt[0]=0;
for(i=1;i<=np;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<np;i++)
ft[i]=ft[i-1]+bt[i]; printf("\n\n\t\tGANTT CHART\n");
printf("\\n");
for(i=0;i<np;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n"); for(i=0;i<=np;i++)
printf("%d\t\t",wt[i]);
printf("\n \n");
printf("\n"); for(i=0;i<np;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<np;i++)
tat[i]=wat[i]-at[i]; printf("\nPRIORITY SCHEDULING:\n");
printf("\nProcess Priority Burst-time Arrival-time Waiting-time Turnaround-time");
for(i=0;i<np;i++)
printf("\n\n%d%s\t%d\t\t%d\t\t%d\t%d\t\t%d",i+1,p[i],pr[i],bt[i],a t[i],wt[i],tat[i]);
for(i=0;i<np;i++)
sum=sum+wat[i]; awt=sum/np; for(i=0;i<np;i++)
sum1=sum1+tat[i]; att=sum1/np;
printf("\n\nAverage waiting time:%f",awt); printf("\n\nAverageturnaroundtime is:%f",att);
}
OUTPUT:

Enter the number of process3


Enter the process, burst-time and priority:p1 3 3 p2 4 2
p3 5 1
Enter the arrival-time: 0 1 2

GANTT CHART

| p3 | p2 | p1 |

0
5
9
12

PRIORITY SCHEDULING:
Process Priority Burst-time Arrival-time Waiting-time Turnaround-time
p3 1 5 0 0 0
p2 2 4 1 5 3
p1 3 3 2 9 5
Average waiting time: 3.666667 Average turnaround time is: 2.666667
RESULT:

The Priority scheduling algorithm has been implemented in C Successfully.


EX NO:4 DINING PHILOSOPHER PROBLEM USING SEMAPHORE
DATE :

AIM:

Write a C program to simulate the concept of Dining-Philosophers problem.

ALGORITHM:

Step 1: Start
Step 2: Define the number of philosophers
Step 3: Declare one thread per philosopher
Step 4: Declare one semaphore (represent chopsticks) per philosopher
Step 5: When a philosopher is hungry See if chopsticks on both sides are free Acquire both
chopsticks
Eat Restore the chopstick If chopsticks aren‟t free
Step 6: Wait till they are available
Step 7: Stop
PROGRAM

//Write the C Program to implement dining philosophy problem


#include<stdio.h>
#include<conio.h>
int tph, philname[20], status[20], howhung, hu[20], cho;
void main()
{
int i;
clrscr();
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1); status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("\n 1.One can eat at a time\n 2.Two can eat at atime\n 3.Exit \nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
exit(0);
printf("\n No deadlock occur in this stage");
break;
default:
printf("\nInvalid option..");
break;
}
}while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));t=hu[i]; r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat",philname[hu[i]],philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
getch();
}
OUTPUT:

DINING PHILOSOPHER PROBLEM


Enter the total no.of philosophers: 5
How many are hungry : 3
Enter philosopher1 position:2
Enter philosopher2 position:4
Enter philosopher3 position:5
One can eat at a time 2.Two can eat at a time 3.Exit Enter your choice: 1
Allow one philosopher to eat at any time P3 is granted to eat P3 is waiting P5 is waiting P 0 is
waiting
P 5 is granted to eat P 5 is waiting P 0 is waiting
P0 is granted to eat P0 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice:
2
Allow two philosophers to eat at same time
combination1
P3 and P5 are granted to eat P0 is w a i t i n g
co mb in at io n 2
P3 and P0 are granted to eat P5 is w a i t i n g
co mb i n at io n3
P5 and P0 are granted to eat P3 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 3
RESULT:

Thus the program to implement the dining Philosopher was executed and verified.
EX.NO: 5A DEADLOCK AVOIDANCE
DATE:

AIM:

To Simulate Algorithm for Deadlock avoidance

ALGORITHM:

1: Start the Program


2: Get the values of resources and processes.
3: Get the avail value.
4: After allocation find the need value.
5: Check whether its possible to allocate. If possible it is safe state
6: If the new request comes then check that the system is in safety or not if weallow the request.
7: Stop the execution
PROGRAM:

#include <stdio.h>
void main() {
int pno, rno, i, j, prc, count, total;
count = 0;
printf("\n Enter number of processes: ");
scanf("%d", &pno);
printf("\n Enter number of resources: ");
scanf("%d", &rno);
int flag[pno + 1], tres[rno + 1], max[pno + 1][rno + 1], allocated[pno + 1][rno + 1], need[pno +
1][rno + 1], avail[rno + 1], work[rno + 1];
for (i = 1; i <= pno; i++) {
flag[i] = 0;
}

printf("\n Enter total numbers of each resource: ");


for (i = 1; i <= rno; i++) {
scanf("%d", &tres[i]);
}

printf("\n Enter Max resources for each process:");


for (i = 1; i <= pno; i++) {
printf("\n For process %d: ", i);
for (j = 1; j <= rno; j++) {
scanf("%d", &max[i][j]);
}
}
printf("\n Enter Allocated resources for each process:");
for (i = 1; i <= pno; i++) {
printf("\n For process %d: ", i);
for (j = 1; j <= rno; j++) {
scanf("%d", &allocated[i][j]);
}
}

printf("\n Available resources:\n");


for (j = 1; j <= rno; j++) {
total = 0;
for (i = 1; i <= pno; i++) {
total += allocated[i][j];
}
avail[j] = tres[j] - total;
work[j] = avail[j];
printf(" %d\t", work[j]);
}

do {
for (i = 1; i <= pno; i++) {
for (j = 1; j <= rno; j++) {
need[i][j] = max[i][j] - allocated[i][j];
}
}

printf("\n Allocated Matrix | Max | Need\n");


for (i = 1; i <= pno; i++) {
printf("\n");
for (j = 1; j <= rno; j++) {
printf("%4d", allocated[i][j]);
}
printf(" | ");
for (j = 1; j <= rno; j++) {
printf("%4d", max[i][j]);
}
printf(" | ");
for (j = 1; j <= rno; j++) {
printf("%4d", need[i][j]);
}
}

prc = 0;
for (i = 1; i <= pno; i++) {
if (flag[i] == 0) {
prc = i;
for (j = 1; j <= rno; j++) {
if (work[j] < need[i][j]) {
prc = 0;
break;
}
}
}
if (prc != 0) break;
}
if (prc != 0) {
printf("\n Process %d completed", prc);
count++;
printf("\n Available matrix:");
for (j = 1; j <= rno; j++) {
work[j] += allocated[prc][j];
allocated[prc][j] = 0;
max[prc][j] = 0;
flag[prc] = 1;
printf(" %d", work[j]);
}
}
} while (count != pno && prc != 0);

if (count == pno)
printf("\nThe system is in a safe state!!\n");
else
printf("\nThe system is in an unsafe state!!\n");
}
OUTPUT:

Enter number of process:5 Enter number of resources:3


Enter total numbers of each resources:10 5 7Enter Max resources for each process:
for process 1: 7 5 3
for process 2: 3 2 2
for process 3: 9 0 2
for process 4: 2 2 2
for process 5: 4 3 3
Enter allocated resources for each process:for process 1: 0 1 0
for process 2: 3 0 2
for process 3: 3 0 2
for process 4: 2 1 1
for process 5: 0 0 2 available resources:
2 3 0

Allocated matrix Max need

0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2 Allocated matrix
Maxneed
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3 Allocated matrix
Maxneed

0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0

3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3 Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0

0 0 0| 0 0 0| 0 00
0 0 2| 4 3 3| 4 31
Process 3 completed
Available matrix: 10 5 5 Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 57
The system is in a safe state!!
RESULT:
Thus the program to implement the deadlock avoidance was executed and verified.
EX.NO:5B DEADLOCK DETECTION ALGORITHM
DATE:

AIM:
To Simulate Algorithm for Deadlock detection.

ALGORITHM:

Step 1: Start the Program.


Step 2: Get the values of resources and processes.
Step 3: Get the available value.
Step 4: After allocation, find the need value.
Step 5: Check whether it is possible to allocate.
Step 6: If it is possible, then the system is in a safe state.
Step 7: Stop the execution.
PROGRAM:

#include <stdio.h>
#include <conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the number of Processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
scanf("%d", &r);
printf("Enter the Max Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources:\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}

void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\n");
for (i = 0; i < n; i++) {
printf("P%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++) printf("%d ", avail[j]);
}
printf("\n");
}
}

void cal() {
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int dead[100], safe[100];
int i, j;

for (i = 0; i < n; i++) {


finish[i] = 0;
}

// Find need matrix


for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][k];
}
finish[i] = 1;
flag = 1;
}
}
}
}
}

j = 0;
flag = 0;
for (i = 0; i < n; i++) {
if (finish[i] == 0) {
dead[j] = i;
j++;
flag = 1;
}
}

if (flag == 1) {
printf("\n\nSystem is in Deadlock and the Deadlock processes are: \n");
for (i = 0; i < j; i++) {
printf("P%d\t", dead[i]);
}
} else {
printf("\n\nThe system is not in a deadlock state.\n");
}
getch();
}
OUTPUT:

Enter the number of processes: 3


Enter the number of resource instances: 3
Enter the Max Matrix:
368
433
344
Enter the Allocation Matrix:
333
203
124
Enter the available resources:
120

Process Allocation Max Available


P1 333 368 120
P2 203 433
P3 124 344

System is in deadlock and deadlock processes are P1 P2 P3


RESULT:
Thus, the program to implement deadlock detection was executed successfully.
EX.NO:6 IMPLEMENTATION OF PAGING TECHNIQUE OF MEMORY
MANAGEMENT
DATE:

AIM:
To write a C program to implement paging concept for memory management.

ALGORITHM:

Step 1: Start the program.


Step 2: Enter the logical memory address.
Step 3: Enter the page table which has offset and page frame.
Step 4: The corresponding physical address can be calculated by,
PA = [ pageframe * No. of pagesize ] + Page offset.
Step 5: Print the physical address for the corresponding logical address.
Step 6: Terminate the program.
PROGRAM:

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

int main() {
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];

clrscr();
printf("\nEnter the memory size -- ");
scanf("%d", &ms);
printf("\nEnter the page size -- ");
scanf("%d", &ps);

nop = ms / ps;
printf("\nThe number of pages available in memory are -- %d ", nop);
printf("\nEnter number of processes -- ");
scanf("%d", &np);

rempages = nop;
for (i = 1; i <= np; i++) {
printf("\nEnter the number of pages required for p[%d]-- ", i);
scanf("%d", &s[i]);

if (s[i] > rempages) {


printf("\nMemory is Full");
break;
}

rempages -= s[i];
printf("\nEnter page table for p[%d] --- ", i);
for (j = 0; j < s[i]; j++) {
scanf("%d", &fno[i][j]);
}
}

printf("\nEnter Logical Address to find Physical Address");


printf("\nEnter process no., page number, and offset -- ");
scanf("%d %d %d", &x, &y, &offset);
if (x > np || y >= s[x] || offset >= ps) {
printf("\nInvalid Process or Page Number or Offset");
} else {
pa = fno[x][y] * ps + offset;
printf("\nThe Physical Address is -- %d", pa);
}

getch();
return 0;
}
OUTPUT:

Enter the memory size -- 1000


Enter the page size -- 100
The number of pages available in memory are -- 10
Enter number of processes -- 3
Enter the number of pages required for p[1]-- 4
Enter page table for p[1] --- 8 6
Enter the number of pages required for p[2]-- 5
Enter page table for p[2] --- 1 5 4 10 9
Enter the number of pages required for p[3]-- 5
Memory is Full

Enter Logical Address to find Physical Address


Enter process no., page number, and offset -- 2 3 60
The Physical Address is 760
RESULT:
Thus, the C program for implementing the paging concept for memory management has
been executed successfully

You might also like