Operating system Reg no : 23BSA10132
Name : Pavnesh yadav
Exp 1: Study of MS-DOS commands
MS-DOS (Microsoft Disk Operating System) commands are instructions used to
perform tasks on files and directories in the MS-DOS operating system. These
commands are divided into two main categories: internal and external
commands.
Internal Commands
Internal commands are built into the command interpreter ([Link])
and are always available in MS-DOS. Some common internal commands
include:
•
• • CLS: Clears the screen.
• plaintext
• C:\> CLS
• • DIR: Displays a list of files and directories in the current directory.
• plaintext
• C:\> DIR
• • COPY: Copies files from one location to another.
• plaintext
• C:\> COPY C:\[Link] D:\
• • DEL: Deletes files.
• plaintext
• C:\> DEL [Link]
• • REN: Renames files.
• plaintext
• C:\> REN [Link] [Link]
• • CD: Changes the current directory.
• plaintext
• C:\> CD \foldername
• • MD: Creates a new directory.
• plaintext
• C:\> MD newfolder
• • RD: Removes an empty directory.
• plaintext
• C:\> RD emptyfolder
External Commands
External commands are not built into the command interpreter and are stored
as separate executable files on the disk. Some common external commands
include:
•
• FORMAT: Formats a disk.
plaintext
C:\> FORMAT A:
• DISKCOPY: Copies the contents of one disk to another.
plaintext
C:\> DISKCOPY A: B:
• XCOPY: Copies files and directories, including subdirectories.
plaintext
C:\> XCOPY C:\source D:\destination /E
• CHKDSK: Checks the disk for errors and displays a status report.
plaintext
C:\> CHKDSK C:
• DEFRAG: Defragments the disk to improve performance.
plaintext
C:\> DEFRAG C:
• BACKUP: Creates backup copies of files.
plaintext
C:\> BACKUP C:\data D:\backup
• RESTORE: Restores files from a backup.
plaintext
C:\> RESTORE D:\backup C:\data
These examples illustrate how to use various MS-DOS commands to manage files,
directories, and disks.
Exp 2: C program to create a clone of current process.
Ans: #include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid;
// Create a new process
pid = fork();
if (pid < 0) {
// Fork failed
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
// Child process
printf("This is the child process. PID: %d\n", getpid());
} else {
// Parent process
printf("This is the parent process. PID: %d\n", getpid());
}
return 0;
Exp 3: Write a C program to create a new process using fork().
#include <stdio.h>
1. #include <stdlib.h>
2. #include <unistd.h>
3. #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\n");
exit(1);
}
if (pid == 0) {
// Child process
printf("Child process ID: %d\n", getpid());
printf("Parent process ID: %d\n", getppid());
sleep(2); // Sleep for 2 seconds
printf("Child process exiting...\n");
exit(0);
}
// Parent process
printf("Parent process ID: %d\n", getpid());
printf("Child process ID: %d\n", pid);
wait(NULL); // Wait for child process to finish
printf("Parent process exiting...\n");
return 0;
}
Output:
Exp 4: Implement the parent-child process relationship and demonstrate process termination.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
printf("Before fork()\n");
// Create a new process
pid = fork();
if (pid < 0) {
// Error occurred
fprintf(stderr, "fork() failed\n");
exit(1);
}
if (pid == 0) {
// Child process
printf("Child process ID: %d\n", getpid());
printf("Parent process ID: %d\n", getppid());
printf("Child process: Hello from child!\n");
sleep(2); // Sleep for 2 seconds
printf("Child process exiting...\n");
exit(0);
// Parent process
printf("Parent process ID: %d\n", getpid());
printf("Child process ID: %d\n", pid);
printf("Parent process: Hello from parent!\n");
wait(NULL); // Wait for child process to finish
printf("Parent process exiting...\n");
return 0;
Output:
}
Exp 5: Write a program to simulate First-Come-First-Served (FCFS) scheduling.
#include <stdio.h>
struct Process {
int pid; // process id
int arrival_time; // arrival time
int burst_time; // burst time
};
void fcfs_scheduling(struct Process p[], int n) {
int i, total_waiting_time = 0;
float average_waiting_time;
printf("PID\tAT\tBT\tWT\tTAT\n");
for (i = 0; i < n; i++) {
int waiting_time = (i == 0) ? 0 : p[i-1].burst_time + p[i-1].arrival_time;
int turn_around_time = waiting_time + p[i].burst_time;
printf("%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, waiting_time,
turn_around_time);
total_waiting_time += waiting_time;
average_waiting_time = (float)total_waiting_time / n;
printf("Average Waiting Time: %.2f\n", average_waiting_time);
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i+1;
fcfs_scheduling(p, n);
return 0;
Output:
Exp 6; C program to simulate Shortest Job First (SJF) scheduling with and without preemption:
#include <stdio.h>
// Structure to represent a process
struct Process {
int pid; // process id
int arrival_time; // arrival time
int burst_time; // burst time
int remaining_time; // remaining time
};
// Function to implement SJF scheduling without preemption
void sjf_scheduling_non_preemptive(struct Process p[], int n) {
int i, j, total_waiting_time = 0;
float average_waiting_time;
// Sort the processes based on their burst time
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].burst_time > p[j].burst_time) {
struct Process temp = p[i];
p[i] = p[j];
p[j] = temp;
printf("PID\tAT\tBT\tWT\tTAT\n");
int current_time = 0;
for (i = 0; i < n; i++) {
if (p[i].arrival_time > current_time) {
current_time = p[i].arrival_time;
int waiting_time = current_time - p[i].arrival_time;
int turn_around_time = waiting_time + p[i].burst_time;
printf("%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, waiting_time,
turn_around_time);
total_waiting_time += waiting_time;
current_time += p[i].burst_time;
average_waiting_time = (float)total_waiting_time / n;
printf("Average Waiting Time: %.2f\n", average_waiting_time);
// Function to implement SJF scheduling with preemption
void sjf_scheduling_preemptive(struct Process p[], int n) {
int i, j, total_waiting_time = 0;
float average_waiting_time;
// Sort the processes based on their arrival time
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].arrival_time > p[j].arrival_time) {
struct Process temp = p[i];
p[i] = p[j];
p[j] = temp;
printf("PID\tAT\tBT\tWT\tTAT\n");
int current_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
int shortest_job = -1;
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0) {
if (shortest_job == -1 || p[i].remaining_time < p[shortest_job].remaining_time) {
shortest_job = i;
if (shortest_job != -1) {
p[shortest_job].remaining_time--;
current_time++;
if (p[shortest_job].remaining_time == 0) {
int waiting_time = current_time - p[shortest_job].arrival_time - p[shortest_job].burst_time;
int turn_around_time = current_time - p[shortest_job].arrival_time;
printf("%d\t%d\t%d\t%d\t%d\n", p[shortest_job].pid, p[shortest_job].arrival_time,
p[shortest_job].burst_time, waiting_time, turn_around_time);
total_waiting_time += waiting_time;
completed_processes++;
} else {
current_time++;
average_waiting_time = (float)total_waiting_time / n;
printf("Average Waiting Time: %.2f\n", average_waiting_time);
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i+1;
p[i].remaining_time = p[i].burst_time;
printf("SJF Scheduling without Preemption:\n");
sjf_scheduling_non_preemptive(p, n);
printf("\nSJF Scheduling with Preemption:\n");
sjf_scheduling_preemptive(p, n);
return 0;
Output:
Exp 7: Write c program to simulate round robin scheduling and calculate turnaround and waiting
times.
#include <stdio.h>
#define MAX_PROCESS 100
#define MAX_BURST 100
void roundRobin(int processes[], int n, int burst_times[], int quantum) {
int i, time = 0, total_turnaround = 0, total_waiting = 0;
int turnaround[MAX_PROCESS], waiting[MAX_PROCESS];
// Calculate Turnaround and Waiting Times
for (i = 0; i < n; i++) {
int temp = quantum;
while (temp < burst_times[i]) {
time += quantum;
temp += quantum;
time += burst_times[i] - temp + quantum;
turnaround[i] = time;
waiting[i] = turnaround[i] - burst_times[i];
total_turnaround += turnaround[i];
total_waiting += waiting[i];
// Print the results
printf("Process\tBurst Time\tTurnaround Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", i + 1, burst_times[i], turnaround[i], waiting[i]);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
printf("Average Waiting Time: %.2f\n", (float)total_waiting / n);
int main() {
int n, i, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n], burst_times[n];
printf("Enter the burst times for each process:\n");
for (i = 0; i < n; i++) {
scanf("%d", &burst_times[i]);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
roundRobin(processes, n, burst_times, quantum);
return 0;
}
Output:
#include <stdio.h>
Exp 8:Write a c program Implement Priority Scheduling and test with different sets of priorities with
outout.
#include <stdio.h>
#define MAX_PROCESS 100
// Structure to represent a process
typedef struct {
int pid;
int burst_time;
int priority;
} Process;
// Function to swap two processes
void swap(Process *p1, Process *p2) {
Process temp = *p1;
*p1 = *p2;
*p2 = temp;
// Function to implement Priority Scheduling
void priorityScheduling(Process processes[], int n) {
int i, j, time = 0, total_turnaround = 0, total_waiting = 0;
int turnaround[MAX_PROCESS], waiting[MAX_PROCESS];
// Sort the processes based on their priorities
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (processes[i].priority < processes[j].priority) {
swap(&processes[i], &processes[j]);
// Calculate Turnaround and Waiting Times
for (i = 0; i < n; i++) {
time += processes[i].burst_time;
turnaround[i] = time;
waiting[i] = turnaround[i] - processes[i].burst_time;
total_turnaround += turnaround[i];
total_waiting += waiting[i];
// Print the results
printf("Process\tBurst Time\tPriority\tTurnaround Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time,
processes[i].priority, turnaround[i], waiting[i]);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
printf("Average Waiting Time: %.2f\n", (float)total_waiting / n);
int main() {
int n, i;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process processes[n];
printf("Enter the burst times and priorities for each process:\n");
for (i = 0; i < n; i++) {
processes[i].pid = i + 1;
scanf("%d %d", &processes[i].burst_time, &processes[i].priority);
priorityScheduling(processes, n);
return 0;
Output:
Exp9: write a c program to implement producer consumer problem of process
syndronization in operating system .
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define MAX 5 // Maximum size of the buffer
int buffer[MAX]; // Shared buffer
int in = 0; // Index for the next produced item
int out = 0; // Index for the next consumed item
sem_t empty; // Semaphore to count empty slots
sem_t full; // Semaphore to count full slots
pthread_mutex_t mutex; // Mutex for critical section
void* producer(void* param) {
for (int i = 0; i < 10; i++) {
// Produce an item
int item = rand() % 100;
// Wait for an empty slot
sem_wait(&empty);
// Lock the critical section
pthread_mutex_lock(&mutex);
// Add the item to the buffer
buffer[in] = item;
printf("Produced: %d\n", item);
in = (in + 1) % MAX;
// Unlock the critical section
pthread_mutex_unlock(&mutex);
// Signal that there is a new full slot
sem_post(&full);
// Sleep for a bit
sleep(1);
}
return NULL;
}
void* consumer(void* param) {
for (int i = 0; i < 10; i++) {
// Wait for a full slot
sem_wait(&full);
// Lock the critical section
pthread_mutex_lock(&mutex);
// Remove the item from the buffer
int item = buffer[out];
printf("Consumed: %d\n", item);
out = (out + 1) % MAX;
// Unlock the critical section
pthread_mutex_unlock(&mutex);
// Signal that there is a new empty slot
sem_post(&empty);
// Sleep for a bit
sleep(1);
}
return NULL;
}
int main() {
pthread_t prod, cons;
// Initialize semaphores and mutex
sem_init(&empty, 0, MAX); // Initially, the buffer is empty
sem_init(&full, 0, 0); // Initially, no full slots
pthread_mutex_init(&mutex, NULL);
// Create producer and consumer threads
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
// Wait for threads to finish
pthread_join(prod, NULL);
pthread_join(cons, NULL);
// Clean up
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
Exp10:Write a c program to implement dining philosopher problem in
operating system.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define N 5 // Number of philosophers
sem_t mutex; // Mutex for critical section
sem_t S[N]; // Semaphores for each philosopher's chopstick
void* philosopher(void* param) {
int id = *(int*)param;
int left = id;
int right = (id + 1) % N;
while (1) {
// Think for a bit
printf("Philosopher %d is thinking\n", id);
sleep(1);
// Pick up left chopstick
sem_wait(&S[left]);
printf("Philosopher %d picked up left chopstick %d\n", id, left);
// Check if right chopstick is available
sem_wait(&mutex);
if (sem_trywait(&S[right]) == 0) {
printf("Philosopher %d picked up right chopstick %d\n", id, right);
// Eat for a bit
printf("Philosopher %d is eating\n", id);
sleep(1);
// Put down right chopstick
sem_post(&S[right]);
printf("Philosopher %d put down right chopstick %d\n", id, right);
}
sem_post(&mutex);
// Put down left chopstick
sem_post(&S[left]);
printf("Philosopher %d put down left chopstick %d\n", id, left);
}
return NULL;
}
int main() {
pthread_t threads[N];
int ids[N];
// Initialize semaphores and mutex
sem_init(&mutex, 0, 1);
for (int i = 0; i < N; i++) {
sem_init(&S[i], 0, 1);
ids[i] = i;
}
// Create philosopher threads
for (int i = 0; i < N; i++) {
pthread_create(&threads[i], NULL, philosopher, &ids[i]);
}
// Wait for threads to finish
for (int i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
// Clean up
sem_destroy(&mutex);
for (int i = 0; i < N; i++) {
sem_destroy(&S[i]);
}
return 0;
}
Output:
Exp11:Write a c program to implement reader writer problemof process
syndronization in operating system.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define READERS 5
#define WRITERS 2
sem_t read_mutex; // Semaphore for controlling access to read_count
sem_t write_mutex; // Semaphore for controlling access to writing
int read_count = 0; // Number of readers currently reading
void* reader(void* param) {
int id = *(int*)param;
while (1) {
// Start reading
sem_wait(&read_mutex);
read_count++;
if (read_count == 1) {
sem_wait(&write_mutex); // First reader locks the writer
}
sem_post(&read_mutex);
// Reading section
printf("Reader %d is reading\n", id);
sleep(1); // Simulate reading time
// End reading
sem_wait(&read_mutex);
read_count--;
if (read_count == 0) {
sem_post(&write_mutex); // Last reader unlocks the writer
}
sem_post(&read_mutex);
sleep(1); // Simulate time between reads
}
return NULL;
}
void* writer(void* param) {
int id = *(int*)param;
while (1) {
// Start writing
sem_wait(&write_mutex);
// Writing section
printf("Writer %d is writing\n", id);
sleep(2); // Simulate writing time
// End writing
sem_post(&write_mutex);
sleep(1); // Simulate time between writes
}
return NULL;
}
int main() {
pthread_t readers[READERS], writers[WRITERS];
int reader_ids[READERS], writer_ids[WRITERS];
// Initialize semaphores
sem_init(&read_mutex, 0, 1); // Binary semaphore for read_count
sem_init(&write_mutex, 0, 1); // Binary semaphore for writing
// Create reader threads
for (int i = 0; i < READERS; i++) {
reader_ids[i] = i;
pthread_create(&readers[i], NULL, reader, &reader_ids[i]);
}
// Create writer threads
for (int i = 0; i < WRITERS; i++) {
writer_ids[i] = i;
pthread_create(&writers[i], NULL, writer, &writer_ids[i]);
}
// Wait for threads to finish
for (int i = 0; i < READERS; i++) {
pthread_join(readers[i], NULL);
}
for (int i = 0; i < WRITERS; i++) {
pthread_join(writers[i], NULL);
}
// Clean up
sem_destroy(&read_mutex);
sem_destroy(&write_mutex);
return 0;
}
Output:
Exp 12 : C program to implement bukers algorithm Deadlock avoidance in
operating system.
Ans : #include <stdio.h>
int main() {
int n, m; // n = number of processes, m = number of resources
int i, j, k;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resources: ");
scanf("%d", &m);
int allocation[n][m], maximum[n][m], available[m];
int need[n][m], finished[n], safeSequence[n];
// Input Allocation Matrix
printf("Enter the Allocation Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
scanf("%d", &allocation[i][j]);
}
}
// Input Maximum Matrix
printf("Enter the Maximum Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
scanf("%d", &maximum[i][j]);
}
}
// Input Available Resources
printf("Enter the Available Resources:\n");
for (j = 0; j < m; j++) {
scanf("%d", &available[j]);
}
// Calculate the Need Matrix: Need[i][j] = Maximum[i][j] - Allocation[i][j]
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
// Initialize Finished array to 0 (false)
for (i = 0; i < n; i++) {
finished[i] = 0;
}
// Banker's Algorithm
int count = 0; // To count the number of processes in the safe sequence
while (count < n) {
int found = 0; // To track if a process is found to execute
for (i = 0; i < n; i++) {
if (!finished[i]) { // Process is not yet finished
for (j = 0; j < m; j++) {
if (need[i][j] > available[j]) {
break; // Resources are insufficient for this process
}
}
if (j == m) { // All resources for process i can be allocated
// Update Available Resources
for (k = 0; k < m; k++) {
available[k] += allocation[i][k];
}
safeSequence[count++] = i; // Add process to safe sequence
finished[i] = 1; // Mark process as finished
found = 1;
}
}
}
if (!found) { // No process could be executed
printf("System is in an unsafe state. Deadlock may occur.\n");
return 1;
}
}
// Output Safe Sequence
printf("System is in a safe state.\nSafe Sequence: ");
for (i = 0; i < n; i++) {
printf("P%d ", safeSequence[i]);
}
printf("\n");
return 0;
}
Output: