0% found this document useful (0 votes)
28 views33 pages

5th Sem OS LAB Manual

Uploaded by

kumarsomit31
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)
28 views33 pages

5th Sem OS LAB Manual

Uploaded by

kumarsomit31
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/ 33

EXPERIMENT – 1

BASIC LINUX COMMANDS

• ls - The most frequently used command in Linux to list directories


• pwd - Print working directory command in Linux
• cd - Linux command to navigate through directories
• mkdir - Command used to create directories in Linux
• mv - Move or rename files in Linux
• cp - Similar usage as mv but for copying files in Linux
• rm - Delete files or directories
• touch - Create blank/empty files
• ln - Create symbolic links (shortcuts) to other files
• cat - Display file contents on the terminal
• clear - Clear the terminal display
• echo - Print any text that follows the command
• less - Linux command to display paged outputs in the terminal
• man - Access manual pages for all Linux commands
• uname - Linux command to get basic information about the OS
• whoami - Get the active username
• tar - Command to extract and compress files in Linux
• grep - Search for a string within an output
• head - Return the specified number of lines from the top
• tail - Return the specified number of lines from the bottom
• diff - Find the difference between two files
• cmp - Allows you to check if two files are identical
• comm - Combines the functionality of diff and cmp
• sort - Linux command to sort the content of a file while outputting
• export - Export environment variables in Linux
• zip - Zip files in Linux
• unzip - Unzip files in Linux
• ssh - Secure Shell command in Linux
• service - Linux command to start and stop services
• ps - Display active processes
• kill and killall - Kill active processes by process ID or name
• df - Display disk filesystem information
• mount - Mount file systems in Linux
• chmod - Command to change file permissions
• chown - Command for granting ownership of files or folders
• ifconfig - Display network interfaces and IP addresses
• traceroute - Trace all the network hops to reach the destination
• wget - Direct download files from the internet
• ufw - Firewall command
• iptables - Base firewall for all other firewall utilities to interface
with
• apt, pacman, yum, rpm - Package managers depending on the distro
• sudo - Command to escalate privileges in Linux
• cal - View a command-line calendar
• alias - Create custom shortcuts for your regularly used commands
• dd - Majorly used for creating bootable USB sticks
• whereis - Locate the binary, source, and manual pages for a command
• whatis - Find what a command is used for
• top - View active processes live with their system usage
• useradd and usermod - Add new user or change existing users data
• passwd - Create or update passwords for existing users
• who – Displays current logged in users

EXPERIMENT - 2
LINUX ADMIN COMMANDS

• ip – from Iproute2, a collection of utilities for controlling TCP/IP


