CS3461- Lab Programs
CS3461- Lab Programs
TOTAL: 45 PERIODS
Ex. No. : 1 Installation of windows oper ating system
Date:
Aim
To install the windows oper ating System.
Pr er equisites
1. Backup Your Data: Befor e star ting the installation pr ocess, back up any impor tant
data on your computer to an exter nal dr ive or cloud stor age.
2. Windows Installation Media: A Windows installation USB dr ive or DVD is needed. A
bootable USB dr ive can be cr eated using the Media Cr eation Tool fr om Micr osoft's
website.
3. Pr oduct Key: Windows pr oduct key should be known.
Steps to Install Windows
Step 1: Cr eate Installation Media
1. Download the Media Cr eation Tool fr om the Micr osoft website for Windows 10 or
Windows 11.
2. Run the Tool: Follow the instr uctions to cr eate a bootable USB dr ive.
● Inser t a USB dr ive (at least 8GB).
● Select "Cr eate installation media (USB flash dr ive, DVD, or ISO file) for
another PC".
● Follow the pr ompts to complete the pr ocess.
Step 2: Boot fr om Installation Media
1. Inser t the USB Dr ive or DVD into the computer .
2. Restar t the Computer and enter the BIOS/UEFI setup.
● This typically involves pr essing a key like F2, F12, Del, or Esc immediately
after tur ning on the computer . The key var ies by manufactur er .
1. Change the Boot Or der : Set the USB dr ive or DVD dr ive as the fir st boot device.
2. Save and Exit: The computer will r estar t and boot fr om the installation media.
Step 3: Install Windows
1. Language, Time, and Keyboar d Pr efer ences: Choose the pr efer ences and click
"Next".
2. Install Now: Click the "Install Now" button.
3. Enter Pr oduct Key: Input the Windows pr oduct key when pr ompted.
4. Accept License Ter ms: Check the box to accept the license ter ms and click "Next".
5. Custom Installation: Choose "Custom: Install Windows only (advanced)" for a clean
install.
6. Select Par tition: Choose the par tition wher e to install Windows.
● If it is installed on a new dr ive, select the unallocated space and click "Next".
● If r einstalling, select the existing Windows par tition, for mat it, and then
pr oceed.
Step 4: Complete Installation
1. Windows Will Copy Files: The installer will copy files, install featur es, and updates.
2. Restar t Multiple Times: The computer will r estar t sever al times dur ing the pr ocess.
3. Per sonalize Settings: Follow the pr ompts to per sonalize the settings or use expr ess
settings.
4. Cr eate User Account: Set up a user account and passwor d.
5. Finalize Settings: Windows will finalize the settings and boot to the desktop.
Post- Installation
1. Install Updates: Go to Settings > Update & Secur ity > Windows Update and check
for updates.
2. Install Dr iver s: Ensur e all the har dwar e is r ecognized and functioning pr oper ly by
installing the latest dr iver s fr om the computer or mother boar d manufactur er 's
website.
3. Install Essential Softwar e: Install essential applications like web br owser s, office
suites, and antivir us softwar e.
By following these steps, installation of Windows should be done and r un on the computer .
1. Fir st input the windows 10 installer media you ar e using (flash or DVD)
2. Select Language to install, Time and cur r ency for mat, and Keyboar d or input method. If it
is selected, click next.
4. Then enter the ser ial number that you have. If you don’t have it, just click skip.
5. In this section, you can customize the Windows 10 that you will install. Adjust it to each
of your choices or needs! If it is selected, click Next.
6. Then check the I accept the license ter ms section. Then click Next
7. In this section, ther e ar e two options, which you can choose between upgr ade or custom.
Since we will r einstall our computer or laptop, then I choose custom.
8. Then we will continue with a split of par titions. Ur gent! Her e I hope you ar e ver y car eful,
especially for the fir st time. Because if it’s wr ong, your data might be lost (ther efor e, don’t
for get to backup data befor e r einstalling the computer /laptop).
9. Next ar e the steps in the pr ocess of allocating Windows files fr om the installer media to
the
har d disk. Wait for the pr ocess to finish.
10. Then the computer will ask to r estar t itself. Just click Restar t now.
11. Now almost finished, follow for the next step to the Windows 10 desktop. Then, enter
the ser ial number if asked. If you don’t have it, click Do this later .
13. Cr eate an account for your computer . Enter your user name, passwor d, and HINT (for a
r eminder if you for get your passwor d), don’t leave it blank because if for get your passwor d
have to for mat the data. When it is selected, click Next.
14. Congr atulations you have successfully installed windows 10 on your computer or laptop!
Result
Thus the windows oper ating system was successfully installed.
AIM:
To study about the basics of UNIX Commands.
UNIX:
It is a multi- user oper ating system. It was developed at AT&T Bell Industr ies, USA, in 1969
by Ken Thompson, along with Dennis Ritchie, who developed it fr om the MULTICS
(Multiplexed Infor mation and Computing Ser vice) OS. By 1980, UNIX had been completely
r ewr itten using the C language.
LINUX:
It is similar to UNIX, which was cr eated by Linus Tor valds. All UNIX commands wor k in
Linux. Linux is open- sour ce softwar e. The main featur e of Linux is coexisting with other OSs
such as Windows and UNIX.
PROCEDURE:
1. Log on to the Ubuntu oper ating system.
2. Open the ter minal.
3. The pr ompt,$ which is called a command pr ompt is issued by the shell
4. The shell is now r eady to r eceive commands fr om the user .
5. Var ious commands ar e executed as follows.
UNIX COMMANDS
GENERAL COMMANDS
a) date
–used to check the date and time Syn:$date
b) cal
–used to display the calendar
Syntax: $cal 2 2009
c) echo
–used to pr int the message on the scr een.
Syntax: $echo “text”
d) ls
–used to list the files. Your files ar e kept in a dir ector y.
Syntax: $lsls–s
e) lp
–used to take pr intouts
Syn: $lp filename
f) man
–used to pr ovide manual help on ever y UNIX commands.
Syn: $man unix command
$man cat
g) who & whoami
–it displays data about all user s who have logged into the system cur r ently. The next
command displays about cur r ent user only.
Syn: $who
$whoami
h) uptime
–tells you how long the computer has been r unning since its last r eboot or power - off.
Syn: $uptime
i) uname
–it displays the system infor mation such as har dwar e platfor m, system name and
pr ocessor , OS type.
Syn: $uname–a
j) hostname
–displays and set system host name
Syn: $ hostname
k) bc
–stands for best calculator
Aim
To wr ite simple shell pr ogr ams by using conditional, br anching and looping statements
Descr iption
The shell pr ovides an inter face to the UNIX system. It gather s input fr om user and executes
pr ogr ams based on that input. A shell is an envir onment in which we can r un our commands,
pr ogr ams, and shell scr ipts. Ther e ar e differ ent flavor s of shells, just as ther e ar e differ ent
flavor s of oper ating systems. Shell Types ar e
The Bour ne shell- If you ar e using a Bour ne- type shell, the default pr ompt is the $
char acter .
The C shell- If you ar e using a C- type shell, the default pr ompt is the % char acter .
Pr ogr am:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_ t pid; int x = 5;
pid = for k();
x++;
if (pid < 0)
{
pr intf("Pr ocess cr eation er r or ");
exit(- 1);
}
else if (pid == 0)
{
pr intf("Child pr ocess:");
pr intf("\nPr ocess id is %d", getpid());
pr intf("\nValue of x is %d", x);
pr intf("\nPr ocess id of par ent is %d\n", getppid());
}
else
{
pr intf("\nPar ent pr ocess:");
pr intf("\nPr ocess id is %d", getpid());
pr intf("\nValue of x is %d", x);
pr intf("\nPr ocess id of shell is %d\n", getppid());
}
r etur n 0;
}
Output:
Pr ogr am:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int i, status; pid_ t pid;
Output:
Child star ts
Odd Nos: 1 3 5 7 9
Child ends
Pr ogr am:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_ t pid;
switch(pid = for k())
{
case - 1:
per r or ("For k failed");
exit(- 1);
case 0:
pr intf("Child pr ocess\n");
execlp("/bin/date", "date",0);
exit(0);
default:
wait(NULL);
pr intf("Child Ter minated\n");
exit(0);
}
r etur n 0;
}
Output:
Child pr ocess
Wed Apr 17 06:20:01 UTC 2024
Child Ter minated
Pr ogr am:
Output:
opened the fd = 3
closed the fd.
Ex.No. 4a FCFS Scheduling Algor ithm
Pr ogr am:
#include <stdio.h>
#include<conio.h>
str uct pr ocess
{
int pid; int btime; int wtime; int ttime;
} p[ 10] ;
void main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ",(i+1)); scanf("%d", &p[ i] .btime);
p[ i] .pid = i+1;
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0; for (i=0; i<n; i++)
{
ttur += p[ i] .ttime; twat += p[ i] .wtime;
}
awat = (float)twat / n; atur = (float)ttur / n;
pr intf("\n FCFS Scheduling\n\n"); for (i=0; i<28; i++)
pr intf("- ");
pr intf("\nPr ocess B- Time T- Time W- Time\n"); for (i=0; i<28; i++)
pr intf("- ");
for (i=0; i<n; i++)
pr intf("\n P%d\t%4d\t%3d\t%2d", p[ i] .pid,p[ i] .btime,p[ i] .ttime,p[ i] .wtime);
pr intf("\n"); for (i=0; i<28; i++)
pr intf("- ");
pr intf("\n\nAver age waiting time : %5.2fms", awat); pr intf("\nAver age tur n ar ound time :
%5.2fms\n", atur );
Output
FCFS Scheduling
-----------------------------------------
----------
Pr ocess B- Time T- Time W- Time
-----------------------------------------
----------
P 10 10 0
P2 5 15 10
P3 15 30 15
-----------------------------------------
----------
GANTT Char t
-------------------------------------
| P1 | P2 | P3 |
-------------------------------------
0 10 15 30
#include <stdio.h>
str uct pr ocess
{
int pid; int btime; int wtime; int ttime;
} p[ 10] , temp;
main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ",(i+1));
scanf("%d", &p[ i] .btime);
p[ i] .pid = i+1;
}
for (i=0; i<n- 1; i++)
{
for (j=i+1; j<n; j++)
{
if((p[ i] .btime > p[ j] .btime) ||
(p[ i] .btime == p[ j] .btime && p[ i] .pid > p[ j] .pid))
{
temp = p[ i] ; p[ i] = p[ j] ; p[ j] = temp;
}
}
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0;
Output
Enter no. of pr ocess : 3
Bur st time for pr ocess P1 (in ms) : 10
Bur st time for pr ocess P2 (in ms) : 5
Bur st time for pr ocess P3 (in ms) : 15
SJF Scheduling
---------------------------------------------------------
------------
Pr ocess B- Time T- Time W- Time
---------------------------------------------------
------------------
P2 5 5 0
P1 10 15 5
P3 15 30 15
---------------------------------------------------
-----------------
GANTT Char t
-------------------------------------
| P2 | P1 | P3 |
-------------------------------------
0 5 15 30
Pr ogr am:
#include <stdio.h>
str uct pr ocess
{
int pid; int btime; int pr i; int wtime; int ttime;
} p[ 10] , temp;
main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ", (i+1));
scanf("%d", &p[ i] .btime);
pr intf("Pr ior ity for pr ocess P%d : ", (i+1));
scanf("%d", &p[ i] .pr i);
p[ i] .pid = i+1;
}
}
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0; for (i=0; i<n; i++)
{
ttur += p[ i] .ttime; twat += p[ i] .wtime;
}
awat = (float)twat / n; atur = (float)ttur / n;
pr intf("\n\t Pr ior ity Scheduling\n\n"); for (i=0; i<38; i++)
pr intf("- ");
pr intf("\nPr ocess B- Time Pr ior ity T- Time W- Time\n"); for (i=0; i<38; i++)
pr intf("- "); for (i=0; i<n; i++)
pr intf("\nP%- 4d\t%4d\t%3d\t%4d\t%4d", p[ i] .pid,p[ i] .btime,p[ i] .pr i,p[ i] .ttime,p[ i] .wtime);
pr intf("\n"); for (i=0; i<38; i++)
pr intf("- ");
pr intf("\n\nAver age waiting time : %5.2fms", awat);
pr intf("\nAver age tur n ar ound time : %5.2fms\n", atur );
pr intf("\n\nGANTT Char t\n"); pr intf("- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n|"); for (i=0; i<n; i++)
{
k = p[ i] .btime/2; for (j=0; j<k; j++)
pr intf(" "); pr intf("P%d",p[ i] .pid); for (j=k+1; j<p[ i] .btime; j++)
pr intf(" ");
pr intf("|");
}
pr intf("\n- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n0"); for (i=0; i<n; i++)
{
for (j=0; j<p[ i] .btime; j++) pr intf(" ");
pr intf("%2d",p[ i] .ttime);
}
getch();
r etur n 0;
}
Output:
Enter no. of pr ocess : 3
Bur st time for pr ocess P1 (in ms) : 10
Pr ior ity for pr ocess P1 : 5
Bur st time for pr ocess P2 (in ms) : 15
Pr ior ity for pr ocess P2 : 2
Bur st time for pr ocess P3 (in ms) : 15
Pr ior ity for pr ocess P3 : 1
GANTT Char t
-----------------------------------------
------
| P3 | P2 | P1 |
-----------------------------------------
------
0 15 30 40
Ex.No. 5a IPC using shar ed memor y
Pr ogr am:
Ser ver :
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
char c; int shmid;
key_ t key = 2013; char *shm, *s;
if ((shmid = shmget(key, shmsize, IPC_ CREAT|0666)) < 0)
{
per r or ("shmget"); exit(1);
}
pr intf("Shar ed memor y id : %d\n", shmid);
if ((shm = shmat(shmid, NULL, 0)) == (char *) - 1)
{
per r or ("shmat"); exit(1);
}
memset(shm, 0, shmsize); s = shm;
pr intf("Wr iting (a- z) onto shar ed memor y\n"); for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';
while (*shm != '*');
pr intf("Client finished r eading\n");
if(shmdt(shm) != 0)
fpr intf(stder r , "Could not close memor y segment.\n");
shmctl(shmid, IPC_ RMID, 0);
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
int shmid;
key_ t key = 2013; char *shm, *s;
Ser ver :
Shar ed memor y id : 0
Wr iting (a- z) onto shar ed memor y
Client finished r eading
Client:
Accessing shar ed memor y id : 0
Shar ed memor y contents:
abcdefghijklmnopqr stuvwxyz
Ex.No.5b IPC using Message Passing
Pr ogr am:
Ser ver :
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// str uctur e for message queue
str uct msg_ buffer {
long msg_ type;
char msg[ 100] ;
} message;
main() {
key_ t my_ key;
int msg_ id;
my_ key = ftok("pr ogfile", 65); //cr eate unique key
msg_ id = msgget(my_ key, 0666 | IPC_ CREAT); //cr eate message queue and r etur n id
message.msg_ type = 1;
pr intf("Wr ite Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_ id, &message, sizeof(message), 0); //send message
pr intf("Sent message is : %s \n", message.msg);
}
Client:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// Define message queue str uctur e
str uct msg_ buffer {
long msg_ type;
char msg[ 100] ;
} message;
main() {
key_ t my_ key;
int msg_ id;
my_ key = ftok("pr ogfile", 65); //cr eate unique key
msg_ id = msgget(my_ key, 0666 | IPC_ CREAT); //cr eate message queue and r etur n id
msgr cv(msg_ id, &message, sizeof(message), 1, 0); //used to r eceive message
// display the message
pr intf("Received Message is : %s \n", message.msg);
msgctl(msg_ id, IPC_ RMID, NULL); //destr oy the message queue
r etur n 0;
}
Output:
Ser ver :
Wr ite Message : Hai Hello
Sent message is : Hai Hello
Client:
Received Message is : Hai Hello
Ex.No.6 Mutual exclusion by Semaphor e
Pr ogr am:
#include <stdio.h>
#include <pthr ead.h>
#include <semaphor e.h>
#include <unistd.h>
sem_ t mutex;
void* thr ead(void* ar g)
{
//wait sem_ wait(&mutex);
pr intf("\nEnter ed..\n");
//cr itical section
sleep(4);
//signal
pr intf("\nJust Exiting...\n");
sem_ post(&mutex);
}
int main()
{
sem_ init(&mutex, 0, 1);
pthr ead_ t t1,t2;
pthr ead_ cr eate(&t1,NULL,thr ead,NULL);
sleep(2);
pthr ead_ cr eate(&t2,NULL,thr ead,NULL);
pthr ead_ join(t1,NULL);
pthr ead_ join(t2,NULL);
sem_ destr oy(&mutex);
r etur n 0;
}
Output:
Enter ed..
Enter ed..
Just Exiting...
Just Exiting...
#include <stdio.h>
#include<conio.h>
void 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;
pr intf("enter the number of pr ocesses in a system");
scanf("%d", &np);
pr intf("enter the number of r esour ces in a system");
scanf("%d",&nr );
for (i=1; i<=nr ; i++)
{
pr intf("Enter no. of instances of r esour ce R%d " ,i);
scanf("%d", &r [ 0] [ i] );
av[ 0] [ i] = r [ 0] [ i] ;
}
pr intf("\nAvailability ");
for (i=1; i<=nr ; i++)
pr intf("R%d %d\t", i, av[ 0] [ i] );
pr intf("\n ");
pr intf("\n safe sequence");
pocess P1
allocated 3 maximum 4 need 1
allocated 2 maximum 4 need 2
allocated 1 maximum 4 need 3
pocess P2
allocated 1 maximum 3 need 2
allocated 1 maximum 4 need 3
allocated 2 maximum 5 need 3
pocess P3
allocated 4 maximum 5 need 1
allocated 1 maximum 2 need 1
allocated 2 maximum 4 need 2
Availability R1 2 R2 3 R3 2
safe sequence
P3 Availability R1 6 R2 4 R3 4
P1 Availability R1 9 R2 6 R3 5
P2 Availability R1 10 R2 7 R3 7
Pr ogr am:
#include <stdio.h>
#include <stdbool.h>
int main() {
int m, n;
pr intf("Enter the number of pr ocesses: ");
scanf("%d", &m);
// Initialize ar r ays
initialize(m, n);
// Detect deadlock
if (detectDeadlock(m, n))
pr intf("No deadlock detected.\n");
else
pr intf("Deadlock detected.\n");
r etur n 0;
}
Output:
Pr ogr am
#include <stdio.h>
#include <str ing.h>
#include <pthr ead.h>
#include <stdlib.h>
#include <unistd.h>
pthr ead_ t tid[ 2] ; int counter ;
pthr ead_ mutex_ t lock;
void* tr ythis(void *ar g)
{
pthr ead_ mutex_ lock(&lock);
unsigned long i = 0; counter += 1;
pr intf("\n Job %d has star ted\n", counter );
for (i=0; i<(0xFFFFFFFF);i++);
pr intf("\n Job %d has finished\n", counter );
pthr ead_ mutex_ unlock(&lock);
r etur n NULL;
}
void main()
{
int i = 0; int er r or ;
if (pthr ead_ mutex_ init(&lock, NULL) != 0)
{
pr intf("\n mutex init has failed\n");
}
while(i < 2)
{
er r or = pthr ead_ cr eate(&(tid[ i] ), NULL,
&tr ythis, NULL);
if (er r or != 0)
pr intf("\nThr ead can't be cr eated :[ %s] ",
str er r or (er r or ));
i++;
}
pthr ead_ join(tid[ 0] , NULL);
pthr ead_ join(tid[ 1] , NULL);
pthr ead_ mutex_ destr oy(&lock);
}
Output
Pr ogr am
#include <stdio.h>
#include <stdlib.h>
#define PAGE_ SIZE 1024
#define FRAME_ SIZE 1024
#define MEMORY_ SIZE 8192
#define PAGE_ TABLE_ SIZE
(MEMORY_ SIZE / FRAME_ SIZE)
// Str uctur e to r epr esent a page table entr y
typedef str uct {
int fr ame_ number ;
int valid;
} PageTableEntr y;
// Function to initialize page table with invalid
entr ies
void initialize_ page_ table(PageTableEntr y
*page_ table, int size) {
for (int i = 0; i < size; i++) {
page_ table[ i] .valid = 0;
}
}
// Function to simulate page lookup and page
fault handling
void page_ lookup(PageTableEntr y *page_ table,
int page_ number ) {
if (page_ table[ page_ number ] .valid) {
pr intf("Page %d is alr eady in memor y.
Fr ame number : %d\n", page_ number ,
Pr ogr am
#include <stdio.h>
#define MAX_ HOLES 10
#define MAX_ PROCESSES 10
intch;
str uct Hole {
int size;
};
void fir stFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
pr intf("Fir st fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}
}
void wor stFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
str uct Hole c;
pr intf("Wor st fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti=0;i<numHoles- 1;i++)
for (int j=i+1;j<numHoles;j++)
if(holes[ i] .size<holes[ j] .size)
{
c=holes[ i] ;
holes[ i] =holes[ j] ;
holes[ j] =c;
}
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}}
void bestFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
str uct Hole c;
pr intf("Best fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti=0;i<numHoles- 1;i++)
for (int j=i+1;j<numHoles;j++)
if(holes[ i] .size>holes[ j] .size)
{
c=holes[ i] ;
holes[ i] =holes[ j] ;
holes[ j] =c;
}
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}
}
intmain() {
intnumHoles, numPr ocesses;
pr intf("Enter the number of Holes : ");
scanf("%d", &numHoles);
str uct Hole holes[ MAX_ HOLES] ;
for (inti = 0; i<numHoles; i++) {
pr intf("Enter size for hole H%d : ", i);
scanf("%d", &holes[ i] .size);
}
Pr ogr am
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX_ FRAMES 10
int fifo(int pages[ ] , int n, int capacity) {
int fr ame[ MAX_ FRAMES] , fr ameCount = 0,
pageFaults = 0, i, j, flag;
if(flag == 0) {
if(fr ameCount < capacity) {
fr ame[ fr ameCount++] = pages[ i] ;
}
else {
for (j = 0; j < capacity - 1; j++) {
fr ame[ j] = fr ame[ j + 1] ;
}
fr ame[ j] = pages[ i] ;
}
pageFaults++;
}
}
r etur n pageFaults;
}
far thest = 0;
for (j = 1; j < capacity; j++) {
if(temp[ j] > temp[ far thest] )
far thest = j;
}
r etur n pageFaults;
}
int main() {
int pages[ ] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3,
2} ;
int n = sizeof(pages) / sizeof(pages[ 0] );
int capacity = 4;
pr intf("FIFO Page Replacement Algor ithm\n");
pr intf("Number of Page Faults: %d\n",
fifo(pages, n, capacity));
r etur n 0;
}
Output
FIFO Page Replacement Algor ithm
Number of Page Faults: 7
Pr ogr am
#include <stdio.h>
#include <stdlib.h>
#include <str ing.h>
void displayFiles() {
pr intf("Files in the dir ector y:\n");
for (int i = 0; i < totalFiles; i++) {
pr intf("%s\n", files[ i] .name);
}
}
int main() {
cr eateFile("file1.txt");
cr eateFile("file2.txt");
cr eateFile("file3.txt");
displayFiles();
r etur n 0;
}
Output
Pr ogr am
#include <stdio.h>
#include <stdlib.h>
#include <str ing.h>
int main() {
cr eateUser Dir ector y("user 1");
cr eateUser Dir ector y("user 2");
cr eateFileInDir ector y("user 1", "file1.txt");
cr eateFileInDir ector y("user 1", "file2.txt");
cr eateFileInDir ector y("user 2", "file3.txt");
displayFilesInDir ector y("user 1");
displayFilesInDir ector y("user 2");
r etur n 0;
}
Output
User dir ector y 'user 1' cr eated successfully.
User dir ector y 'user 2' cr eated successfully.
File 'file1.txt' cr eated successfully in dir ector y
'user 1'.
File 'file2.txt' cr eated successfully in dir ector y
'user 1'.
File 'file3.txt' cr eated successfully in dir ector y
'user 2'.
Files in dir ector y 'user 1':
file1.txt
file2.txt
Files in dir ector y 'user 2':
file3.txt
Ex. No. 14 File Allocation methods
Pr ogr am
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
void pr intDisk() {
for (int i = 0; i < DISK_ SIZE; i++) {
pr intf("%d", disk[ i] );
}
pr intf("\n");
}
if (file- >indexBlock == - 1) {
fr ee(file);
r etur n NULL;
}
int allocated = 0;
int fileBlockIndex = 0;
for (int i = 0; i < DISK_ SIZE && allocated <
fileSize; i++) {
if (!disk[ i] ) {
file- >fileBlocks[ fileBlockIndex++] = i;
disk[ i] = tr ue;
allocated++;
}
}
r etur n file;
}
fr ee(file);
}
int pr eviousBlock = - 1;
if (pr eviousBlock == - 1) {
fr ee(file);
r etur n NULL;
}
r etur n file;
}
while (block != - 1) {
int nextBlock = diskBlocks[ block] .nextBlock;
disk[ block] = false;
block = nextBlock;
}
fr ee(file);
}
int main() {
int choice, star t, length, fileSize;
IndexedFile* indexedFile = NULL;
LinkedFile* linkedFile = NULL;
while (1) {
pr intf("Choose file allocation str ategy:\n");
pr intf("1. Sequential\n");
pr intf("2. Indexed\n");
pr intf("3. Linked\n");
pr intf("4. Exit\n");
pr intf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
pr intf("Enter star t block and length: ");
scanf("%d %d", &star t, &length);
if (allocateSequential(star t, length)) {
pr intf("Sequential file allocated fr om
block %d to %d.\n", star t, star t + length - 1);
} else {
pr intf("Failed to allocate sequential
file.\n");
}
pr intDisk();
pr intf("Deallocating sequential file...\n");
deallocateSequential(star t, length);
pr intDisk();
br eak;
case 2:
pr intf("Enter file size: ");
scanf("%d", &fileSize);
indexedFile = cr eateIndexedFile(fileSize);
if (indexedFile) {
pr intf("Indexed file allocated with
index block at %d.\n", indexedFile- >indexBlock);
} else {
pr intf("Failed to allocate indexed file.
\n");
}
pr intDisk();
if (indexedFile) {
pr intf("Deallocating indexed file...\n");
deallocateIndexedFile(indexedFile,
fileSize);
}
pr intDisk();
br eak;
case 3:
pr intf("Enter file size: ");
scanf("%d", &fileSize);
linkedFile = cr eateLinkedFile(fileSize);
if (linkedFile) {
pr intf("Linked file allocated star ting
at block %d.\n", linkedFile- >star tBlock);
} else {
pr intf("Failed to allocate linked file.
\n");
}
pr intDisk();
if (linkedFile) {
pr intf("Deallocating linked file...\n");
deallocateLinkedFile(linkedFile);
}
pr intDisk();
br eak;
case 4:
exit(0);
br eak;
default:
pr intf("Invalid choice. Tr y again.\n");
}
}
r etur n 0;
}
Output
Pr ogr am
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,r eq[ 50] ,mov=0,cp;
pr intf("enter the cur r ent position\n");
scanf("%d",&cp);
pr intf("enter the number of r equests\n");
scanf("%d",&n);
pr intf("enter the r equest or der \n");
for (i=0;i<n;i++)
{
scanf("%d",&r eq[ i] );
}
mov=mov+abs(cp- r eq[ 0] ); // abs is used to
calculate the absolute value
pr intf("%d - > %d",cp,r eq[ 0] );
for (i=1;i<n;i++)
{
mov=mov+abs(r eq[ i] - r eq[ i- 1] );
pr intf(" - > %d",r eq[ i] );
}
pr intf("\n");
pr intf("total head movement = %d\n",mov);
}
Output
enter the cur r ent position
40
enter the number of r equests
5
enter the r equest or der
23
51
48
75
82
40 - > 23 - > 51 - > 48 - > 75 - > 82
total head movement = 82
Pr ogr am
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int
i,n,k,r eq[ 50] ,mov=0,cp,index[ 50] ,min,a[ 50] ,j=0,
mini,cp1;
pr intf("enter the cur r ent position\n");
scanf("%d",&cp);
pr intf("enter the number of r equests\n");
scanf("%d",&n);
cp1=cp;
pr intf("enter the r equest or der \n");
for (i=0;i<n;i++)
{
scanf("%d",&r eq[ i] );
}
for (k=0;k<n;k++)
{
for (i=0;i<n;i++)
{
index[ i] =abs(cp- r eq[ i] ); // calculate distance
of each r equest fr om cur r ent position
}
// to find the near est r equest
min=index[ 0] ;
mini=0;
for (i=1;i<n;i++)
{
if(min>index[ i] )
{
min=index[ i] ;
mini=i;
}
}
a[ j] =r eq[ mini] ;
j++;
cp=r eq[ mini] ; // change the cur r ent position
value to next r equest
r eq[ mini] =999;
} // the r equest that is pr ocessed its value is
changed so that it is not pr ocessed again
pr intf("Sequence is : ");
pr intf("%d",cp1);
mov=mov+abs(cp1- a[ 0] ); // head movement
pr intf(" - > %d",a[ 0] );
for (i=1;i<n;i++)
{
mov=mov+abs(a[ i] - a[ i- 1] ); ///head
movement
pr intf(" - > %d",a[ i] );
}
pr intf("\n");
pr intf("total head movement = %d\n",mov);
}
Output
enter the cur r ent position
40
enter the number of r equests
5
enter the r equest or der
50
30
70
60
20
Sequence is : 40 - > 50 - > 60 - > 70 - > 30 - > 20
total head movement = 80
9. Install Linux
● The vir tual machine will boot fr om the ISO file.
● Follow the on- scr een instr uctions to install the Linux distr ibution. This typically
involves:
● Selecting the installation language.
● Choosing the installation type (e.g., "Er ase disk and install Ubuntu").
● Setting up user infor mation (user name, passwor d).
10. Complete Installation
● After the installation pr ocess completes, the vir tual machine will pr ompt to
r estar t.
● Once r estar ted, need to r emove the ISO fr om the vir tual CD/DVD dr ive to pr event
it fr om booting fr om the ISO again.
11. Install VMwar e Tools
● After the OS is installed, it’s r ecommended to install VMwar e Tools for better
per for mance and additional featur es.
● In the VM window, click VM > Install VMwar e Tools.
● Follow the pr ompts within the Linux VM to install the tools. This often involves
mounting the VMwar e Tools ISO and r unning the installer fr om a ter minal.
Pr ogr am
#include <pthr ead.h>
#include <semaphor e.h>
#include <stdio.h>
#include <unistd.h>
while (1) {
// Thinking
pr intf("Philosopher %d is thinking.\n", id);
sleep(1);
// Hungr y
pr intf("Philosopher %d is hungr y.\n", id);
// Eating
pr intf("Philosopher %d is eating.\n", id);
sleep(1);
int main() {
pthr ead_ t philosopher s[ NUM_ PHILOSOPHERS] ;
int ids[ NUM_ PHILOSOPHERS] ;
// Initialize semaphor es
for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
sem_ init(&for ks[ i] , 0, 1);
}
// Destr oy semaphor es
for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
sem_ destr oy(&for ks[ i] );
}
r etur n 0;
}
Output
Philosopher 0 is thinking.
Philosopher 1 is thinking.
Philosopher 3 is thinking.
Philosopher 2 is thinking.
Philosopher 4 is thinking.
Philosopher 3 is hungr y.
Philosopher 3 picked up left for k 3.
Philosopher 3 picked up r ight for k 4.
Philosopher 3 is eating.
Philosopher 0 is hungr y.
Philosopher 0 picked up left for k 0.
Philosopher 0 picked up r ight for k 1.
….