0% found this document useful (0 votes)
30 views23 pages

Sohan Mandal OS LAB

The document provides a comprehensive guide on basic Linux commands, including file management, date and time commands, and the vi command editor. It also includes C programming examples for various CPU scheduling algorithms such as FCFS, SJF, Priority, and Round Robin, along with page replacement algorithms like FIFO. Each section contains command descriptions, usage, and sample code with expected outputs.

Uploaded by

dm694844
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)
30 views23 pages

Sohan Mandal OS LAB

The document provides a comprehensive guide on basic Linux commands, including file management, date and time commands, and the vi command editor. It also includes C programming examples for various CPU scheduling algorithms such as FCFS, SJF, Priority, and Round Robin, along with page replacement algorithms like FIFO. Each section contains command descriptions, usage, and sample code with expected outputs.

Uploaded by

dm694844
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/ 23

INDEX

Sl Assignment no. Page Remarks


no.
01 Module 1 01-03

02 Module 2 04-08

03 Module 3 09-17

04 Module 4 18-22

0|Page
REG-D222307686
BASIC LINUX COMMANDS

Sl
No. Command Uses Output

Print current working


1. pwd
directory

2. mkdir <filename> Create a directory

Change current
3. cd <dir path> working directory to
specified path

List files & folders of a


4. ls
directory

print or set the system


5. date
date and time

prints the current


6. date +%m month in two-digit
format

print the current


7. date +%M
minute

print the hour in 24-


8. date +%H
hour format.

1|Page
REG-D222307686
Sl
No. Command Uses Output

Current month’s
9. cal
calendar.

Displays the entire


10. cal 2025 calendar for the year
2025.

Creates a new file &


11. cat > <filename> lets us type input
directly into it.

Displays the contents


12. cat <filename> of the specified file in
the terminal.

Copies file1 into


13. cp file1 D2/file2 directory D2/ with a
new name file2.
Moves & renames
14. mv file1 D5/file2 file1 to file2 inside
directory D5/.

2|Page
REG-D222307686
Sl
No. Command Uses Output

Lists files and directories


in the current directory in
long format.
Output includes file
15. ls -l permissions, Number of
hard links, owner, group,
file size in bytes, Name of
file or dir.

Shows a numbered list


of previously run
16. history
commands in current
shell session.

Gives the user (owner)


execute permission on
the file.
chmod u+x
17. • u -> user
<filename>
• +x -> add
executable
permission
Adds execute (+x)
chmod ugo+x permission for user
18.
<filename> (u), group (g), and
others (o) on the file.

Give full read, write,


and execute
permissions to user,
group, and others.

Each number is a sum


of permissions:
chmod 777
18.
<filename> • 4 → read (r)
• 2 → write (w)
• 1 → execute
(x)

So, 7 = 4 + 2 + 1
gives read, write, and
execute permissions.

3|Page
REG-D222307686
VI COMMAND EDITOR

SL COMMAND USE OUTPUT


NO. NAME
1. vi filename To create
and open a
new file
(ex- vi
hello.txt)

2. i Insert mode
(it give the
access to
type text in
vi terminal)

3. esc Exit insert There is no specific output or massage for the command
mode
To command
mode.it just shows blank and users can opearate many
mode commands.

4. :w To save the
file in vi
terminal

5. : ( colon) Enter
command
line mode

4|Page
REG-D222307686
6. :q To quit the
editor to
linux Output-
terminal

7. : wq Save and quit


the editor.
Output:-

8. : q! Quit the
editor
without
saving .
Now these are command in below after press ”esc” to enter the command mode.
9. x Delete a
single
character of
a line .
After press esc x

10. dd Delete the


entire
current line

After press dd

11. yy To copy the


entire line

There is no immidiate ouput after yy


It will paste the copied line after p
12. p Paste the
copied line in
vi editor

after press yy and then p

5|Page
REG-D222307686
13. u Undo the last
action
After press u

