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

Os File

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Os File

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

1|Page

Kushagra Porwal
22scse1120004
Eeee

Exp 1
Basic Commands in Linux Operating System

AIM:
To study of Basic UNIX Commands and various UNIX editors such as vi, ed, ex
and EMACS.

CONTENT:

Linux is a powerful and versatile operating system that provides a wide range of
commands to manage the system and perform various tasks. Here are some basic
Linux commands that are essential for navigating and manipulating files and
directories, managing processes, and configuring the system:

: File and Directory Commands

1. ls: Lists files and directories in the current directory

Syntax : ls

2. cd: Changes the current

directory Syntax : cd

/path/to/directory

3. pwd: Prints the current working directory

Syntax : pwd

4. mkdir: Creates a new

directory Syntax : mkdir

new_directory

5. rm: Removes files or directories.

Syntax : rmdir empty_directory

6. cp: Copies files or directories

Syntax : cp source_file destination

cp -r source_directory destination # for directories

2|Page

Kushagra Porwal
22scse1120004
7. mv: Moves or renames files or

directories Syntax : mv source_file

destination

mv old_name new_name # for renaming

8. touch: Creates an empty file or updates the timestamp of an existing file

Syntax : touch new_file

9. cat: Concatenates and displays file content

Syntax : cat file

3|Page

Kushagra Porwal
22scse1120004
Write a C prog ra m using the following system calls(fork, exec ).

Exp 2 Write a C program using the following system calls (fork, exec).

AIM:
Write a C program using the following system calls (fork, exec).

CONCEPT:
Fork : a copy of the current process except for the returned value
execlp() replaces the current process image with a new process image

Code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{ pid_t pid;

// Create a new process


pid = fork();

if (pid < 0) { // Error occurred


fprintf(stderr, "Fork Failed");
return 1;
} else if (pid == 0) { // Child process
// Execute a new program using execlp
execlp("/bin/ls", "ls", NULL);

// If exec returns, it must have failed


perror("execlp failed");
exit(1);
} else { // Parent process
// Wait for the child to complete
wait(NULL);
printf("Child Complete\n");
}

return 0;
}

4|Page

Kushagra Porwal
22scse1120004
Write a C programs to simulate Page Replacement Algorithms) FIFO b) LRU
Exp 3

AIM:
Write a C programs to simulate Page Replacement Algorithms) FIFO b) LRU -

Content :

FIFO Page Replacement Algorithm

Initialization:

1. The frames array holds the pages currently in memory, initialized to -1 (empty).
2. page_faults counts the number of page faults.
3. index keeps track of the next frame to replace.

Page Replacement:

1. For each page in the input:

1. Check if the page is already in the frames.


2. If not, replace the page at the current index and increment the index.
3. Print the frames after each replacement.

2. Print the total number of page faults at the end.

LRU Page Replacement Algorithm

Initialization:

1. The frames array holds the pages currently in memory, initialized to -1 (empty).
2. The time array keeps track of when each frame was last used.
3. counter simulates the passage of time.
4. page_faults counts the number of page faults.

Page Replacement:

1. For each page in the input:

1. Check if the page is already in the frames.


5|Page

Kushagra Porwal
22scse1120004
2. If not, find the least recently used frame using the time array and replace it.
3. Update the time array with the current counter value for the accessed or replaced page.
4. Print the frames after each replacement.

2. Print the total number of page faults at the end.

FIFO Page Replacement ALGORITHM:


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

void printFrames(int frames[], int num_frames)


{ for (int i = 0; i < num_frames; i++) {
if (frames[i] != -1)
printf("%d ", frames[i]);
else
printf("E "); // E denotes an empty frame
}
printf("\n");
}

void fifoPageReplacement(int pages[], int num_pages, int num_frames) {


int frames[num_frames];
for (int i = 0; i < num_frames; i++) {
frames[i] = -1; // Initialize frames as empty
}

int page_faults = 0;
int index = 0;

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


{ int page = pages[i];
int found = 0;

for (int j = 0; j < num_frames; j++) {


if (frames[j] == page) {
found = 1;
break;
}
}

if (!found)
{ frames[index] =
page;
index = (index + 1) % num_frames;
page_faults++;
}

printFrames(frames, num_frames);
}

printf("Total Page Faults: %d\n", page_faults);


}

6|Page

Kushagra Porwal
22scse1120004
int main() {

7|Page

Kushagra Porwal
22scse1120004
int num_frames = 3;
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1};
int num_pages = sizeof(pages) / sizeof(pages[0]);

printf("FIFO Page Replacement Algorithm\n");


fifoPageReplacement(pages, num_pages, num_frames);

return 0;
}

LRU Page
Replacement
Algorithm:
#include <stdio.h>
#include <stdlib.h>
void printFrames(int frames[], int num_frames) {
for (int i = 0; i < num_frames; i++) {

if (frames[i] != -1)
printf("%d ", frames[i]);
else

printf("E "); // E denotes an empty frame


}
printf("\n");
}
int findLRU(int time[], int num_frames)
{ int min = time[0];

int pos = 0;

for (int i = 1; i < num_frames; i++)


{ if (time[i] < min) {
min = time[i];
pos = i;

}
}
return pos;

8|Page

Kushagra Porwal
22scse1120004
}