networking and traffic control in Linux.
• ls – list directory contents.
• df – display disk space usage.
• du – estimate file space usage.
• free – display memory usage.
• scp – securely Copy Files Using SCP, with examples.
• find – locates files based on some user-specified criteria.
• ncdu – a disk utility for Unix systems.
• pstree – display a tree of processes.
• last – show a listing of last logged-in users.
• w – show a list of currently logged-in user sessions.
• grep – Search a file for a pattern of characters, then display all
matching lines.
• uptime – shows system uptime and load average.
• top – shows an overall system view.
• vmstat – shows system memory, processes, interrupts, paging, block I/O,
and CPU info.
• htop – interactive process viewer and manager.
• dstat – view processes, memory, paging, I/O, CPU, etc., in real-time.
All-in-one for vmstat, iostat, netstat, and ifstat.
• iftop – network traffic viewer.
• nethogs – network traffic analyzer.
• iotop – interactive I/O viewer. Get an overview of storage r/w
activity.
• iostat – for storage I/O statistics.
• netstat – for network statistics.
• ss – utility to investigate sockets.
• atop – For Linux server performance analysis.
• Glances and nmon – htop and top Alternatives:
• ssh – secure command-line access to remote Linux systems.
• sudo – execute commands with administrative privilege.
• cd – directory navigation.
• pwd – shows your current directory location.
• cp – copying files and folders.
• mv – moving files and folders.
• rm – removing files and folders.
• mkdir – create or make new directories.
• touch – used to update the access date and modification date of a
computer file or directory.
• man – for reading system reference manuals.
• apropos – Search man page names and descriptions.
• rsync – remote file transfers and syncing.
• tar – an archiving utility.
• gzip – file compression and decompression.
• bzip2 – similar to gzip. It uses a different compression algorithm.
• zip – for packaging and compressing (to archive) files.
• locate – search files in Linux.
• ps – information about the currently running processes.
• Making use of Bash scripts. Example: ./bashscript.sh
• cron – set up scheduled tasks to run.
• nmcli – command-line tool for controlling NetworkManager.
• ping – send ICMP ECHO_REQUEST to network hosts.
• traceroute – check the route packets take to a specified host.
• mtr – network diagnostic tool.
• nslookup – query Internet name servers (NS) interactively.
• host – perform DNS lookups in Linux.
• dig – DNS lookup utility.
• wget – retrieve files over HTTP, HTTPS, FTP, and FTPS.
• curl – transferring data using various network protocols. (supports
more protocols than wget)
• dd – convert and copy files.
• fdisk – manipulate the disk partition table.
• parted – for creating and manipulating partition tables.
• blkid – command-line utility to locate/print block device attributes.
• mkfs – build a Linux file system.
• fsck – tool for checking the consistency of a file system.
• whois – client for the whois directory service.
• nc – command-line networking utility. (Also, see 60 Linux Networking
commands and scripts.)
• umask – set file mode creation mask.
• chmod – change the access permissions of file system objects.
• chown – change file owner and group.
• chroot – run command or interactive shell with a special root
directory.
• useradd – create a new user or update default new user information.
• userdel – used to delete a user account and all related files.
• usermod – used to modify or change any attributes of an existing user
account.
• vi – text editor.
• cat – display file contents.
• tac – output file contents, in reverse.
• more – display file contents one screen/page at a time.
• less – similar to the more command with additional features.
• tail – used to display the tail end of a text file or piped data.
• dmesg – prints the message buffer of the kernel ring.
• journalctl – query the systemd journal.
• kill – terminate a process.
• killall – Sends a kill signal to all instances of a process by name.
• sleep – suspends program execution for a specified time.
• wait – Suspend script execution until all jobs running in the
background have been terminated.
• nohup – Run Commands in the Background.
• screen – hold a session open on a remote server. (also a full-screen
window manager)
• tmux – a terminal multiplexer.
• passwd – change a user’s password.
• chpassword – allows users to change the password for various user
accounts.
• mount / umount – provides access to an entire filesystem in one
directory.
• systemctl – central management tool for controlling the init system.
• clear – clears the screen of the terminal.
• env -Run a command in a modified environment.
EXPERIMENT - 3
UNIX SHELL PROGRAMMING

1.Write a Shell program to check the given number is even or odd

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of n.
STEP 3: Calculate „r=expr $n%2‟.
STEP 4: If the value of r equals 0 then print the number is even
STEP 5: If the value of r not equal to 0 then print the number is
odd.

PROGRAM:
echo "Enter the Number"
read n
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo "$n is Even number"
else
echo "$n is Odd number"
fi

2.Write a Shell program to check the given year is leap year or


not

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of year.
STEP 3: Calculate „b=expr $y%4‟.
STEP 4: If the value of b equals 0 then print the year is a leap
year
STEP 5: If the value of r not equal to 0 then print the year is
not a leap year.

PROGRAM:
echo "Enter the year"
read y
b=`expr $y % 4`
if [ $b -eq 0 ]
then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi

3.Write a Shell program to find the factorial of a number

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of n.
STEP 3: Calculate „i=expr $n-1‟.
STEP 4: If the value of i is greater than 1 then calculate „n=expr
$n \* $i‟ and „i=expr $i – 1‟
STEP 5: Print the factorial of the given number.

PROGRAM:
echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"

4.Write a Shell program to swap the two integers

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of a,b.
STEP 3: Calculate the swapping of two values by using a temporary
variable temp.
STEP 4: Print the value of a and b.

PROGRAM:
echo "Enter Two Numbers"
read a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b
EXPERIMENT - 4
PROGRAMS ON PROCESS MANAGEMENT

