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

Amity Os File

The document provides details about implementing the First Come First Serve (FCFS) CPU scheduling algorithm in C programming language. It includes: 1) Defining a Process structure to store details of each process like ID, burst time, arrival time etc. 2) Functions to take input, calculate waiting time, turnaround time, completion time, and sort processes based on arrival time. 3) The main() function calls these functions, calculates average waiting and turnaround time, and displays the results. 4) The code implements FCFS by scheduling processes in the order of their arrival time and maintaining a queue.

Uploaded by

Akhil Suryan
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)
94 views23 pages

Amity Os File

The document provides details about implementing the First Come First Serve (FCFS) CPU scheduling algorithm in C programming language. It includes: 1) Defining a Process structure to store details of each process like ID, burst time, arrival time etc. 2) Functions to take input, calculate waiting time, turnaround time, completion time, and sort processes based on arrival time. 3) The main() function calls these functions, calculates average waiting and turnaround time, and displays the results. 4) The code implements FCFS by scheduling processes in the order of their arrival time and maintaining a queue.

Uploaded by

Akhil Suryan
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

COMPUTER SCIENCE & ENGINEERING

AMITY SCHOOL OF ENGINEERING & TECHNOLOGY

AMITY UNIVERSITY, NOIDA, UTTAR PRADESH

SESSION:2023-2024

Practical File

On

Operating System

Submitted to: Submitted by:

Dr Rani Akhil Suryan

Computer Science & Engineering A2305222737

B.Tech 4CSE10X
Lab Practical 1

Aim: Use of Basic Linux/ Unix Shell Commands

Commands:

1. pwd: This command is used to display the location of the current working
directory, in which we are operating, i.e. in which the terminal is open, and is
useful when directory changes are made frequently.
Syntax of the command: $ pwd

2. ls: This command is used to list down all the directories and files inside the
present working directory. If the path is removed, ls command shows the
current working directory’s content.
Syntax of the command: $ ls

3. mkdir :The mkdir command is used to create a new directory under any
directory.

Syntax: mkdir <directory name>

4. rmdir :The rmdir command is used to delete a directory.

Syntax: rmdir <directory name>


5. cd: This command is used to change the current directory and it navigates
the Linus files and directories.
Syntax of the command: $ cd Desktop

6. touch: This command can be used to create a new file. We can create
multiple empty files by executing it once, and can be used to generate and
modify timestamp of any file.
Syntax of the command: $ touch file1.txt

7. cat: This command is a multi-purpose utility in the Linux system. It can


be used to create a file, display content of the file, copy the content of one
file to another file, and more.
Syntax of the command: $ cat file1.txt

8. rm: This command removes specific files from the directory.


Syntax of the command: $ rm test.txt

9. cp: This command copies any file or folder to any directory.


Syntax of the command: $ cp file1.txt directory1
10.mv: This command is used move or rename files and directories inside
a specific directory. The command given below moves the “file2.txt” to
“directory1”.
Syntax of the command: $ mv file2.txt directory1

11.head: This command prints the first ten lines of a text file and displays
the content of a file. The command given below helps to print the first ten
lines of the “file1.txt” file.
Syntax of the command: $ head file1.txt
12. tail: This command prints the last ten lines of a text file and is useful for
reading the error message. The command given below helps to print the
ten lines from the bottom of “file1.txt” file.
Syntax of the command: $ tail file1.txt

13. grep: This is a useful command and is used to search for matching
patterns in a file. This command prints all lines containing the matching
strings. The command given below prints all the lines that contain “20”
from “file1.txt”.
Syntax of the command: $ cat file1.txt | grep 20

14. sed: This command is also known as stream editor. It is used to edit files
using a regular expression. This lets us to find, replace, and delete
patterns in a file without using a text editor.
Syntax of the command: $ sed
15. tac: This displays the file content in the reverse order. This prints the last
line first, then the second last and so on.
Syntax of the command: $ tac

16. more: This command is used to view the text files in the command
prompt, displaying one screen at a time in case the file is large. This
allows the user to scroll up and down through the page.
Syntax of the command: $ more

17. less: This command is used to read the contents of a file one page(one
screen) at a time.
Syntax of the command: $ less
18. id: This command is used to find out the user and group names, and,
numeric IDs of the current user or any other user in the server. This
command is mainly used for user management in a server.
Syntax of the command: $ id
19. cut: This command retrieves sections from a file and prints the result
as Terminal outputs. This is used to remove sections from each line of
files and print specific parts of lines from each file to standard output.
This is used to select a specific column of a file.
Syntax of the command: $ cut

20. sort: This command is used to sort the contents of a file in numeric or
alphabetical order, and print the results to standard output(usually the
terminal screen).
Syntax of the command: $ sort

21. find: This command is used to find a particular file within a directory. It
is used to find the list of files for the various conditions like permission,
user ownership, modification, date/time, size, and more.
Syntax of the command: $ find
22. ps: This command is used to view currently running processes on the
system and to display some process information.
Syntax of the command: $ ps

23. od :The od command is used to display the content of a file in different


s, such as hexadecimal, octal, and ASCII characters.

Syntax:

1. od -b <fileName> // Octal format


2. od -t x1 <fileName> // Hexa decimal format
3. od -c <fileName> // ASCII character format
24. clear: Linux clear command is used to clear the terminal screen.

Syntax: clear
25. cal:The cal command is used to display the current month's calendar with the
current date highlighted.

Syntax: cal
Lab Practical 2

Aim: Use of Input/Output redirection commands

Commands:

1.bg Command : bg is a process control command that resumes suspended


process while keeping them running in the background.
Syntax: bg[job] Command: bg%

2.Fg Command : Fg command moves a background job in the current shell


environment into the foreground.
Syntax: fg [ %job]
Command: $ fg

3.Jobs Command : Jobs command is used to list the jobs that you are running
in the background and in the foreground.
Syntax: jobs [JOB]
Command: $ jobs

4.Top command: It displays information about CPU usage, memory usage,


running processes, and more, allowing users to monitor and analyze system
activity effectively.
Syntax: top

5.Ps command: The `ps` command, which stands for “process status,” is like a
computer tool that helps you see what’s happening inside your Linux computer.
The ps command provides a snapshot of the current processes on your system.
Syntax: ps [options]

6.Kill command: Kill command in Linux is a built-in command which is used


to terminate processes manually. Kill command sends a signal to a process that
terminates the process.
Syntax: kill [signal] PID

7.Watch command: Watch command in Linux is used to execute a program


periodically, showing output in full-screen. This command will run the specified
command in the argument repeatedly by showing its output and errors.
Syntax: watch [options] command
8.Overwrite Redirection: Overwrite redirection is useful when you want to
store/save the output of a command to a file and replace all the existing content
of that file.
Output redirection: cat > file.txt
Input redirection: cat < file.txt

9.Append Redirection: With the help of this Redirection, you can append the
output to the file without compromising the existing data of the file.
“>>” standard output
“<<” standard input

10.Pipe Operator: The pipe operator is the | character (typically located above
the enter key). This operator serves to join the standard output stream from one
process to the standard input stream of another process
Lab Practical 3

Aim: Write a program to implement First come First serve CPU scheduling
algorithm

Language used :C

Code:
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a process


struct Process {
int id, bt, at, ct, tat, wt;
};

// Function prototypes
void input(struct Process*, int);
void calc(struct Process*, int);
void show(struct Process*, int);
void sort(struct Process*, int);

int main() {
int n;
printf("\nEnter the number of processes in your system:\n");
scanf("%d", &n);

struct Process *p = (struct Process*)malloc(n * sizeof(struct Process));


struct Process f;

input(p, n);
sort(p, n);
calc(p, n);
show(p, n);

// Calculate and display average waiting time and average turnaround time
float avgWT = 0, avgTAT = 0;
for (int i = 0; i < n; i++) {
avgWT += p[i].wt;
avgTAT += p[i].tat;
}
avgWT /= n;
avgTAT /= n;

printf("\nAverage Waiting Time: %.2f\n", avgWT);


printf("Average Turnaround Time: %.2f\n", avgTAT);
free(p);
return 0;
}

// Taking input arrival and burst times for all processes


void input(struct Process *p, int n) {
for (int i = 0; i < n; i++) {
printf("\nEnter arrival time for process %d:", i + 1);
scanf("%d", &p[i].at);
printf("\nEnter burst time for process %d:", i + 1);
scanf("%d", &p[i].bt);
p[i].id = i + 1;
}
}

// Calculating waiting, turn-around, and completion time


void calc(struct Process *p, int n) {
int sum = 0;
sum = sum + p[0].at;

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


sum = sum + p[i].bt;
p[i].ct = sum;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;

if (i < n - 1 && sum < p[i + 1].at) {


int t = p[i + 1].at - sum;
sum = sum + t;
}
}
}

// Sorting processes with respect to arrival times


void sort(struct Process *p, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].at > p[j + 1].at) {
// Sorting burst times
int temp = p[j].bt;
p[j].bt = p[j + 1].bt;
p[j + 1].bt = temp;

// Sorting arrival times


temp = p[j].at;
p[j].at = p[j + 1].at;
p[j + 1].at = temp;

// Sorting their respective IDs


temp = p[j].id;
p[j].id = p[j + 1].id;
p[j + 1].id = temp;
}
}
}
}

