0% found this document useful (0 votes)
14 views

Operating_System_Lab_BCS451

The document outlines the practical exercises for an Operating Systems lab, covering hardware and software requirements for UNIX, Linux, and Windows systems. It includes examples of system calls for process management, file management, and input/output operations, along with implementations of various CPU scheduling algorithms such as Shortest Job First, Priority Scheduling, and Multi-level Queue Scheduling. Each section provides C code examples demonstrating the concepts discussed.

Uploaded by

Milind Parashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Operating_System_Lab_BCS451

The document outlines the practical exercises for an Operating Systems lab, covering hardware and software requirements for UNIX, Linux, and Windows systems. It includes examples of system calls for process management, file management, and input/output operations, along with implementations of various CPU scheduling algorithms such as Shortest Job First, Priority Scheduling, and Multi-level Queue Scheduling. Each section provides C code examples demonstrating the concepts discussed.

Uploaded by

Milind Parashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

BCS451 - Operating System Lab Practical’s

1. Study of Hardware and Software Requirements


Objective: Understand and compare the hardware and software
requirements of various operating systems including UNIX, LINUX,
WINDOWS XP, WINDOWS 7/8.

Details:

 UNIX/Linux:

o Hardware: Compatible with most architectures (x86,


x64)

o Software: Requires bash shell, GCC compiler

UNIX Command Example: uname -a

o Syntax: uname [OPTION]

o Description: Displays system information

Input: (Command entered)

o uname -a

Output:

o Linux mypc 5.15.0-86-generic #96-Ubuntu SMP x86_64


GNU/Linux

Command Explanation:

o uname prints system information

o -a option displays all available system information like


kernel name, version, hardware name, etc.
C Program:
#include <stdio.h>

#include <unistd.h>

