Cse CS3461 OS LAB MANUAL Fina Version
Cse CS3461 OS LAB MANUAL Fina Version
NAME :
DEGREE / BRANCH :
YEAR : II YEAR
SEMESTER : IV SEMESTER
REGULATION : R 2021
ARJUN COLLEGE OF TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai)
Coimbatore to Pollachi Highway, Thamaraikulam post, Coimbatore - 642120
BONAFIDE CERTIFICATE
Year : ………………………………………………………………
Semester : ……………………………………………………………....
Branch : ……………………………………………………...……….
Aim:
Procedure:
Installing Operating system steps
1. Turn on your computer, insert the Windows 7 installation CD-ROM or DVD-ROM drive, and then restart
your computer.
2. The computer will boot from the CD-ROM automatically as the BIOS set up, or the massage of “press any
key to boot from CD or DVD” will appear, so Press any key when prompted to do , and then follow any
instructions that appear.
5. When the Install Windows page appears, click Install now to begin the installation process.
6. Windows 7 is working in the background. This may take several minutes depending on the system
7. Here we see the License Agreement you must accept before continuing. After reading the agreement, check
the box next to “I accept the license terms” and select Next button.
8. Select which kind of installation does you want, for here we will select custom advanced.
9. The next screen will ask you where you want to install the system, meaning on what partition.
At that point, you need to decide one of the two options:
1- Install Windows on the entire available disk space.
2- Create a partition on the hard disk, and install Windows on that partition.
If you pick option 1, then you simply click “Next” and get done with it. The setup program will create a
partition on the entire hard disk and format it with the NTFS file system. It will then install Windows on that
partition.
However we will hose option 2, because we want to create the partition exactly as we want it . Click on
“Drive options advanced”. The screen will change and show you these buttons:
New – to create a new partition.
Delete – to delete a specific partition – all data on that partition will be deleted.
Format – to format a specific partition – all data on that partition will be deleted.
Extend – to extend a partition of the limitation of physical disk, and to span the partition on more than one
physical disk.
To create a new partition click “New”. In the “Size” box, enter the size for the new partition. When done,
click “Apply”.
Note: the size of the partition should be in MB. For example if you want a partition of 24 GB you will
multiply it by 1024 then write that number in the size box.
24.414×1024= 25,000
▪ The setup program will let you know that it will create an additional partition on that drive. That
partition will be, by default, hidden, and contain the essential files to boot the computer. This partition
will consume 100 MB of disk space.
Note: Even if you do not manually create the partition by using this procedure Windows will still create this
partition. However, since you did manually create the partition, Windows will tell you that another partition
will also be created.
You can now see the partition that you have created, and the hidden boot partition. In most cases, the partition
on which Windows will be installed is partition number 2, but you can also choose where do you want to
install OS by selecting that partition. Press “Format” to format the new partition.
Warning window will appear telling that all data will be deleted. That should make no issue because the
partition was just created, and it is blank, Installation will now complete as usual.
10. Here is where Windows 7 installs major components of the OS. This process can take 10 to 60 minutes
depending on the system.
11. Here we see the system needing to reboot to continue installation tasks
12. Here we see progress as Windows 7 updates the registry settings. This process may take several minutes
before going to the next screen.
Note if you got the message of “press any key to boot from CD or DVD” do not press as it will boot from hard
disk to continue installation
13. Windows 7 starts up required services at this point in the setup process.
14. Windows 7 continues to install at this screen. After initial setup is complete, another reboot is required
with no user input. Then setup will prepare the system for initial booting
15. Here is where creating the first account happens. This user will have full administrator and automatically
be logged in after setup is completed. Enter in your user name and computer name as you would like it to be
identified on the network and then select the Next button.
16. This screen prompts you to enter a password for the account that you just created. I highly recommend that
you password your account. Enter it twice, then include a “password hint” to help remind you of what it is if
you misplace or forget it. You, of course, should not enter in your actual password in this field, but a subtle
reminder to jog your memory. After this information is entered, select the Next button.
17. This is where Automatic Updates and problems and reports solutions are offered as an option. I
recommend to most users to “Use Recommended Settings” here. These options can always be changed later.
Make your selection to continue to the next screen.
18. Ensure that your time zone is entered correctly and make adjustments as necessary. Also ensure the proper
time is set. This information is extracted from the BIOS settings, so if the time was set correctly in a previous
installation of Windows (or most any other OS), it should still be correct here. After this information is
verified, select the Next button.
19. Most people will want to use “Home network” for desktop installations or “Public network” for a portable
system, like a laptop. Make your selection to continue to the next screen
20. Finalizing settings, this should only take a few moments to complete. Then welcome windows will
appears, after that preparing for your desk top.
(Windows 7 Install Guide, n.d)
If you want more and further explanation with applying steps, just watch this video which could help you a
lot. You can see how the OS starts with the boot device and then partitioning then entering infromation about
user account, and complete the steps as mentioned above. As well as you can follow the instructions with this
tutorial and apply at the same time.
Result:
Thus the Installation of windows operating system is completed successfully.
EX. NO: 2A ILLUSTRATE UNIX COMMANDS
Aim:
To study and execute the basic UNIX commands.
Description:
Result:
Thus the basic UNIX commands were studied.
EX. NO: 2B ILLUSTRATE SHELL PROGRAMMING
Aim:
To implement the shell programming.
Description:
The shell provides an interface to the UNIX system. It gathers input from user and
executes programs based on that input. A shell is an environment in which we can run our
commands, programs, and shell scripts. There are different flavours of shells, just as there are
different flavours of operating systems. Shell Types are The Bourne shell. If you are using a
Bourne-type shell, the default prompt is the $ character. The C shell. If you are using a C-type
shell, the default prompt is the % character.
Program:
Factorial of N Number:
7 is prime number
Result:
Thus the basic shell programming was implemented.
PROCESS MANAGEMENT USING SYSTEM CALLS : FORK, WAIT
EX. NO: 3A
AND EXIT
Aim:
To write a program in C to implement the system calls fork, wait and exit.
Algorithm:
1. Start the program.
2. Declare the variables to store the status value and fork return value.
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int status;
pid_t fork_return;
fork_return = fork();
if (fork_return == 0)
{
printf("\n I'm the child!!");
exit(0);
}
else
{
wait(&status);
printf("\n I'm the parent!!");
printf("\n Child returned:%d\n", status);
}
return 0;
}
Output:
Result:
Thus the implementation of system calls fork exit and wait has been executed
successfully.
EX. NO: 3B PROCESS MANAGEMENT USING SYSTEM CALLS : GETPID
Aim:
To write a program in C to implement the system calls getpid.
Algorithm:
1. Start the program.
2. Invoke the built in function getpid() which will return current process identification
number.
3. Display the process ID.
4. Stop the program.
Program:
#include<stdio.h>
#include<unistd.h>
int main()
{
fork();
printf("\n Hello,I am the process with ID=%d\n",getpid());
return 0;
}
Output:
[exam127@redhat exam127]$ cc anugetpaid.c
[exam127@redhat exam127]$ ./a.out
Result:
Thus the implementation of system calls Getpid has been executed successfully.
EX. NO: 3C PROCESS MANAGEMENT USING SYSTEM CALLS : CLOSE
Aim:
To write a program in C to implement the system call opendir (), readdir () and
closedir().
Algorithm:
1. Start the program.
2. The function opendir() is invoked to open the directory which contains name of the
directory a parameter.
3. The content of the directory can be read using the function readdir().
4. It contains the name of the directory as a parameter.
5. Display the content of the directory.
6. The directory is closed using the function closedir().
7. Stop the program.
Program:
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdio.h>
void traverse(char *fn)
{
DIR *dir;
struct dirent *entry;
char path[1025];
struct stat info;
printf("%s\n",fn);
if((dir=opendir(fn))==NULL)
printf("error");
else
{
while((entry=readdir(dir))!=NULL)
{
if((entry->d_name[0])!='.')
{
strcpy(path,fn);
strcat(path,"/");
strcat(path,(entry->d_name));
if(stat(path,&info)!=0)
printf("error");
else if(S_ISDIR(info.st_mode))
traverse(path);
}
}
closedir(dir);
}
}
main()
{
Result:
Thus the implementation of system calls Opendir(), Readdir() and Closedir() has been
executed successfully.
IMPLEMENTATION OF CPU SCHEDULING ALGORITHM –
EX. NO: 4A
ROUND ROBIN
Aim:
To simulate the CPU scheduling algorithm round-robin.
Algorithm:
1. Start the process
2. Accept the number of processes in the ready Queue and time quantum (or) time slice
3. For each process in the ready Q, assign the process id and accept the CPU burst time
4. Calculate the no. of time slices for each process where No. of time slice for process
(n) = burst time process (n)/time slice
5. If the burst time is less than the time slice then the no. of time slices =1.
6. Consider the ready queue is a circular Q, calculate
a) Waiting time for process (n) = waiting time of process(n-1)+ burst time of
process(n-1 ) + the time difference in getting the CPU from process(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of
process(n) + the time difference in getting CPU from process(n).
7. Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
8. Stop the process
Program:
#include <stdio.h>
main()
{
int s[10], p[10], n, i, j, w1 = 0, w[10], t[10], st[10], tq, tst = 0;
int tt = 0, tw = 0;
float aw, at;
printf("Enter no.of processes \n");
scanf("%d", &n);
printf("\n Enter the time quantum \n");
scanf("%d", &tq);
printf("\n Enter the process &service time of each process separated by a space \n");
for (i = 0; i < n; i++)
scanf("%d%d", &p[i], &s[i]);
t[i] = w1;
w[i] = t[i] - st[i];
s[i] = s[i] - tq;
}
}
}
Output:
Enter Number of process: 3
Enter the Time Quantum: 3
Enter the process and service time of each process separated by a space
1 3 2 6 3 9
Process st wt tt
1 3 0 3
2 6 6 12
3 9 9 18
Awt=5.000000
Att=11.000000
Result:
Thus the Round Robin CPU scheduling algorithm was implemented and average
waiting time, average turnaround time was computed.
IMPLEMENTATION OF CPU SCHEDULING ALGORITHM –
EX. NO: 4B
SHORTEST JOB FIRST
Aim:
To write a C program to simulate the shortest job first CPU scheduling algorithm.
Algorithm:
1. Start the program.
2. Accept the number of processes in the ready Queue
3. For each process in the ready Q, assign the process id and accept the CPU burst time
and process arrival time.
4. Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
5. Set the waiting time of the first process as ‗0‘and its turnaround time as its burst time.
6. Sort the processes names based on their Burt time and Arrival time
7. For each process in the ready queue, calculate
a). Waiting times(n) = waiting time (n-1) + Burst time (n-1)-arrival time(n)
b). Turnaround time (n) = waiting time (n) +Burst time (n)
8. Step 8: Calculate and print the results
9. (a) Average waiting time = Total waiting Time / Number of process
10. (b) Average Turnaround time = Total Turnaround Time / Number of process.
11. Stop the program.
Program:
#include <stdio.h>
main()
{
int s[10], p[10], n, i, j, w1 = 0, w[10], t[10], st[10], tq, tst = 0;
int tt = 0, tw = 0;
float aw, at;
clrscr();
printf("Enter no.of processes \n");
scanf("%d", &n);
printf("\n Enter the time quantum \n");
scanf("%d", &tq);
printf("\n Enter the process &service time of each process separated by a space \n");
for (i = 0; i < n; i++)
scanf("%d%d", &p[i], &s[i]);
for (i = 0; i < n; i++)
{
st[i] = s[i];
tst = tst + s[i];
}
w1 = w1 + tq;
t[i] = w1;
w[i] = t[i] - st[i];
s[i] = s[i] - tq;
}
}
aw = tw / n;
at = tt / n;
printf("process\tst\twt\ttt \n");
for (i = 0; i < n; i++)
printf("%d\t%d\t%d\t%d \n", p[i], st[i], w[i], t[i]);
printf("awt=%f\n", aw);
printf("att=%f\n", at);
getch();
}
Output:
1 3 0 3
0 4 3 7
2 5 7 12
Result:
Thus the Shortest Job First (SJF) CPU scheduling algorithm was implemented and
average waiting time, average turnaround time was computed.
IMPLEMENTATION OF CPU SCHEDULING ALGORITHM – FIRST COME
EX. NO: 4C
FIRST SERVE
Aim:
To write a c program to simulate the CPU scheduling algorithm First Come First Serve (FCFS)
Algorithm:
1. Start the process
3. For each process in the ready Q, assign the process name and the burst time
4. Set the waiting of the first process as ‗0‘and its burst time as its turnaround time
6. Calculate
Program:
#include <stdio.h>
void main()
{
int i, n, sum, wt, tat, twt, ttat;
int t[10];
float awt, atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d", &n);
Result:
Thus the FCFS CPU scheduling algorithm was implemented and average waiting time,
average turnaround time was computed.
IMPLEMENTATION OF CPU SCHEDULING ALGORITHM – PRIORITY
EX. NO: 4D
SCHEDULING
Aim:
Algorithm:
1. Start the process
3. For each process in the ready Q, assign the process id and accept the CPU burst time
5. Set the waiting of the first process as ‗0‘ and its burst time as its turnaround time
9. Calculate
wt[0] = 0;
for (i = 0; i < n; i++)
{
wt[i + 1] = bt[i] + wt[i];
tt[i] = bt[i] + wt[i];
w1 = w1 + wt[i];
t1 = t1 + tt[i];
}
aw = w1 / n;
at = t1 / n;
printf("\nbt\tprority\twt\ttt\n");
for (i = 0; i < n; i++)
printf("%d\t%d\t%d\t%d\n", bt[i], pt[i], wt[i], tt[i]);
printf("aw=%f\nat=%f", aw, at);
getch();
}
Output:
Enter no of jobs: 3
Enter burst time: 10 11 12
Enter priority values: 3 2 1
Bt priority wt tt
10 3 0 10
11 2 10 21
12 1 21 33
Aw=10.000000
At=21.000000
Result:
Thus the Priority CPU scheduling algorithm was implemented and average waiting
time, average turnaround time was computed.
IMPLEMENTATION OF THE INTER PROCESS COMMUNICATION
EX. NO: 5
STRATEGY
Aim:
To develop a client-server application program, this uses shared memory using Inter
Process Communication (IPC).
Algorithm:
Client:
1. Define the key to be 5600
Server:
1. Define shared memory size of 30 bytes
3. Create a shared memory using shmget () system calls and gets the shared memory id in
variable shmid.
5. Get the content to be placed in the shared memory from the user of the server.
6. Write the content in the shared memory, which will read out by the client.
7. Stop
Program:
Server:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
perror(s);
exit(1);
int main()
char c;
int shmid;
key_t key;
key = 5678;
die("shmget");
die("shmat");
s = shm;
*s++ = c;
sleep(1);
exit
}
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
*shm = '*';
exit(0);
}
Output:
[gokul@localhost ~]$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0000162e 98307 gokul 666 27 1
abcdefghijklmnopqrstuvwxyz
Result:
Thus the above program executed and verified successfully.
IMPLEMENTATION OF MUTUAL EXCLUSION BY SEMAPHORE –
EX. NO: 6
DINING PHILOSOPHER
Aim:
To implement dining philosopher problem using semaphores.
Algorithm:
1. There are N philosophers meeting around a table, eating spaghetti and talking about
philosophy.
2. There are only N forks available such that only one fork between each philosopher.
3. There are only 5 philosophers and each one requires 2 forks to eat.
4. A solution to the problem is to ensure that at most number of philosophers can eat
Spaghetti at once.
Program:
#include <stdio.h
#define n 4
int compltedPhilo = 0, i;
struct fork
{
int taken;
}
ForkAvil[n];
struct philosp
{
int left;
int right;
}
Philostatus[n];
void goForDinner(int philID)
{
int otherFork = philID - 1; //same like threads concept here cases implemented
if (Philostatus[philID].left == 10 && Philostatus[philID].right == 10)
printf("Philosopher %d completed his dinner\n", philID + 1);
//if already completed dinner
else if (Philostatus[philID].left == 1 && Philostatus[philID].right == 1)
{
//if just taken two forks
printf("Philosopher %d completed his dinner\n", philID + 1);
Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that
he completed dinner by assigning value 10
if (otherFork == -1)
otherFork = (n - 1);
ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
printf("Philosopher %d released fork %d and fork %d\n", philID + 1, philID + 1,
otherFork + 1);
compltedPhilo++;
}
else if (Philostatus[philID].left == 1 && Philostatus[philID].right == 0)
{
//left already taken, trying for
right fork
if (philID == (n - 1))
{
if (ForkAvil[philID].taken == 0)
{
//KEY POINT OF THIS PROBLEM, THAT LAST
PHILOSOPHER TRYING IN reverse DIRECTION
ForkAvil[philID].taken = Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d\n", philID + 1, philID + 1);
}
else
{
else
{
26
int dupphilID = philID;
philID -= 1;
if (philID == -1)
philID = (n - 1);
if (ForkAvil[philID].taken == 0)
{
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n", philID + 1, dupphilID + 1);
}
else
{
printf("Philosopher %d is waiting for Fork %d\n", dupphilID + 1, philID+ 1);
}
}
}
else if (Philostatus[philID].left == 0)
{
//nothing taken yet
if (philID == (n - 1))
{
if (ForkAvil[philID - 1].taken == 0)
{
PHILOSOPHER TRYING IN reverse DIRECTION
ForkAvil[philID - 1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n", philID, philID + 1);
}
else
{
int main()
{
for (i = 0; i < n; i++)
ForkAvil[i].taken = Philostatus[i].left = Philostatus[i].right = 0;
while (compltedPhilo < n)
{
for (i = 0; i < n; i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\n",
compltedPhilo);
}
return 0;
}
Output:
Result:
Thus the program for demonstrating Dining-Philosopher problem was implemented.
EX. NO: 7 BANKER’S ALGORITHM FOR DEALOCK AVOIDANCE
Aim:
To simulate bankers algorithm for Dead Lock 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 it’s possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety or not if we allow the
request.
9. Stop the program.
Program:
#include <stdio.h>
main()
{
int r[1][10], av[1][10];
int all[10][10], max[10][10], ne[10][10], w[10], safe[10];
int i = 0, j = 0, k = 0, l = 0, np = 0, nr = 0, count = 0, cnt = 0;
printf("enter the number of processes in a system");
scanf("%d", &np);
printf("enter the number of resources in a system");
scanf("%d", &nr);
for (i = 1; i <= nr; i++)
{
printf("\n enter the number of instances of resource R%d ", i);
scanf("%d", &r[0][i]);
av[0][i] = r[0][i];
}
for (i = 1; i <= np; i++)
for (j = 1; j <= nr; j++)
all[i][j] = ne[i][j] = max[i][j] = w[i] = 0;
printf(" \nEnter the allocation matrix");
for (i = 1; i <= np; i++)
{
printf("\n");
for (j = 1; j <= nr; j++)
{
scanf("%d", &all[i][j]);
av[0][j] = av[0][j] - all[i][j];
}
}
printf("\nEnter the maximum matrix");
for (i = 1; i <= np; i++)
{
printf("\n");
for (j = 1; j <= nr; j++)
{
scanf("%d", &max[i][j]);
}
}
for (i = 1; i <= np; i++)
{
for (j = 1; j <= nr; j++)
31
{
printf("\n ");
printf("\n safe sequence");
for (count = 1; count <= np; count++)
{
for (i = 1; i <= np; i++)
{
cnt = 0;
for (j = 1; j <= nr; j++)
{
if (ne[i][j] <= av[0][j] && w[i] == 0)
cnt++;
}
if (cnt == nr)
{
k++;
safe[k] = i;
for (l = 1; l <= nr; l++)
av[0][l] = av[0][l] + all[i][l];
printf("\n P%d ", safe[k]);
printf("\t Availability");
for (l = 1; l <= nr; l++)
printf("R%d %d\t", l, av[0][l]);
w[i] = 1;
}
}
}
}
Output:
Enter the number of resources in a system 4
Enter the number of instances of resource R1 2
Enter the number of instances of resource R2 2
Enter the number of instances of resource R3 2
Enter the number of instances of resource R4 2
Enter the allocation matrix
10110
11000
00010
00000
Enter the maximum matrix
10100
10000
00001
pocess P1
allocated 1 maximum 0 need -1
allocated 0 maximum 0 need 0
allocated 1 maximum 0 need -1
allocated 1 maximum 0 need -1
pocess P2
allocated 0 maximum 1 need 1
allocated 1 maximum 0 need -1
allocated 1 maximum 1 need 0
allocated 0 maximum 0 need 0
pocess P3
allocated 0 maximum 0 need 0
allocated 0 maximum 1 need 1
allocated 0 maximum 0 need 0
allocated 0 maximum 0 need 0
pocess P4
allocated 0 maximum 0 need 0
allocated 1 maximum 0 need -1
allocated 0 maximum 0 need 0
allocated 0 maximum 0 need 0
AvailabilityR1 1 R2 0 R3 0 R4 1
safe sequence
P1 AvailabilityR1 2 R2 0 R3 1 R4 2
P2 AvailabilityR1 2 R2 1 R3 2 R4 2
P3 AvailabilityR1 2 R2 1 R3 2 R4 2
P4 AvailabilityR1 2 R2 2 R3 2 R4 2
Result:
Thus the banker’s algorithm for deadlock avoidance was simulated.
EX. NO: 8 IMPLEMENTATION OF DEADLOCK DETECTION ALGORITHM
Aim:
Algorithm:
Program:
#include <stdio.h>
static int mark[20];
int i, j, np, nr;
int main()
{
int alloc[10][10], request[10][10], avail[10], r[10], w[10];
printf("\nEnter the no of process: ");
scanf("%d", &np);
printf("\nEnter the no of resources: ");
scanf("%d", &nr);
for (i = 0; i < nr; i++)
{
printf("\nTotal Amount of the Resource R%d: ", i + 1);
scanf("%d", &r[i]);
}
printf("\nEnter the request matrix:");
for (i = 0; i < np; i++)
for (j = 0; j < nr; j++)
scanf("%d", &request[i][j]);
printf("\nEnter the allocation matrix:");
for (i = 0; i < np; i++)
for (j = 0; j < nr; j++)
scanf("%d", &alloc[i][j]);
/*Available Resource calculation*/
for (j = 0; j < nr; j++)
{
avail[j] = r[j];
for (i = 0; i < np; i++)
{
avail[j] -= alloc[i][j];
}
}
//marking processes with zero allocation
for (i = 0; i < np; i++)
{
int count = 0;
for (j = 0; j < nr; j++)
{
if (alloc[i][j] == 0)
count++;
else
break;
}
if (count == nr)
mark[i] = 1;
}
// initialize W with avail
for (j = 0; j < nr; j++)
w[j] = avail[j];
//mark processes with request less than or equal to W
for (i = 0; i < np; i++)
{
int canbeprocessed = 0;
if (mark[i] != 1)
{
for (j = 0; j < nr; j++)
{
if (request[i][j] <= w[j])
canbeprocessed = 1;
else
{
canbeprocessed = 0;
break;
}
}
if (canbeprocessed)
{
mark[i] = 1;
Deadlock detected
Result:
Thus the deadlock detection algorithm was implemented.
IMPLEMENTATION OF THREADING & SYNCHRONIZATION
EX. NO: 9
APPLICATIONS
Aim:
To implement threading & synchronization applications using c.
Algorithm:
1. Start the program
2. Read the Input
3. Allocate the memory
4. Process the input
5. Checking error
6. Print result
Program:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* trythis(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d has finished\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int error;
while(i < 2)
{
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
Output:
Result:
Thus the threading and synchronization concept was implemented.
IMPLEMENTATION OF PAGING TECHNIQUE FOR MEMORY
EX. NO: 10
MANAGEMENT
Aim:
To implement simple paging technique.
Algorithm:
1. Start the process
4. Check the need of replacement from old page to new page in memory
Program:
#include <stdio.h>
#define max 25
main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
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]);
}
Block 1:5
Block 2:2
Block 3:7
File 1:1
File 2:4
1 1 1 5 4
2 4 3 7 3
Result:
Aim:
To write a program to implement first fit algorithm for memory management.
Algorithm:
1. Start the process.
2. Declare the size.
3. Get the number of processes to be inserted.
4. Allocate the first hole that is big enough for searching.
5. Start the beginning of the set of holes.
6. If not start at the hole, which is sharing the previous first fit search end.
7. Compare the hole.
8. If large enough, then stop searching in the procedure.
9. Display the values.
10. Terminate the process.
Program:
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
clrscr();
printf("Enter no of blocks:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dst block size:",i);
scanf("%d",&a[i]);
}
printf("\nEnter the no of process:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nEnter the size of %dst process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The process %d allocated %d\n",j,i);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("\n The process is not allocated \n",j);
}
}
getch();
}
Output:
Enter no of blocks:
3
Enter the 0st block size:70
Enter the 1st block size:50
Enter the 2st block size:90
Result:
Aim:
Program:
#include <stdio.h>
#include <conio.h>
#define max 25
void main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
static int bf[max], ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst 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) //if bf[j] is not allocated
{
temp = b[j] - f[i];
if (temp >= 0)
if (highest < temp)
{
ff[i] = j;
highest = temp;
}
}
}
frag[i] = highest;
bf[ff[i]] = 1;
highest = 0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for (i = 1; i <= nf; 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();
}
Output:
Enter the no of blocks: 3
Block 1:5
Block 2: 2
Block 3:7
File 1:1
File 2:4
1 1 3 7 6
2 4 1 5 1
Result:
Aim:
To write a program to implement best fit algorithm for memory management.
Algorithm:
1. Start the process.
2. Declare the size.
3. Get the number of processes to be inserted.
4. Allocate the best hole that is small enough for searching.
5. Start at the best of the set of holes.
6. If not start at the hole, which is sharing the previous best fit search end.
7. Compare the hole.
8. If small enough, then stop searching in the procedure.
9. Display the values.
10. Terminate the process.
Program:
#include <stdio.h>
#include <process.h>
void main()
{
int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20];
clrscr();
printf("Enter the no of blocks:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the %dst block size:", i);
scanf("%d", &a[i]);
b[i] = i;
}
printf("Enter the no of process:");
scanf("%d", &m);
for (i = 0; i < m; i++)
{
printf("Enter the size of %dst process:", i);
scanf("%d", &p[i]);
c[i] = i;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (a[i] < a[j])
{
temp = a[i];
temp1 = b[i];
a[i] = a[j];
b[i] = b[j];
a[j] = temp;
b[j] = temp1;
}
Result:
Thus the Best fit algorithm was implemented successfully.
EX. NO: 12A IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - FIFO
Aim:
To implement FIFO page replacement technique.
Algorithm:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
Program:
#include<stdio.h>
main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
if(frame[k]==a[i])
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
}
printf("\n");
printf("Page Fault Is %d",count);
return 0;
}
Output:
ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES: 3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
Page Fault Is 15
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1
Page Fault Is 15
Result:
Thus the implementation of FIFO page replacement algorithm was executed and output
verified.
EX. NO: 12B IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - LRU
Aim:
Algorithm:
Program:
#include <stdio.h>
main()
{
int q[20], p[50], c = 0, c1, d, f, i, j, k = 0, n, r, t, b[20], c2[20];
printf("Enter no of pages:");
scanf("%d", &n);
printf("Enter the reference string:");
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
printf("Enter no of frames:");
scanf("%d", &f);
q[k] = p[k];
printf("\n\t%d\n", q[k]);
c++;
k++;
for (i = 1; i < n; i++)
{
c1 = 0;
for (j = 0; j < f; j++)
{
if (p[i] != q[j])
c1++;
}
if (c1 == f)
{
c++;
if (k < f)
{
q[k] = p[i];
k++;
for (j = 0; j < k; j++)
printf("\t%d", q[j]);
printf("\n");
}
else
{
for (r = 0; r < f; r++)
{
c2[r] = 0;
for (j = i - 1; j < n; j--)
{
if (q[r] != p[j])
c2[r]++;
else
break;
}
}
Enter no of pages: 10
Enter the reference string: 7 5 9 4 3 7 9 6 2 1
Enter no of frames: 3
7
75
759
459
439
437
937
967
962
162
The no of page faults is 10
Result:
Thus the LRU page replacement algorithm was implemented and output verified.
EX. NO: 12C IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - LFU
Aim:
Algorithm:
1. Start program
2. Read number of pages and frames
3. Read each page value
4. Search for page in the frames
5. If not available allocate free frame
6. If no frames is free replace the page with the page that is lastly used
7. Print page number of page faults
8. Stop process.
Program:
#include <stdio.h>
int main()
{
int f, p;
int pages[50], frame[10], hit = 0, count[50], time[50];
int i, j, page, flag, least, minTime, temp;
printf("Enter no of frames : ");
scanf("%d", &f);
printf("Enter no of pages : ");
scanf("%d", &p);
for (i = 0; i < f; i++)
{
frame[i] = -1;
}
for (i = 0; i < 50; i++)
{
count[i] = 0;
}
if (flag)
{
minTime = 50;
for (j = 0; j < f; j++)
{
if (count[frame[j]] == count[least] && time[frame[j]] < minTime)
{
temp = j;
minTime = time[frame[j]];
}
}
count[frame[temp]] = 0;
frame[temp] = pages[i];
}
for (j = 0; j < f; j++)
{
printf("%d ", frame[j]);
}
printf("\n");
}
printf("Page hit = %d", hit);
return 0;
}
Output:
Enter no of frames: 3
Enter no of pages: 1 4 7 8 5 2 3 6 0 9
Enter page no:
4 -1 -1
Page hit = 0
Result:
Aim:
Algorithm:
1. Start
2. Declare the number, names and size of the directories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories.
5. Stop.
Program:
#include <stdio.h>
int main()
{
int master, s[20];
char f[20][20][20];
char d[20][20];
int i, j;
printf("enter number of directorios:");
scanf("%d", &master);
printf("enter names of directories:");
for (i = 0; i < master; i++)
scanf("%s", &d[i]);
printf("enter size of directories:");
for (i = 0; i < master; i++)
scanf("%d", &s[i]);
printf("enter the file names :");
for (i = 0; i < master; i++)
for (j = 0; j < s[i]; j++)
scanf("%s", &f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
for (i = 0; i < master; i++)
{
printf("%s\t\t%2d\t", d[i], s[i]);
for (j = 0; j < s[i]; j++)
printf("%s\n\t\t\t", f[i][j]);
printf("\n");
}
printf("\t\n");
}
Output:
Result:
Aim:
Algorithm:
#include <stdio.h>
main()
{
int f[50], i, st, j, len, c, k;
Output:
Result:
Aim:
Algorithm:
Output:
Result:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int f[50], p, i, j, k, a, st, len, n, c;
clrscr();
for (i = 0; i < 50; i++)
f[i] = 0;
printf("Enter how many blocks that are already allocated");
scanf("%d", &p);
printf("\nEnter the blocks no.s that are already allocated");
for (i = 0; i < p; i++)
{
scanf("%d", &a);
f[a] = 1;
}
X:
printf("Enter the starting index block &length");
scanf("%d%d", &st, &len);
k = len;
for (j = st; j < (k + st); j++)
{
if (f[j] == 0)
{
f[j] = 1;
printf("\n%d->%d", j, f[j]);
}
else
{
printf("\n %d->file is already allocated", j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d", &c);
if (c == 1)
goto X;
else
exit();
getch();
}
Output:
Enter index block 9
Enter no of files on index 3
123
Allocated file indexed
9->1:1
9->2:1
9->3:1
Enter 1 to enter more files and 0 to exit
Result:
Aim:
To implement the disk scheduling using First Come First Serve Algorithm.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, n, TotalHeadMoment = 0, initial;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
// logic for FCFS disk scheduling
for (i = 0; i < n; i++)
{
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
printf("Total head moment is %d", TotalHeadMoment);
return 0;
}
Output:
Result:
Thus the disk scheduling using First Come First Serve was implemented successfully.
EX. NO: 15B IMPLEMENTATION OF DISK SCHEDULING ALGORITHM - SSTF
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, n, TotalHeadMoment = 0, initial, count = 0;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
Output:
Result:
Thus the disk scheduling using Short Seek Time First was implemented successfully.
EX. NO: 15C IMPLEMENTATION OF DISK SCHEDULING ALGORITHM - SCAN
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],
queue2[20], temp1 = 0, temp2 = 0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d", &max);
printf("Enter the initial head position\n");
scanf("%d", &head);
printf("Enter the size of queue request\n");
scanf("%d", &n);
printf("Enter the queue of disk positions to be read\n");
for (i = 1; i <= n; i++)
{
scanf("%d", &temp);
if (temp >= head)
{
queue1[temp1] = temp;
temp1++;
}
else
{
queue2[temp2] = temp;
temp2++;
}
}
for (i = 0; i < temp1 - 1; i++)
{
for (j = i + 1; j < temp1; j++)
{
if (queue1[i] > queue1[j])
{
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
for (i = 0; i < temp2 - 1; i++)
{
for (j = i + 1; j < temp2; j++)
{
if (queue2[i] < queue2[j])
{
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
for (i = 1, j = 0; j < temp1; i++, j++)
queue[i] = queue1[j];
queue[i] = max;
for (i = temp1 + 2, j = 0; j < temp2; i++, j++)
queue[i] = queue2[j];
queue[i] = 0;
queue[0] = head;
for (j = 0; j <= n + 1; j++)
{
diff = abs(queue[j + 1] - queue[j]);
seek += diff;
printf("Disk head moves from %d to %d with seek %d\n", queue[j],
queue[j + 1], diff);
}
printf("Total seek time is %d\n", seek);
avg = seek / (float) n;
printf("Average seek time is %f\n", avg);
return 0;
}
Output:
Enter the max range of disk
500
Enter the initial head position
100
Enter the size of queue request
6
Enter the queue of disk positions to be read
150
210
86
405
325
65
Disk head moves from 100 to 150 with seek 50
Disk head moves from 150 to 210 with seek 60
Disk head moves from 210 to 325 with seek 115
Disk head moves from 325 to 405 with seek 80
Disk head moves from 405 to 500 with seek 95
Disk head moves from 500 to 86 with seek 414
Disk head moves from 86 to 65 with seek 21
Disk head moves from 65 to 0 with seek 65
Total seek time is 900
Average seek time is 150.000000
Result:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (RQ[j] > RQ[j + 1])
{
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}
if (move == 1)
{
for (i = index; i < n; i++)
66
{
200
Enter the head movement direction for high 1 and for low 0
Result:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (RQ[j] > RQ[j + 1])
{
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}
if (move == 1)
{
68
Output:
Enter the number of Request
8
Enter the Requests Sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Enter the head movement direction for high 1 and for low 0
Result:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (RQ[j] > RQ[j + 1])
{
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}
if (move == 1)
{
for (i = index; i < n; i++)
{
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
Output:
Enter the number of Requests
Enter the head movement direction for high 1 and for low 0
Result:
Aim:
Procedure:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
Step 11:
Step 12:
Step 13:
Step 14:
Step 15:
Step 16:
Step 17:
Step 18:
Step 19:
Step 20:
Step 21:
Step 22:
Step 23:
Step 24:
Result: