INDEX
S.No. Program Page No. Sign
Installation of unix and Write programs using the following
1 system calls of UNIX operating system: fork, exec, getpid, exit, 2-4
wait, stat, opendir, readdir.
Write programs using the I/O system calls of UNIX operating
2 5-6
system (open, read, write, etc).
3 Write c program to simulate unix commands like ls, grep etc. 7-8
Given a list of processes, their cpu burst time and arrival time.
4 Display the gantt chart for fcfs, and sjf. And compute and print 9-10
avg. Waiting time and turn around time.
Given a list of processes, their cpu burst time and arrival time.
5 Display the gantt chart for priority and round robin And 11-14
compute and print avg. Waiting time and turn around time.
1
Experiment - 1
Objective : Installation of unix and write programs using the following system calls of UNIX operating
system: fork, exec, getpid, exit, wait, stat, opendir, readdir.
Code:
fork(), getpid(), wait(), exit()
#include <stdio.h> // For printf()
#include <stdlib.h> // For exit()
#include <unistd.h> // For fork(), getpid()
#include <sys/wait.h> // For wait()
int main() {
pid_t pid = fork(); // Create a child process
if (pid < 0) {
// Error in creating a child process
perror("fork failed");
exit(1);
}
else if (pid == 0) {
// Child process block
printf("Child: PID = %d\n", getpid());
exit(0); // Child exits
}
else {
// Parent process block
wait(NULL); // Parent waits for child to finish
printf("Parent: PID = %d, Child terminated\n", getpid());
}
return 0;
}
Output :
exec() System Call
#include <stdio.h>
#include <unistd.h>
int main() {
2
printf("Before exec\n");
execl("/bin/ls", "ls", "-l", NULL);
printf("This won't be printed if exec is successful\n");
return 0;
}
Output :
Stat()
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat fileStat; // Declare a struct to hold file info
if (stat("one1.c", &fileStat) < 0) {
perror("stat failed"); // Print error if file does not exist or is inaccessible
return 1;
}
// Print file size in bytes
printf("File Size: %ld bytes\n", fileStat.st_size);
// Print file permissions (last 9 bits)
printf("Permissions: %o\n", fileStat.st_mode & 0777);
return 0;
}
Output :
3
opendir(), readdir()
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
struct dirent *entry;
if (dir == NULL) {
perror("opendir failed");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return 0;
}
Output :
4
Experiment - 2
Objective : Write programs using the I/O system calls of UNIX operating system (open, read, write,
etc.)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> // For open()
#include <unistd.h> // For read(), write(), close()
#define BUFFER_SIZE 1024
int main() {
int input_fd, output_fd;
ssize_t bytes_read, bytes_written;
char buffer[BUFFER_SIZE];
// Open input file for reading
input_fd = open("input.txt", O_RDONLY);
if (input_fd < 0) {
perror("Failed to open input.txt");
return 1;
}
// Open (or create) output file for writing (truncate if it exists)
output_fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (output_fd < 0) {
perror("Failed to open output.txt");
close(input_fd);
return 1;
}
// Read from input and write to output
while ((bytes_read = read(input_fd, buffer, BUFFER_SIZE)) > 0) {
bytes_written = write(output_fd, buffer, bytes_read);
if (bytes_written != bytes_read) {
perror("Write error");
close(input_fd);
close(output_fd);
return 1;
}
}
if (bytes_read < 0) {
perror("Read error");
}
5
// Close files
close(input_fd);
close(output_fd);
printf("File copied successfully using system calls.\n");
return 0;
}
Output :
6
Experiment - 3
Objective : Write c program to simulate unix commands like ls, grep etc..
Code:
Ls command
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
const char *path = "."; // Default to current directory
if (argc > 1) {
path = argv[1];
}
DIR *dir = opendir(path);
if (!dir) {
perror("opendir failed");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
// Skip hidden files (starting with '.')
if (entry->d_name[0] != '.') {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
return 0;
}
Output :
7
Grep command
// simulate_grep.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_SIZE 1024
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <pattern> <filename>\n", argv[0]);
return 1;
}
char *pattern = argv[1];
char *filename = argv[2];
FILE *file = fopen(filename, "r");
if (!file) {
perror("fopen failed");
return 1;
}
char line[LINE_SIZE];
while (fgets(line, sizeof(line), file)) {
if (strstr(line, pattern)) {
printf("%s", line);
}
}
fclose(file);
return 0;
}
Output :
8
Experiment - 4
Objective : Given a list of processes, their CPU burst time and arrival time. Display the Gantt chart for
FCFS, and SJF. And compute and print avg. Waiting time and turnaround time.
Code:
import random
# List of words for the game
word_list = ['python', 'hangman', 'programming', 'developer', 'algorithm']
# Choose a random word
word = random.choice(word_list)
guessed_word = ['_'] * len(word)
attempts = 6
guessed_letters = []
print("Welcome to Hangman!")
while attempts > 0 and '_' in guessed_word:
print("\nWord: " + ' '.join(guessed_word))
print(f"Guessed letters: {', '.join(guessed_letters)}")
print(f"Attempts left: {attempts}")
guess = input("Guess a letter: ").lower()
if not guess.isalpha() or len(guess) != 1:
print("Please enter a single valid letter.")
continue
if guess in guessed_letters:
print("You've already guessed that letter.")
continue
guessed_letters.append(guess)
if guess in word:
for i, letter in enumerate(word):
if letter == guess:
guessed_word[i] = guess
print("Good guess!")
else:
attempts -= 1
print("Wrong guess!")
# End of game
if '_' not in guessed_word:
print("\nCongratulations! You guessed the word:", word)
9
else:
print("\nGame Over! The word was:", word)
Output :
10
Experiment - 5
Objective : Given a list of processes, their CPU burst time and arrival time. Display the Gantt chart for
priority and round robin and compute and print average Waiting time and turnaround time.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
#define QUANTUM 2
typedef struct {
int pid;
int arrival;
int burst;
int priority;
int remaining;
int waiting;
int turnaround;
int completed;
} Process;
void printPriorityScheduling(Process proc[]) {
int time = 0, completed = 0;
float total_wait = 0, total_turn = 0;
printf("\n--- Priority Scheduling (Non-Preemptive) ---\n");
printf("Gantt Chart:\n|");
while (completed < N) {
int idx = -1;
int highest_priority = 9999;
for (int i = 0; i < N; i++) {
if (!proc[i].completed && proc[i].arrival <= time && proc[i].priority < highest_priority) {
highest_priority = proc[i].priority;
idx = i;
}
}
if (idx == -1) {
time++;
continue;
}
printf(" P%d |", proc[idx].pid);
11
proc[idx].waiting = time - proc[idx].arrival;
time += proc[idx].burst;
proc[idx].turnaround = proc[idx].waiting + proc[idx].burst;
proc[idx].completed = 1;
completed++;
total_wait += proc[idx].waiting;
total_turn += proc[idx].turnaround;
}
// Timeline
printf("\n0");
time = 0;
for (int i = 0; i < N; i++) {
if (proc[i].completed) {
time = proc[i].arrival + proc[i].turnaround;
printf(" %d", time);
}
}
// Result Table
printf("\n\nProcess\tArrival\tBurst\tPriority\tWaiting\tTurnaround\n");
for (int i = 0; i < N; i++) {
printf("P%d\t%d\t%d\t%d\t\t%d\t%d\n",
proc[i].pid, proc[i].arrival, proc[i].burst,
proc[i].priority, proc[i].waiting, proc[i].turnaround);
}
printf("Avg Waiting Time: %.2f\n", total_wait / N);
printf("Avg Turnaround Time: %.2f\n", total_turn / N);
}
void printRoundRobin(Process proc[]) {
int time = 0, completed = 0;
float total_wait = 0, total_turn = 0;
int queue[100], front = 0, rear = 0;
int visited[N] = {0};
printf("\n--- Round Robin Scheduling (Time Quantum = %d) ---\n", QUANTUM);
printf("Gantt Chart:\n|");
// Initialize process state
for (int i = 0; i < N; i++) {
proc[i].remaining = proc[i].burst;
proc[i].waiting = 0;
proc[i].turnaround = 0;
proc[i].completed = 0;
}
12
// Add the first process
queue[rear++] = 0;
visited[0] = 1;
while (completed < N) {
int idx = queue[front++];
if (proc[idx].remaining > 0) {
printf(" P%d |", proc[idx].pid);
int exec = (proc[idx].remaining >= QUANTUM) ? QUANTUM : proc[idx].remaining;
time += exec;
proc[idx].remaining -= exec;
// Enqueue any newly arrived processes
for (int i = 0; i < N; i++) {
if (!visited[i] && proc[i].arrival <= time) {
queue[rear++] = i;
visited[i] = 1;
}
}
// Re-queue the current process if not completed
if (proc[idx].remaining > 0) {
queue[rear++] = idx;
} else {
proc[idx].turnaround = time - proc[idx].arrival;
proc[idx].waiting = proc[idx].turnaround - proc[idx].burst;
proc[idx].completed = 1;
total_wait += proc[idx].waiting;
total_turn += proc[idx].turnaround;
completed++;
}
}
// If queue becomes empty but not all are completed
if (front == rear && completed < N) {
for (int i = 0; i < N; i++) {
if (!proc[i].completed) {
queue[rear++] = i;
break;
}
}
}
}
// Result Table
printf("\n\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");
13
for (int i = 0; i < N; i++) {
printf("P%d\t%d\t%d\t%d\t%d\n",
proc[i].pid, proc[i].arrival, proc[i].burst,
proc[i].waiting, proc[i].turnaround);
}
printf("Avg Waiting Time: %.2f\n", total_wait / N);
printf("Avg Turnaround Time: %.2f\n", total_turn / N);
}
int main() {
Process proc1[N] = {
{1, 0, 10, 3},
{2, 1, 4, 1},
{3, 2, 5, 4},
{4, 3, 3, 2},
{5, 4, 1, 5}
};
Process proc2[N];
memcpy(proc2, proc1, sizeof(proc1)); // Copy for Round Robin
printPriorityScheduling(proc1);
printRoundRobin(proc2);
return 0;
}
Output :
14