AIM:
To write a c program to implement IPC using shared memory.
ALGORITHM:
Step 1: Start the process
Step 2: Declare the segment size
Step 3: Create the shared memory
Step 4: Read the data from the shared memory
Step 5: Write the data to the shared memory
Step 6: Edit the data
Step 7: Stop the process
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[]="poooda......";
key=ftok(".",'s');
if((shmid=shmget(key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666))== -1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory seg \n");
printf("SHMID:%d",shmid);
}
system("ipcs –m");
if((segptr=(char*)shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to shared memory…\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from shared memory…\n");
printf("DATA:-%s\n",segptr);
printf("DONE\n");
printf("Removing shared memory Segment…\n");
if(shmctl(shmid,IPC_RMID,0)== -1)
printf("Can‟t Remove Shared memory Segment…\n");
else
printf("Removed Successfully");
}

AIM:
To write a C-program to implement the producer – consumer problem
using semaphores.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want
to produce.
Step 4: Get the option, which you want to do either producer,
consumer or exit from the
operation.
Step 5: If you select the producer, check the buffer size if it is
full the producer should not
produce the item or otherwise produce the item and increase the
value buffer size.
Step 6: If you select the consumer, check the buffer size if it is
empty the consumer should not
consume the item or otherwise consume the item and decrease the
value of buffer size.
Step 7: If you select exit come out of the program.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1) {
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{ case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
}
int wait(int s) {
return(--s); }
int signal(int s) {
return(++s); }
void producer() {
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex); }
void consumer() {
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}
EXPERIMENT - 5
PROGRAMS ON UNIX SYSTEM CALLS

AIM:
To write C Programs using the following system calls of UNIX
operating system fork, exec, getpid, exit, wait, close, stat,
opendir, readdir.
1. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEMS (OPENDIR,
READDIR, CLOSEDIR)
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create struct dirent.
STEP 3: declare the variable buff and pointer dptr.
STEP 4: Get the directory name.
STEP 5: Open the directory.
STEP 6: Read the contents in directory and print it.
STEP 7: Close the directory.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER DIRECTORY NAME”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);
}
EXPERIMENT - 6
PROGRAMS ON CPU SCHEDULING ALGORITHM

AIM:
To write a C program for implementation of Priority scheduling
algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime
is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and
priority.
Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and
assign as turnaround time.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pno;
int pri;
int pri;
int btime;
int wtime;
}sp;
int main()
{
int i,j,n;
int tbm=0,totwtime=0,totttime=0;
sp *p,t;
printf("\n PRIORITY SCHEDULING.\n");
printf("\n enter the no of process....\n");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("enter the burst time and priority:\n");
for(i=0;i<n;i++)
{
printf("process%d:”,i+1);
scanf("%d%d",&p[i].btime,&p[i].pri);
p[i].pno=i+1;
p[i].wtime=0;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
printf("\n process\tbursttime\twaiting time\tturnaround time\n");
for(i=0;i<n;i++)
{
totwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pno,p[i].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].wtime+p[i].btime);
}
totttime=tbm+totwtime;
printf("\n total waiting time:%d",totwtime);
printf("\n average waiting time:%f",(float)totwtime/n);
printf("\n total turnaround time:%d",totttime);
printf("\n avg turnaround time:%f",(float)totttime/n);
}

AIM:
To write a C program for implementation of Round Robin scheduling
algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime
is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and
priority and read the time quantum.
Step 5: Assign wtime as zero.
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and
assign as turnaround time.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct rr
{
int pno,btime,sbtime,wtime,lst;
}p[10];
int main()
{
int pp=-1,ts,flag,count,ptm=0,i,n,twt=0,totttime=0;
printf("\n round robin scheduling............");
printf("enter no of processes:");
scanf("%d",&n);
printf("enter the time slice:");
scanf("%d",&ts);
printf("enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process%d\t",i+1);
scanf("%d",&p[i].btime);
p[i].wtime=p[i].lst=0;
p[i].pno=i+1;
p[i].sbtime=p[i].btime;
}
printf("scheduling....\n");
do
{
flag=0;
for(i=0;i<n;i++)
{
count=p[i].btime;
if(count>0)
{
flag=-1;
count=(count>=ts)?ts:count;
printf("\n process %d",p[i].pno);
printf("from%d",ptm);
ptm+=count;
printf("to%d",ptm);
p[i].btime-=count;
if(pp!=i)
{
pp=i;
p[i].wtime+=ptm-p[i].lst-count;
p[i].lst=ptm;
}
}
AIM:
To write a C program for implementation of FCFS and SJF scheduling
algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime
is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of
p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside
the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by
dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct fcfs
{
int pid;
int btime;
int wtime;
int ttime;
}
p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0;
printf("\n fcfs scheduling...\n");
printf("enter the no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i].pid=1;
printf("\n burst time of the process”);
scanf("%d",&p[i].btime);
}
p[0].wtime=0;
p[0].ttime=p[0].btime;
totttime+=p[i].ttime;
for(i=0;i<n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].btim
p[i].ttime=p[i].wtime+p[i].btime;
totttime+=p[i].ttime;
towtwtime+=p[i].wtime;
}
for(i=0;i<n;i++)
{{
printf("\n waiting time for process”);
printf("\n turn around time for process”);
printf("\n");
}}
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time :%f",(float)totwtime/n);
printf("\n total turn around time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}

AIM:
To write a C program for implementation of SJF scheduling
algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime
is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of
p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside
the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by
dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pid;
int btime;
int wtime;
}
sp;
int main()
{
int i,j,n,tbm=0,towtwtime=0,totttime
sp*p,t;
printf("\n sjf schaduling ..\n");
printf("enter the no of processor");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("\n enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process %d\t",i+1);
scanf("%d",&p[i].btime);
p[i].pid=i+1;
p[i].wtime=0;
}
for(i=0;i<n;i++)
for(j=j+1,j<n;j++)
{
if(p[i].btime>p[j].btime)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}}
printf("\n process scheduling\n");
printf("\n process \tburst time \t w
for(i=0;i<n;i++)
{
towtwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pid,p[i].bt
printf("\t\t%d\t\t%d",p[i].wtime,p[i
}
totttime=tbm+towtwtime;
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time :%f",(float)totwtime/n);
printf("\n total turn around time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}
EXPERIMENT - 7
PROGRAMS ON DEADLOCK

AIM:
To write a C program to implement banker‟s algorithm for deadlock
avoidance.
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix
and available matrix.
Step-4: Compare each and every process using the banker‟s
algorithm.
Step-5: If the process is in safe state then it is a not a
deadlock process otherwise it is a
deadlock process
Step-6: produce the result of state of process
Step-7: Stop the program
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()
{
int i,j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
31 | P a g escanf("%d",&n);
printf("Enter the no of resources instances\t");
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\t");
for(i=0;i<n;i++)
{
printf("\nP%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]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int 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];
}}
printf("\n");
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][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}}}}}}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{printf("P%d->",i);
}}
if(c1==n)
{printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}

AIM:
To write a C program to implement algorithm for deadlock
detection.
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix
and available matrix.
Step-4: Compare each and every process using the banker‟s
algorithm.
Step-5: If the process is in safe state then it is a not a
deadlock process otherwise it is a
deadlock process
Step-6: produce the result of state of process
Step-7: Stop the program
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()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
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\t");
for(i=0;i<n;i++)
{
printf("\nP%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]);
}}}
void cal()
{ int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int 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][j];
finish[i]=1;
flag=1;
}//printf("\nP%d",i);
if(finish[i]==1)
{i=n;
}}}}}}
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 process are\
n");
for(i=0;i<n;i++)
{printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur");
}
}

EXPERIMENT - 8
PROGRAMS ON PAGE REPLACEMENT ALGORITHM

AIM:
To write a C program for implementation of FIFO page replacement
algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Enter the number of frames.
Step 4: Enter the reference string end with zero.
Step 5: FIFO page replacement selects the page that has been in
memory the longest time and
when the page must be replaced the oldest page is chosen.
Step 6: When a page is brought into memory, it is inserted at the
tail of the queue.
Step 7: Initially all the three frames are empty.
Step 8: The page fault range increases as the no of allocated
frames also increases.
Step 9: Print the total number of page faults.
Step 10: Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int i=0,j=0,k=0,i1=0,m,n,rs[30],flag=1,p[30];
system("clear");
printf("FIFO page replacement algorithm....\\n");
printf("enter the no. of frames:");
scanf("%d",&n);
printf("enter the reference string:");
while(1)
{
scanf("%d",&rs[i]);
if(rs[i]==0)
break;
i++;
}
m=i;
for(j=0;j<n;j++)
p[j]=0;
for(i=0;i<m;i++)
{
flag=1;
for(j=0;j<n;j++)
if(p[j]==rs[i])
{
printf("data already in page....\n");
flag=0;
break;
}
if(flag==1)
{
p[i1]=rs[i];
i1++;
k++;
if(i1==n)
i1=0;
for(j=0;j<n;j++)
{
printf("\n page %d:%d",j+1,p[j]);
if(p[j]==rs[i])
printf("*");
}
printf("\n\n");
}
}
printf("total no page faults=%d",k);
}

AIM:
To write a c program to implement LRU page replacement algorithm.
ALGORITHM:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least recently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process
ROGRAM:
#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;
}}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}}}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}}}
printf("\nThe no of page faults is %d",c);
}