void lruPageReplacement(int pages[], int num_pages, int num_frames) {


int frames[num_frames];
int time[num_frames];
int counter = 0;
int page_faults = 0;

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


{ frames[i] = -1;

}
for (int i = 0; i < num_pages; i++) {
int page = pages[i];

int found = 0;

for (int j = 0; j < num_frames; j++)


{ if (frames[j] == page) {

found = 1;
time[j] = ++counter;
break;

}
}
if (!found) {
int pos = findLRU(time, num_frames);
frames[pos] = page;
time[pos] = ++counter;
page_faults++;

}
printFrames(frames, num_frames);
}
printf("Total Page Faults: %d\n", page_faults);
}

9|Page

Kushagra Porwal
22scse1120004
int main() {
int num_frames = 3;
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1};
int num_pages = sizeof(pages) / sizeof(pages[0]);

printf("LRU Page Replacement Algorithm\n");


lruPageReplacement(pages, num_pages, num_frames);

return 0;
}
}
}

10 | P a g e

Kushagra Porwal
22scse1120004
.

11 | P a g e

Kushagra Porwal
22scse1120004
Ex.No:3 Implementation of memory allocation algorithms: a)First Fit b) Best Fit
c)Worst Fit).

AIM:

1. Implementation of memory allocation

algorithms: a)First Fit b) Best Fit c)Worst Fit).


Content :
1. First Fit Memory Allocation Algorithm:
The allocation array is initialized to -1, indicating no process is allocated
initially.
#include <stdio.h>

void firstFit(int blockSize[], int m, int processSize[], int n) { int


allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially no block is assigned
}

for (int i = 0; i < n; i++) { // Allocate memory for each process for
(int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i])
{ allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]); if
(allocation[i] != -1)
printf("%d\n", allocation[i] + 1); else
printf("Not Allocated\n");
}
}

int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

printf("First Fit Memory Allocation:\n");


firstFit(blockSize, m, processSize, n);

12 | P a g e
return 0;
}

2. Best Fit Memory Allocation Algorithm:


The allocation array is initialized to -1, indicating no process is allocated
initially.
#include <stdio.h>

void bestFit(int blockSize[], int m, int processSize[], int n)


{ int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially no block is assigned
}

for (int i = 0; i < n; i++) { // Allocate memory for each process


int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}

if (bestIdx != -1)
{ allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

printf("Best Fit Memory Allocation:\n");


bestFit(blockSize, m, processSize, n);

return 0;

13 | P a g e
}

3. Worst Fit Memory Allocation Algorithm:


The allocation array is initialized to -1, indicating no process is allocated
initially.

#include <stdio.h>

void worstFit(int blockSize[], int m, int processSize[], int n)


{ int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially no block is assigned
}

for (int i = 0; i < n; i++) { // Allocate memory for each process


int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx])
{ worstIdx = j;
}
}
}

if (worstIdx != -1)
{ allocation[i] =
worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

printf("Worst Fit Memory Allocation:\n");


worstFit(blockSize, m, processSize, n);

return 0;

14 | P a g e
}

15 | P a g e
Exp:4 Implement the Producer - Consumer problem using semaphores

AIM:
Implement the Producer - Consumer problem using semaphores

Content

The Producer-Consumer problem is a classic synchronization problem that deals with how two processes
(the producer and the consumer) can communicate efficiently using a shared buffer. The producer generates
data and places it in the buffer, while the consumer takes the data from the buffer and processes it. The
challenge is to ensure that the producer does not add data into a full buffer and the consumer does not
remove data from an empty buffer. This can be effectively managed using semaphores.

 Semaphores:

 empty: Initialized to the size of the buffer (BUFFER_SIZE). This semaphore counts the number of empty
slots in the buffer.
 full: Initialized to 0. This semaphore counts the number of full slots in the buffer.
 mutex: Ensures mutual exclusion when accessing the buffer.

 Producer Function:

 Produces an item.
 Waits for an empty slot using empty.acquire().
 Acquires the mutex to ensure exclusive access to the buffer.
 Adds the item to the buffer.
 Releases the mutex.
 Signals that a new item is available by calling full.release().

 Consumer Function:

 Waits for a full slot using full.acquire().


 Acquires the mutex to ensure exclusive access to the buffer.
 Removes an item from the buffer.
 Releases the mutex.
 Signals that a slot has been freed by calling empty.release().

 Threads:

 Two threads are created, one for the producer and one for the consumer.
16 | P a g e
 Both threads are started and will run indefinitely, simulating continuous production and consumption of items.

Code:
import threading
import time
import random

# Define the buffer and its size


BUFFER_SIZE = 10
buffer = []

