Operating system 2.O
Operating system 2.O
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:
•
plaintext
C:\> FORMAT A:
plaintext
C:\> DISKCOPY A: B:
plaintext
• CHKDSK: Checks the disk for errors and displays a status report.
plaintext
C:\> CHKDSK C:
plaintext
C:\> DEFRAG C:
plaintext
These examples illustrate how to use various MS-DOS commands to manage files,
directories, and disks.
#include <unistd.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
// Fork failed
return 1;
} else if (pid == 0) {
// Child process
} else {
// Parent process
}
return 0;
int main() {
pid_t pid;
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");
pid = fork();
if (pid < 0) {
// Error occurred
exit(1);
}
if (pid == 0) {
// Child process
exit(0);
// Parent process
return 0;
Output:
}
Exp 5: Write a program to simulate First-Come-First-Served (FCFS) scheduling.
#include <stdio.h>
struct Process {
};
int i, total_waiting_time = 0;
float average_waiting_time;
printf("PID\tAT\tBT\tWT\tTAT\n");
total_waiting_time += waiting_time;
average_waiting_time = (float)total_waiting_time / n;
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter arrival time and burst time for process %d: ", i+1);
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 i, j, total_waiting_time = 0;
float average_waiting_time;
p[i] = p[j];
p[j] = temp;
printf("PID\tAT\tBT\tWT\tTAT\n");
int current_time = 0;
current_time = p[i].arrival_time;
total_waiting_time += waiting_time;
current_time += p[i].burst_time;
average_waiting_time = (float)total_waiting_time / n;
int i, j, total_waiting_time = 0;
float average_waiting_time;
p[i] = p[j];
p[j] = temp;
printf("PID\tAT\tBT\tWT\tTAT\n");
int current_time = 0;
int completed_processes = 0;
shortest_job = i;
if (shortest_job != -1) {
p[shortest_job].remaining_time--;
current_time++;
if (p[shortest_job].remaining_time == 0) {
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;
scanf("%d", &n);
printf("Enter arrival time and burst time for process %d: ", i+1);
p[i].pid = i+1;
p[i].remaining_time = p[i].burst_time;
sjf_scheduling_non_preemptive(p, 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>
time += quantum;
temp += quantum;
total_turnaround += turnaround[i];
total_waiting += waiting[i];
int main() {
int n, i, quantum;
scanf("%d", &n);
scanf("%d", &burst_times[i]);
scanf("%d", &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>
typedef struct {
int pid;
int burst_time;
int priority;
} Process;
*p1 = *p2;
*p2 = temp;
swap(&processes[i], &processes[j]);
time += processes[i].burst_time;
turnaround[i] = time;
total_turnaround += turnaround[i];
total_waiting += waiting[i];
int main() {
int n, i;
scanf("%d", &n);
Process processes[n];
processes[i].pid = i + 1;
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>
int main() {
pthread_t prod, cons;
// Clean up
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
while (1) {
// Think for a bit
printf("Philosopher %d is thinking\n", id);
sleep(1);
int main() {
pthread_t threads[N];
int ids[N];
// 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
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;
}
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);
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
return 0;
}
Output:
int main() {
int n, m; // n = number of processes, m = number of resources
int i, j, k;
// 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