`14. G Go to the
end of the
file After press G

15. gg To go to the
top of the vi
file

After press gg

16. h Move cursor


left

After press h continously

6|Page
REG-D222307686
17. l Move the
cursor right

After press l

18. j Move the


cursor down

After press j

19. k Move the


cursor up

after press k:-


20. r Replaces the
character
under the
cursor with
<char>.

After press r t :-

21. R replace
mode,
where
every
character
you type
overwrites
existing
text.

7|Page
REG-D222307686
22. s Deletes the
character
under the
cursor and
enters insert
mode.
after press s:-

23. S Deletes the


entire line
and enters
insert mode.
AFTER PRESS S THE LINE WILL BE EARSED

22. gcc Gnu compiler


filename.c collection
It helps to
compile a c
program file
in vi editor

23. ./a.out Run the


compiled c
program
Here a is the
ststic library
of c

8|Page
REG-D222307686
CPU SCHEDULING ALGORITHMS

Q. Write a c program for FCFS scheduling algorithm, CPU burst, arrival time (if needed) must be
given by user.
CODE: -
#include <stdio.h>

int main() {

int n, i;

int AT[10], BT[10], CT[10], TAT[10], WT[10];

printf("\nEnter number of processes: ");

scanf("%d", &n);

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

printf("Process %d Arrival Time: ", i + 1);

scanf("%d", &AT[i]);

printf(" Burst Time: ");

scanf("%d", &BT[i]);

printf("\n");

CT[0] = AT[0] + BT[0];

TAT[0] = CT[0] - AT[0];

WT[0] = TAT[0] - BT[0];

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

if (CT[i-1] < AT[i])

CT[i] = AT[i] + BT[i];

else

CT[i] = CT[i-1] + BT[i];

TAT[i] = CT[i] - AT[i];

WT[i] = TAT[i] - BT[i];

printf("Waiting Times:\n");

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

printf(" Process %d: %d\n", i + 1, WT[i]);

9|Page
REG-D222307686
}

return 0;

OUTPUT: -

Q. Write a c program for SJF scheduling algorithm, CPU burst, arrival time (if needed) must be
given by user.
CODE: -
#include <stdio.h>

int main() {

int n, i, time = 0, completed = 0;

int AT[10], BT[10], CT[10], TAT[10], WT[10];

int done[10] = {0};

printf("\nEnter number of processes: ");

scanf("%d", &n);

printf("\n");

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

10 | P a g e
REG-D222307686
printf("Process %d Arrival Time: ", i + 1);

scanf("%d", &AT[i]);

printf(" Burst Time: ");

scanf("%d", &BT[i]);

while (completed < n) {

int idx = -1;

int minBT = 9999;

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

if (done[i] == 0 && AT[i] <= time && BT[i] < minBT) {

minBT = BT[i];

idx = i;

}}

if (idx != -1) {

time += BT[idx];

CT[idx] = time;

TAT[idx] = CT[idx] - AT[idx];

WT[idx] = TAT[idx] - BT[idx];

done[idx] = 1;

completed++;

} else {

time++;

}}

printf("Waiting Times:\n");

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

printf(" Process %d: %d\n", i + 1, WT[i]);

return 0;

OUTPUT: -

11 | P a g e
REG-D222307686
Q. Write a c program for Priority scheduling algorithm, CPU burst, arrival time (if needed) must
be given by user.

Non-Preemptive
CODE: -
#include <stdio.h>

int main() {

int n, i, j, time = 0;

int AT[10], BT[10], CT[10], TAT[10], WT[10], priority[10];

int done[10] = {0};

printf("\nEnter number of processes: ");

scanf("%d", &n);

printf("\n");

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

printf("Process %d Arrival Time: ", i + 1);

scanf("%d", &AT[i]);

printf(" Burst Time: ");

scanf("%d", &BT[i]);

printf(" Priority: ");

scanf("%d", &priority[i]);

printf("\n");

12 | P a g e
REG-D222307686
}

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

for (j = i + 1; j < n; j++) {

if (priority[i] > priority[j] || (priority[i] == priority[j] && AT[i] > AT[j])) {

int temp = AT[i];

AT[i] = AT[j];

AT[j] = temp;

temp = BT[i];

BT[i] = BT[j];

BT[j] = temp;

temp = priority[i];

priority[i] = priority[j];

priority[j] = temp;

}}}

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

if (i == 0 || AT[i] > time) {

time = AT[i];

CT[i] = time + BT[i];

TAT[i] = CT[i] - AT[i];

WT[i] = TAT[i] - BT[i];

time = CT[i];

printf("Waiting Times:\n");

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

printf(" Process %d: %d\n", i + 1, WT[i]);

return 0;

OUTPUT: -

13 | P a g e
REG-D222307686
Preemptive
CODE: -
#include <stdio.h>

int main() {

int n, i, time = 0, completed = 0;

int AT[10], BT[10], CT[10], TAT[10], WT[10], priority[10];

int remainingBT[10], done[10] = {0};

printf("\nEnter number of processes: ");

scanf("%d", &n);

printf("\n");

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

printf("Process %d Arrival Time: ", i + 1);

scanf("%d", &AT[i]);

printf(" Burst Time: ");

scanf("%d", &BT[i]);

printf(" Priority: ");

scanf("%d", &priority[i]);

remainingBT[i] = BT[i];

printf("\n");

while (completed < n) {

14 | P a g e
REG-D222307686
int idx = -1;

int minPriority = 9999;

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

if (done[i] == 0 && AT[i] <= time && priority[i] < minPriority) {

minPriority = priority[i];

idx = i;

if (idx != -1) {

remainingBT[idx]--;

if (remainingBT[idx] == 0) {

CT[idx] = time + 1;

TAT[idx] = CT[idx] - AT[idx];

WT[idx] = TAT[idx] - BT[idx];

done[idx] = 1;

completed++;

time++;

} else {

time++;

}}

printf("Waiting Times:\n");

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

printf(" Process %d: %d\n", i + 1, WT[i]);

return 0;

OUTPUT: -

15 | P a g e
REG-D222307686
Q. Write a c program for Round Robin scheduling algorithm, CPU burst, arrival time (if needed)
must be given by user.
CODE: -
#include <stdio.h>

int main() {

int n, i, j, tq;

int AT[10], BT[10], RT[10], WT[10] = {0};

int time = 0, done = 0;

printf("\nEnter number of processes: ");

scanf("%d", &n);

printf("Enter Time Quantum: ");

scanf("%d", &tq);

printf("\n");

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

printf("Process %d Arrival Time: ", i + 1);

scanf("%d", &AT[i]);

printf(" Burst Time: ");

scanf("%d", &BT[i]);

RT[i] = BT[i];

printf("\n");

}
16 | P a g e
REG-D222307686
while (done < n) {

int progress = 0;

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

if (RT[i] > 0 && AT[i] <= time) {

progress = 1;

int exec = (RT[i] > tq) ? tq : RT[i];

time += exec;

RT[i] -= exec;

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

if (j != i && RT[j] > 0 && AT[j] <= time)

WT[j] += exec;

if (RT[i] == 0)

done++;

}}

if (!progress)

time++;

printf("Waiting Times:\n");

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

printf(" Process %d: %d\n", i + 1, WT[i]);

printf("\n");

return 0;

OUTPUT: -

17 | P a g e
REG-D222307686
PAGE REPLACEMENT ALGORITHMS

Q. Write a C program for FIFO page replacement algorithm and the needed inputs will be given by users
and calculate the total Page Fault.

CODE: -
#include <stdio.h>

int main() {

int i, j, k, n, frameCount, pageFaults = 0;

int pages[100], frames[10], front = 0;

printf("\nENTER THE NUMBER OF PAGES: ");

scanf("%d", &n);

printf("ENTER THE PAGE NUMBER : ");

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

scanf("%d", &pages[i]);

printf("ENTER THE NUMBER OF FRAMES :");

scanf("%d", &frameCount);

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

frames[i] = -1;

printf("\nref string page frames\n");

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

int found = 0;

for (j = 0; j < frameCount; j++) {

if (frames[j] == pages[i]) {

found = 1;

break;

}}

if (!found) {

frames[front] = pages[i];

front = (front + 1) % frameCount;

18 | P a g e
REG-D222307686
pageFaults++;

printf("%-17d", pages[i]);

for (k = 0; k < frameCount; k++) {

if (frames[k] == -1) {

printf(" -1");

} else {

printf("%4d", frames[k]);

printf("\n");

} else {

printf("%d\n", pages[i]);

}}

printf("\nPage Fault Is %d\n", pageFaults);

return 0;

OUTPUT: -

Q. Write a C program for LRU page replacement algorithm and the needed inputs will be given by users
and calculate the total Page Fault.

CODE: -
#include <stdio.h>

19 | P a g e
REG-D222307686
int main() {

int pages[100], frames[10], time[10];

int i, j, k, n, frameCount, pageFaults = 0, counter = 0;

printf("\nENTER THE NUMBER OF PAGES: ");

scanf("%d", &n);

printf("ENTER THE PAGE REFERENCE STRING: ");

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

scanf("%d", &pages[i]);

printf("ENTER THE NUMBER OF FRAMES: ");

scanf("%d", &frameCount);

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

frames[i] = -1;

time[i] = 0;

printf("\nRef | Frame State\t\t(Page Fault?)\n");

printf("-----|-------------------------------\n");

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

int flag1 = 0, flag2 = 0;

for (j = 0; j < frameCount; j++) {

if (frames[j] == pages[i]) {

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

}}

if (flag1 == 0) {

for (j = 0; j < frameCount; j++) {

if (frames[j] == -1) {

counter++;

frames[j] = pages[i];

time[j] = counter;

pageFaults++;

flag2 = 1;

break;

}}}

20 | P a g e
REG-D222307686
if (flag2 == 0) {

int min = time[0], pos = 0;

for (j = 1; j < frameCount; j++) {

if (time[j] < min) {

min = time[j];

pos = j;

}}

counter++;

frames[pos] = pages[i];

time[pos] = counter;

pageFaults++;

printf(" %-4d|", pages[i]);

for (k = 0; k < frameCount; k++) {

if (frames[k] == -1)

printf(" - ");

else

printf(" %2d ", frames[k]);

if (flag1 == 1)

printf("\t\t(Hit)\n");

else

printf("\t\t(Fault)\n");

printf("\nTOTAL PAGE FAULTS = %d\n", pageFaults);

return 0;

OUTPUT: -

21 | P a g e
REG-D222307686
22 | P a g e
REG-D222307686

You might also like