// Display function
void show(struct Process *p, int n) {
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time \tTurn Around Time\n");
for (int i = 0; i < n; i++) {
printf(" P[%d]\t %d\t\t%d\t\t%d\t\t %d\n", p[i].id, p[i].at, p[i].bt, p[i].wt, p[i].tat);
}
}

Output:
Lab Practical 4

Aim: Write a program to implement Shortest Job First (SJF) CPU scheduling
algorithm

Language used :C

Code:
#include <stdio.h>

int main() {
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;

printf("Enter number of processes: ");


scanf("%d", &n);
printf("Enter Burst Time:\n");

// User Input Burst Time and allotting Process Id.


for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}

// Sorting processes according to their Burst Time.


for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}

A[0][2] = 0;

// Calculation of Waiting Times


for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}

avg_wt = (float)total / n;
total = 0;

printf("P\tBT\tWT\tTAT\n");

// Calculation of Turn Around Time and printing the data in table format.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d\t%d\t%d\t%d\n", A[i][0], A[i][1], A[i][2], A[i][3]);
}

avg_tat = (float)total / n;
printf("Average Waiting Time= %f\n", avg_wt);
printf("Average Turnaround Time= %f\n", avg_tat);

return 0;
}
Output:
Lab Practical 5

Aim: Write a program to implement Priority based CPU scheduling algorithm

Language used :C

Code:
#include <stdio.h>
#include <stdlib.h>

struct Process {
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};

// Function to sort the Process acc. to priority


int comparison(const void *a, const void *b) {
return ((struct Process *)b)->priority - ((struct Process *)a)->priority;
}

// Function to find the waiting time for all processes


void findWaitingTime(struct Process proc[], int n, int wt[]) {
// waiting time for the first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n; i++)
wt[i] = proc[i - 1].bt + wt[i - 1];
}

// Function to calculate turn around time


void findTurnAroundTime(struct Process proc[], int n, int wt[], int tat[]) {
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// Function to calculate average time


void findavgTime(struct Process proc[], int n) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time for all processes


findWaitingTime(proc, n, wt);

// Function to find turn around time for all processes


findTurnAroundTime(proc, n, wt, tat);
// Display processes along with all details
printf("\nProcesses Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn


// around time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d\t\t%d\t %d\t\t %d\n", proc[i].pid, proc[i].bt, wt[i], tat[i]);
}

printf("\nAverage waiting time = %.2f", (float)total_wt / (float)n);


printf("\nAverage turn around time = %.2f", (float)total_tat / (float)n);
}

void priorityScheduling(struct Process proc[], int n) {


// Sort processes by priority
qsort(proc, n, sizeof(struct Process), comparison);

printf("Order in which processes get executed \n");


for (int i = 0; i < n; i++)
printf("%d ", proc[i].pid);

findavgTime(proc, n);
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

struct Process proc[n];

// Input process details


for (int i = 0; i < n; i++) {
printf("Enter details for process %d:\n", i + 1);
printf("Process ID: ");
scanf("%d", &proc[i].pid);
printf("Burst Time: ");
scanf("%d", &proc[i].bt);
printf("Priority: ");
scanf("%d", &proc[i].priority);
}

priorityScheduling(proc, n);
return 0;}
Output:
Lab Practical 6

Aim: Write a program to implement Round Robin based CPU scheduling


algorithm

Language used :C

Code:

#include<stdio.h>

// Structure to represent a process


struct Process {
int id; // Process ID
int burst_time; // Burst time (time required to execute)
int remaining_time; // Remaining time to execute
};

// Function to perform Round Robin scheduling


void roundRobin(struct Process processes[], int n, int quantum) {
int time = 0; // Current time
int i;

// Loop until all processes are executed


while (1) {
int done = 1; // Flag to check if all processes are done

// Traverse all processes


for (i = 0; i < n; i++) {
// If a process is not yet done
if (processes[i].remaining_time > 0) {
done = 0; // There is still a process to be executed

// Execute the process for the quantum time or the remaining time, whichever is smaller
if (processes[i].remaining_time > quantum) {
time += quantum;
processes[i].remaining_time -= quantum;
} else {
time += processes[i].remaining_time;
processes[i].remaining_time = 0;
printf("Process %d is completed at time %d\n", processes[i].id, time);
}
}
}

// If all processes are done, break from the loop


if (done == 1)
break;
}
}

int main() {
int n, quantum;

// Input number of processes and time quantum


printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process processes[n];

// Input burst time for each process


for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
}

// Input time quantum


printf("Enter the time quantum: ");
scanf("%d", &quantum);

// Perform Round Robin scheduling


roundRobin(processes, n, quantum);

return 0;
}

Output:

You might also like