FIFO (First-In-First-Out) Page Replacement Algorithm
#include <stdio.h>
#define MAX_FRAMES 3 // Maximum number of frames in memory
// Function to check if a page is already in memory
int isPageInMemory(int frames[], int num_frames, int page) {
for (int i = 0; i < num_frames; i++) {
if (frames[i] == page) {
return 1; // Page is already in memory
return 0; // Page is not in memory
// FIFO Page Replacement
void fifoPageReplacement(int pages[], int num_pages, int num_frames) {
int frames[num_frames]; // Array to store frames in memory
int page_faults = 0; // Counter for page faults
// Initialize frames to -1 (empty)
for (int i = 0; i < num_frames; i++) {
frames[i] = -1;
// Queue pointers
int front = 0;
int rear = 0;
printf("Pages in memory after each page reference:\n");
// Process each page reference
for (int i = 0; i < num_pages; i++) {
int current_page = pages[i];
printf("\nPage Reference: %d\n", current_page);
// If the page is not in memory, we have a page fault
if (!isPageInMemory(frames, num_frames, current_page)) {
// If memory is full, remove the oldest page (FIFO)
if (frames[rear] != -1) {
printf("Page %d replaced by page %d\n", frames[front], current_page);
// Add the new page to the memory (FIFO - Insert at the rear)
frames[rear] = current_page;
// Move the rear pointer and handle the queue wraparound
rear = (rear + 1) % num_frames;
// If the memory is not full, we just increment the front pointer
if (frames[front] == -1) {
front = (front + 1) % num_frames;
page_faults++;
}
// Print the current contents of the frames
printf("Frames: ");
for (int j = 0; j < num_frames; j++) {
if (frames[j] == -1) {
printf("Empty ");
} else {
printf("%d ", frames[j]);
printf("\n");
printf("\nTotal Page Faults: %d\n", page_faults);
int main() {
// Define the page reference string (sequence of pages that need to be accessed)
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 3};
int num_pages = sizeof(pages) / sizeof(pages[0]);
// Define the number of frames in memory
int num_frames = MAX_FRAMES;
// Call the FIFO page replacement function
fifoPageReplacement(pages, num_pages, num_frames);
return 0;
#include <stdio.h>
#include <stdlib.h>
int main() {
// Simple example to run the "ls" command from C
system("ls -l");
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process ID: %d\n", getpid());
exit(0);
} else if (pid > 0) {
// Parent process
printf("Parent process ID: %d\n", getpid());
wait(NULL); // Wait for child to finish
} else {
perror("Fork failed");
return 0;
#include <stdio.h>
#include <pthread.h>
void* print_hello(void* arg) {
printf("Hello from thread!\n");
return NULL;
}
int main() {
pthread_t thread_id;
pthread_create(&thread_id, NULL, print_hello, NULL);
pthread_join(thread_id, NULL); // Wait for thread to finish
return 0;
#include <stdio.h>
struct Process {
int id, burst_time, waiting_time, turn_around_time;
};
void findWaitingTime(struct Process proc[], int n) {
proc[0].waiting_time = 0;
for (int i = 1; i < n; i++) {
proc[i].waiting_time = proc[i - 1].burst_time + proc[i - 1].waiting_time;
void findTurnAroundTime(struct Process proc[], int n) {
for (int i = 0; i < n; i++) {
proc[i].turn_around_time = proc[i].burst_time + proc[i].waiting_time;
}
void findAverageTime(struct Process proc[], int n) {
float total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += proc[i].waiting_time;
total_tat += proc[i].turn_around_time;
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
int main() {
struct Process proc[] = {{1, 6, 0, 0}, {2, 8, 0, 0}, {3, 7, 0, 0}, {4, 3, 0, 0}};
int n = sizeof(proc) / sizeof(proc[0]);
findWaitingTime(proc, n);
findTurnAroundTime(proc, n);
findAverageTime(proc, n);
return 0;
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full, mutex;
void* producer(void* arg) {
int item;
while (1) {
item = rand() % 100;
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = item;
printf("Produced: %d\n", item);
in = (in + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&full);
sleep(1);
return NULL;
void* consumer(void* arg) {
int item;
while (1) {
sem_wait(&full);
sem_wait(&mutex);
item = buffer[out];
printf("Consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&empty);
sleep(2);
return NULL;
int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return 0;
#include <stdio.h>
void FIFO(int pages[], int n, int capacity) {
int frame[capacity];
int page_faults = 0;
// Initialize frame with -1
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
int next_slot = 0; // To keep track of the next frame to be replaced
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
// Check if page is already in one of the frames
for (int j = 0; j < capacity; j++) {
if (frame[j] == page) {
found = 1;
break;
// If page is not found, replace the page using FIFO
if (!found) {
frame[next_slot] = page;
next_slot = (next_slot + 1) % capacity;
page_faults++;
// Display current frames
printf("Frames: ");
for (int j = 0; j < capacity; j++) {
printf("%d ", frame[j]);
printf("\n");
printf("Total Page Faults: %d\n", page_faults);
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3}; // Reference string
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Frame capacity
FIFO(pages, n, capacity);
return 0;
#include <stdio.h>
void LRU(int pages[], int n, int capacity) {
int frame[capacity];
int page_faults = 0;
int last_used[capacity]; // To keep track of the last used time of each page
// Initialize frame and last_used arrays
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
last_used[i] = -1;
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
// Check if page is already in one of the frames
for (int j = 0; j < capacity; j++) {
if (frame[j] == page) {
found = 1;
last_used[j] = i; // Update the last used time
break;
// If page is not found, replace the least recently used page
if (!found) {
int min_last_used = i;
int replace_idx = 0;
for (int j = 0; j < capacity; j++) {
if (last_used[j] < min_last_used) {
min_last_used = last_used[j];
replace_idx = j;
frame[replace_idx] = page;
last_used[replace_idx] = i; // Update last used time
page_faults++;
// Display current frames
printf("Frames: ");
for (int j = 0; j < capacity; j++) {
printf("%d ", frame[j]);
printf("\n");
printf("Total Page Faults: %d\n", page_faults);
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0