# Create semaphores
empty = threading.Semaphore(BUFFER_SIZE) # Counts the number of empty slots in the
buffer full = threading.Semaphore(0) # Counts the number of full slots in the buffer
mutex = threading.Semaphore(1) # Ensures mutual exclusion for buffer access

# Define the producer function


def producer():
global buffer
while True:
item = random.randint(1, 100) # Produce an item
empty.acquire() # Wait for an empty slot
mutex.acquire() # Ensure mutual exclusion
buffer.append(item) # Add the item to the buffer
print(f'Produced: {item} | Buffer: {buffer}')
mutex.release() # Release mutual exclusion
full.release() # Increase the count of full slots
time.sleep(random.random()) # Simulate production time

# Define the consumer function


def consumer():
global buffer
while True:
full.acquire() # Wait for a full slot
mutex.acquire() # Ensure mutual exclusion
item = buffer.pop(0) # Remove the item from the buffer
print(f'Consumed: {item} | Buffer: {buffer}')
mutex.release() # Release mutual exclusion
empty.release() # Increase the count of empty slots
time.sleep(random.random()) # Simulate consumption time

# Create producer and consumer threads


producer_thread = threading.Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)

# Start the threads


producer_thread.start()
consumer_thread.start()

# Join the threads (optional in this case, as they run indefinitely)


producer_thread.join()

17 | P a g e
consu
mer_t
hread.j
oin()

18 | P a g e
Write a C programs to simulate implementation of Disk Scheduling
Exp:5 Algorithms: FCFS

AIM:
Write a C programs to simulate implementation of Disk Scheduling Algorithms: FCFS

Content:

 Input:

 The number of disk requests (n).


 The sequence of disk requests.
 The initial position of the disk head.

 FCFS Function:

 Takes the request sequence, the number of requests, and the initial head position as arguments.
 Iterates through each request, calculating the total head movement required to service all the requests in the
given order.
 The head movement for each request is calculated as the absolute difference between the current head
position and the next request position.
 Updates the current head position after servicing each request.
 Prints the sequence of request access and the total head movement.

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

// Function to simulate FCFS Disk Scheduling


void FCFS(int requests[], int n, int initial_head) {
int total_head_movement = 0;
int current_position = initial_head;

printf("Sequence of request access:\n");


for (int i = 0; i < n; i++) {
printf("%d ", requests[i]);
total_head_movement += abs(requests[i] - current_position);
current_position = requests[i];
}
printf("\nTotal head movement: %d\n", total_head_movement);
}

int main() {
19 | P a g e
int n, initial_head;

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


scanf("%d", &n);

int requests[n];
printf("Enter the request sequence:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}

printf("Enter the initial head position: ");


scanf("%d", &initial_head);

FCFS(requests, n, initial_head);

return 0;
}

20 | P a g e
Write a C programs to simulate implementation SSTF Disk
Exp 6 Scheduling Algorithm.

AIM:
Write a C programs to simulate implementation SSTF Disk Scheduling Algorithm.

Content:

 Input:

 The number of disk requests (n).


 The sequence of disk requests.
 The initial position of the disk head.

 SSTF Function:

 Takes the request sequence, the number of requests, and the initial head position as arguments.
 Initializes an array serviced to keep track of which requests have been serviced.
 Iterates through the requests, finding the closest unserviced request to the current head position using the
findClosestRequest function.
 Updates the total head movement and current head position.
 Marks the closest request as serviced and stores the sequence of serviced requests.
 Prints the sequence of request access and the total head movement.

 findClosestRequest Function:

 Takes the current head position, the serviced array, the request sequence, and the number of requests as
arguments.
 Iterates through the requests to find the closest unserviced request to the current head position.
 Returns the index of the closest request.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 100

// Function to find the request with the minimum seek time from the current head position
int findClosestRequest(int current_position, bool serviced[], int requests[], int n) {
int min_distance = MAX;
int closest_index = -1;

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


if (!serviced[i]) {
int distance = abs(requests[i] - current_position);
if (distance < min_distance) {
21 | P a g e
min_distance = distance;
closest_index = i;
}
}
}
return closest_index;
}

// Function to simulate SSTF Disk Scheduling


void SSTF(int requests[], int n, int initial_head) {
bool serviced[n];
int total_head_movement = 0;
int current_position = initial_head;
int sequence[n];

// Initialize all requests as unserviced


for (int i = 0; i < n; i++) {
serviced[i] = false;
}

printf("Sequence of request access:\n");

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


int closest_index = findClosestRequest(current_position, serviced, requests, n);
serviced[closest_index] = true;
sequence[i] = requests[closest_index];
total_head_movement += abs(requests[closest_index] - current_position);
current_position = requests[closest_index];
printf("%d ", sequence[i]);
}

printf("\nTotal head movement: %d\n", total_head_movement);


}

int main() {
int n, initial_head;

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


scanf("%d", &n);

int requests[n];
printf("Enter the request sequence:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}

printf("Enter the initial head position: ");

22 | P a g e
scanf("%d", &initial_head);

SSTF(requests, n, initial_head);

return 0;
}

23 | P a g e

You might also like