AIM:
To write C program to implement LFU page replacement algorithm.
ALGORITHM:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least frequently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the 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;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
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;
}

EXPERIMENT - 9
ANDROID PROGRAMMING FOR MOBILE APPLICATION (TIC-TAC-TOE GAME)

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private int grid_size;


TableLayout gameBoard;
TextView txt_turn;
char [][] my_board;
char turn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

grid_size = Integer.parseInt(getString(R.string.size_of_board));
my_board = new char [grid_size][grid_size];
gameBoard = (TableLayout) findViewById(R.id.mainBoard);
txt_turn = (TextView) findViewById(R.id.turn);
resetBoard();
txt_turn.setText("Turn: "+turn);

for(int i = 0; i< gameBoard.getChildCount(); i++){


TableRow row = (TableRow) gameBoard.getChildAt(i);
for(int j = 0; j<row.getChildCount(); j++){
TextView tv = (TextView) row.getChildAt(j);
tv.setText(R.string.none);
tv.setOnClickListener(Move(i, j, tv));
}
}

Button reset_btn = (Button) findViewById(R.id.reset);


reset_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent current = getIntent();
finish();
startActivity(current);
}
});
}

protected void resetBoard(){


turn = 'X';
for(int i = 0; i< grid_size; i++){
for(int j = 0; j< grid_size; j++){
my_board[i][j] = ' ';
}
}
}

protected int gameStatus(){

//0 Continue
//1 X Wins
//2 O Wins
//-1 Draw
int rowX = 0, colX = 0, rowO = 0, colO = 0;
for(int i = 0; i< grid_size; i++){
if(check_Row_Equality(i,'X'))
return 1;
if(check_Column_Equality(i, 'X'))
return 1;
if(check_Row_Equality(i,'O'))
return 2;
if(check_Column_Equality(i,'O'))
return 2;
if(check_Diagonal('X'))
return 1;
if(check_Diagonal('O'))
return 2;
}

boolean boardFull = true;


for(int i = 0; i< grid_size; i++){
for(int j = 0; j< grid_size; j++){
if(my_board[i][j]==' ')
boardFull = false;
}
}
if(boardFull)
return -1;
else return 0;
}

protected boolean check_Diagonal(char player){


int count_Equal1 = 0,count_Equal2 = 0;
for(int i = 0; i< grid_size; i++)
if(my_board[i][i]==player)
count_Equal1++;
for(int i = 0; i< grid_size; i++)
if(my_board[i][grid_size -1-i]==player)
count_Equal2++;
if(count_Equal1== grid_size || count_Equal2== grid_size)
return true;
else return false;
}

protected boolean check_Row_Equality(int r, char player){


int count_Equal=0;
for(int i = 0; i< grid_size; i++){
if(my_board[r][i]==player)
count_Equal++;
}

if(count_Equal== grid_size)
return true;
else
return false;
}

protected boolean check_Column_Equality(int c, char player){


int count_Equal=0;
for(int i = 0; i< grid_size; i++){
if(my_board[i][c]==player)
count_Equal++;
}

if(count_Equal== grid_size)
return true;
else
return false;
}

protected boolean Cell_Set(int r, int c){


return !(my_board[r][c]==' ');
}

protected void stopMatch(){


for(int i = 0; i< gameBoard.getChildCount(); i++){
TableRow row = (TableRow) gameBoard.getChildAt(i);
for(int j = 0; j<row.getChildCount(); j++){
TextView tv = (TextView) row.getChildAt(j);
tv.setOnClickListener(null);
}
}
}

View.OnClickListener Move(final int r, final int c, final


TextView tv){

return new View.OnClickListener() {


@Override
public void onClick(View v) {

if(!Cell_Set(r,c)) {
my_board[r][c] = turn;
if (turn == 'X') {
tv.setText(R.string.X);
turn = 'O';
} else if (turn == 'O') {
tv.setText(R.string.O);
turn = 'X';
}
if (gameStatus() == 0) {
txt_turn.setText("Turn: Player " + turn);
}
else if(gameStatus() == -1){
txt_turn.setText("This is a Draw match");
stopMatch();
}
else{
txt_turn.setText(turn+" Loses!");
stopMatch();
}
}
else{
txt_turn.setText(txt_turn.getText()+" Choose
an Empty Call");
}
}
};
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if
it is present.
getMenuInflater().inflate(R.menu.menu_board, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so
long
// as you specify a parent activity in
AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

You might also like