Os File
Os File
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:
Syntax : ls
directory Syntax : cd
/path/to/directory
Syntax : pwd
new_directory
2|Page
Kushagra Porwal
22scse1120004
7. mv: Moves or renames files or
destination
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;
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 :
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:
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:
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.
int page_faults = 0;
int index = 0;
if (!found)
{ frames[index] =
page;
index = (index + 1) % num_frames;
page_faults++;
}
printFrames(frames, num_frames);
}
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]);
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
int pos = 0;
}
}
return pos;
8|Page
Kushagra Porwal
22scse1120004
}
}
for (int i = 0; i < num_pages; i++) {
int page = pages[i];
int found = 0;
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]);
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:
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;
}
}
}
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]);
12 | P a g e
return 0;
}
if (bestIdx != -1)
{ allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
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]);
return 0;
13 | P a g e
}
#include <stdio.h>
if (worstIdx != -1)
{ allocation[i] =
worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
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]);
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:
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
# 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
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:
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>
int main() {
19 | P a g e
int n, initial_head;
int requests[n];
printf("Enter the request sequence:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
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:
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>
// 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;
int main() {
int n, initial_head;
int requests[n];
printf("Enter the request sequence:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
22 | P a g e
scanf("%d", &initial_head);
SSTF(requests, n, initial_head);
return 0;
}
23 | P a g e