int main() {

char hostname[1024];

gethostname(hostname, 1024);

printf("System Hostname: %s\n", hostname);

return 0;

Input: Compiled and run using GCC

o gcc system_info.c -o system_info

o ./system_info

Output:

o System Hostname: mypc

o Windows XP/7/8:

o Hardware: Requires x86/x64 processor, minimum RAM


1 GB

o Software: Windows-specific APIs, support for .NET


framework

Windows Command Example: systeminfo

o Syntax: systeminfo
o Description: Displays detailed configuration
information about the computer and its operating
system

o Input: (Command Prompt)


o systeminfo

o Output:
o Host Name: MYPC

o OS Name: Microsoft Windows 7 Professional

o OS Version: 6.1.7601 Service Pack 1 Build 7601

o System Type: x64-based PC

o Processor(s): 1 Processor(s) Installed.

[01]: Intel64 Family 6 Model 58


Stepping 9 GenuineIntel ~2501
Mhz
2. Execute Various UNIX System Calls
i. Process Management
Objective: Learn to create and manage processes using system
calls.

System Calls Used: fork(), exec(), wait(), exit()

Code (C):
#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

printf("Child process\n");

execlp("/bin/ls", "ls", NULL);

} else {

wait(NULL);

printf("Parent process\n");

return 0;

Input:
gcc process.c -o process

./process

Output:
Child process

a.out

file1.txt

file2.c

Parent process

ii. File Management


Objective: Learn to create, open, read, write, and close files.
System Calls Used: open(), read(), write(), close()

Code (C):
#include <fcntl.h>

#include <unistd.h>

int main() {

int fd = open("file.txt", O_CREAT | O_WRONLY, 0644);

write(fd, "Hello, file!", 12);

close(fd);

return 0;
}

Input:
gcc process.c -o process

./process

Output:
Child process

a.out file1.txt hello.c notes.docx process.c

Parent process

iii. Input/Output System Calls


Objective: Handle standard input/output operations.
System Calls Used: read(), write()

Code (C):

#include <unistd.h>

int main() {

char buffer[100];

int n = read(0, buffer, 100);

write(1, buffer, n);

return 0;

Input:
Hello, this is a test.

Output:
Hello, this is a test.
3. Implement CPU Scheduling Policies
i. Shortest Job First (SJF)
Algorithm:
 Sort processes by burst time.

 Execute the shortest job first.

Code (C):
#include <stdio.h>

int main() {

int n, bt[20], p[20], wt[20], tat[20];

printf("Enter number of processes: ");

scanf("%d", &n);

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

printf("Enter Burst Time for P%d: ", i+1);

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

p[i] = i+1;

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

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

if(bt[i] > bt[j]) {

int temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;


int tempP = p[i]; p[i] = p[j]; p[j] = tempP;

wt[0] = 0;

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

for(int i = 0; i < n; i++) tat[i] = wt[i] + bt[i];

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

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

printf("P%d\t%d\t%d\t%d\n", p[i], bt[i], wt[i], tat[i]);

return 0;

Input:
Enter the number of processes: 3

Enter Burst Time for P1: 6

Enter Burst Time for P2: 2

Enter Burst Time for P3: 4

Output:
Process BT WT TAT

P2 2 0 2

P3 4 2 6
P1 6 6 12

ii. Priority Scheduling


Algorithm:
 Sort processes by priority.

 Execute highest priority first.

Code (C):
#include <stdio.h>

struct Process {

int pid;

int bt;

int priority;

int wt;

int tat;

};

void sortByPriority(struct Process p[], int n) {

struct Process temp;

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

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

if (p[j].priority < p[i].priority) {

temp = p[i];
p[i] = p[j];

p[j] = temp;

int main() {

int n;

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

scanf("%d", &n);

struct Process p[n];

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

p[i].pid = i + 1;

printf("Enter Burst Time and Priority for P%d: ", p[i].pid);

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

sortByPriority(p, n);

p[0].wt = 0;
p[0].tat = p[0].bt;

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

p[i].wt = p[i-1].wt + p[i-1].bt;

p[i].tat = p[i].wt + p[i].bt;

printf("\nProcess\tBT\tPriority\tWT\tTAT\n");

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

printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i].pid, p[i].bt,


p[i].priority, p[i].wt, p[i].tat);

return 0;

Input:
Enter the number of processes: 3

Enter Burst Time and Priority for P1: 5 2

Enter Burst Time and Priority for P2: 3 1

Enter Burst Time and Priority for P3: 8 3

Output:
Process BT Priority WT TAT

P2 3 1 0 3

P1 5 2 3 8

P3 8 3 8 16

iii. FCFS (First-Come First-Served)


Algorithm:
 Execute processes in order of arrival.

Code (C):
#include <stdio.h>

int main() {

int n, bt[20], wt[20], tat[20];

printf("Enter number of processes: ");

scanf("%d", &n);

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

printf("Enter Burst Time for P%d: ", i+1);

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

wt[0] = 0;

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


for(int i = 0; i < n; i++) tat[i] = wt[i] + bt[i];

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

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

printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);

return 0;

Input:
Enter the number of processes: 3

Enter Burst Time for P1: 5

Enter Burst Time for P2: 3

Enter Burst Time for P3: 2

Output:
Process BT WT TAT

P1 5 0 5

P2 3 5 8

P3 2 8 10

iv. Multi-level Queue Scheduling


Description:
 Divide processes into multiple queues based on
priority/class.
 Each queue can have its own scheduling algorithm.

 Higher priority queues are scheduled before lower ones.

Implementation Hint:

 Use multiple arrays/queues.

 Apply different policies on each queue (e.g., FCFS on system


processes, Round Robin on user processes).

CODE(C):
#include <stdio.h>

#include <string.h>

#define MAX 100

typedef struct {

int pid;

int arrival_time;

int burst_time;

int remaining_time;

int completion_time;

char queue_type[10];

} Process;

void fcfs(Process sys[], int n, int *current_time) {


printf("System Queue Execution (FCFS):\n");

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

if (*current_time < sys[i].arrival_time) {

*current_time = sys[i].arrival_time;

printf("Time %d: Process %d (System)\n", *current_time,


sys[i].pid);

*current_time += sys[i].burst_time;

sys[i].completion_time = *current_time;

void round_robin(Process user[], int n, int start_time, int quantum)


{

int remaining = n, time = start_time, i = 0;

int finished[MAX] = {0};

printf("\nUser Queue Execution (Round Robin):\n");

while (remaining > 0) {

int executed = 0;

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


if (user[i].arrival_time <= time && user[i].remaining_time >
0) {

printf("Time %d: Process %d (User)\n", time, user[i].pid);

if (user[i].remaining_time <= quantum) {

time += user[i].remaining_time;

user[i].remaining_time = 0;

user[i].completion_time = time;

finished[i] = 1;

remaining--;

} else {

time += quantum;

user[i].remaining_time -= quantum;

executed = 1;

if (!executed) {

time++;

int main() {
Process processes[MAX], system_queue[MAX], user_queue[MAX];

int n, sys_count = 0, user_count = 0;

int quantum;

printf("Enter number of processes: ");

scanf("%d", &n);

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

printf("\nProcess %d:\n", i + 1);

processes[i].pid = i + 1;

printf("Arrival Time: ");

scanf("%d", &processes[i].arrival_time);

printf("Burst Time: ");

scanf("%d", &processes[i].burst_time);

printf("Queue Type (system/user): ");

scanf("%s", processes[i].queue_type);

processes[i].remaining_time = processes[i].burst_time;

if (strcmp(processes[i].queue_type, "system") == 0) {

system_queue[sys_count++] = processes[i];

} else {

user_queue[user_count++] = processes[i];
}

printf("Enter Time Quantum for User Queue (Round Robin): ");

scanf("%d", &quantum);

int current_time = 0;

fcfs(system_queue, sys_count, &current_time);

round_robin(user_queue, user_count, current_time, quantum);

printf("\nCompletion Times:\n");

for (int i = 0; i < sys_count; i++)

processes[system_queue[i].pid - 1].completion_time =
system_queue[i].completion_time;

for (int i = 0; i < user_count; i++)

processes[user_queue[i].pid - 1].completion_time =
user_queue[i].completion_time;

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


printf("Process %d - Completion Time: %d\n",
processes[i].pid, processes[i].completion_time);

return 0;

Input:
Enter number of processes: 5

Process 1:

Arrival Time: 0

Burst Time: 4

Queue Type (system/user): system

Process 2:

Arrival Time: 1

Burst Time: 3

Queue Type (system/user): system

Process 3:

Arrival Time: 2

Burst Time: 5
Queue Type (system/user): user

Process 4:

Arrival Time: 3

Burst Time: 4

Queue Type (system/user): user

Process 5:

Arrival Time: 4

Burst Time: 2

Queue Type (system/user): user

Enter Time Quantum for User Queue (Round Robin): 2

Output:
System Queue Execution (FCFS):

Time 0: Process 1 (System)

Time 4: Process 2 (System)

User Queue Execution (Round Robin):

Time 7: Process 3 (User)

Time 9: Process 4 (User)


Time 11: Process 5 (User)

Time 13: Process 3 (User)

Time 15: Process 4 (User)

Time 17: Process 3 (User)

Completion Times:

Process 1 - Completion Time: 4

Process 2 - Completion Time: 7

Process 3 - Completion Time: 19

Process 4 - Completion Time: 17

Process 5 - Completion Time: 13

You might also like