Implement "mv" Command using Kernel API
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
if (argc != 3)
fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
exit(EXIT_FAILURE);
char *src = argv[1];
char *dest = argv[2];
if (rename(src, dest) == -1)
if (errno == EXDEV)
fprintf(stderr,
"Error: Cannot move across di erent filesystems.\n");
else
perror("Error moving file");
}
exit(EXIT_FAILURE);
printf("Successfully moved '%s' to '%s'\n", src, dest);
return 0;
Output
$ gcc exp3-2.c -o bh
$ echo "this is exp no 3" > [Link]
$ ./bh [Link] [Link]
Successfully moved '[Link]' to '[Link]'
$ cat [Link] this is exp no 3
Explore System Calls used in File Operations
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
int fd;
char bu er[50];
// open() system call – create/open file
fd = open("[Link]", O_CREAT | O_RDWR, 0644);
// write() system call
write(fd, "Hello Linux System Calls\n", 25);
// lseek() – move pointer to beginning
lseek(fd, 0, SEEK_SET);
// read() system call
read(fd, bu er, 25);
write(1, bu er, 25); // print to stdout
close(fd);
// fork() – create child process
if (fork() == 0)
printf("\nChild Process ID: %d\n", getpid());
else
wait(NULL);
printf("Parent Process ID: %d\n", getpid());
return 0;
Output
$ gcc Exp5.c -o ex
$ ./ex
Hello Linux System Calls
Child Process ID: 4368
Parent Process ID: 4307
Create a Child Process using fork() System Call
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
int pid;
pid = fork();
if (pid < 0)
printf("Child process creation failed\n");
return 1;
else if (pid == 0)
printf("This is the child process\n");
printf("Child PID: %d\n", getpid());
else
printf("This is the parent process\n");
printf("Parent PID: %d\n", getpid());
printf("Child PID: %d\n", pid);
}
return 0;
Output
$ gcc Exp4.c -o fork_program
$ ./fork_program
This is the parent process
Parent PID: 6352
Child PID: 6353
This is the child process
Child PID = 6353
7 Deadlock Avoidance using Banker's Algorithm
#include <stdio.h>
int main() {
int n, m, i, j, k;
int alloc[10][10], max[10][10], need[10][10];
int avail[10], finish[10], safeSeq[10];
int work[10], count = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);
printf("Enter Allocation Matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Enter Maximum Matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter Available Resources:\n");
for(i=0;i<m;i++)
scanf("%d",&avail[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j] = max[i][j] - alloc[i][j];
for(i=0;i<n;i++)
finish[i] = 0;
for(i=0;i<m;i++)
work[i] = avail[i];
while(count < n) {
int found = 0;
for(i=0;i<n;i++) {
if(finish[i]==0) {
for(j=0;j<m;j++) {
if(need[i][j] > work[j])
break;
if(j==m) {
for(k=0;k<m;k++)
work[k] += alloc[i][k];
safeSeq[count++] = i;
finish[i] = 1;
found = 1;
if(found==0) {
printf("System is NOT in safe state\n");
return 0;
printf("System is in SAFE state\nSafe sequence: ");
for(i=0;i<n;i++)
printf("P%d ", safeSeq[i]);
return 0;
}
Memory Allocation using Best Fit Algorithm
#include <stdio.h>
int main() {
int blockSize[20], processSize[20];
int allocation[20];
int m, n, i, j, bestIdx;
printf("Enter number of memory blocks: ");
scanf("%d", &m);
printf("Enter size of each block:\n");
for(i = 0; i < m; i++)
scanf("%d", &blockSize[i]);
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter size of each process:\n");
for(i = 0; i < n; i++)
scanf("%d", &processSize[i]);
for(i = 0; i < n; i++)
allocation[i] = -1;
for(i = 0; i < n; i++) {
bestIdx = -1;
for(j = 0; j < m; j++) {
if(blockSize[j] >= processSize[i]) {
if(bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
bestIdx = j;
}
}
if(bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for(i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i+1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}
Disk Scheduling Algorithms – FCFS & SSTF
Code – FCFS
#include <stdio.h>
#include <stdlib.h>
int main()
int n, head, i;
printf("Enter number of disk requests: ");
scanf("%d", &n);
int req[n];
printf("Enter disk request queue:\n");
for(i = 0; i < n; i++) scanf("%d", &req[i]);
printf("Enter initial head position: ");
scanf("%d", &head);
int total = 0;
int cur = head;
printf("\nFCFS Seek Sequence: %d ", head);
for(i = 0; i < n; i++)
{
total += abs(req[i] - cur);
cur = req[i];
printf("-> %d ", cur);
printf("\nTotal Head Movement = %d", total);
return 0;
Code – SSTF
#include <stdio.h>
#include <stdlib.h>
int main()
int n, head, i, j;
printf("Enter number of disk requests: ");
scanf("%d", &n);
int req[n], visited[n];
printf("Enter disk request queue:\n");
for(i = 0; i < n; i++) { scanf("%d", &req[i]); visited[i] = 0; }
printf("Enter initial head position: ");
scanf("%d", &head);
int total = 0;
int cur = head;
printf("\nSSTF Seek Sequence: %d ", head);
for(i = 0; i < n; i++)
int min = 100000, index = -1;
for(j = 0; j < n; j++)
if(!visited[j] && abs(req[j] - cur) < min)
min = abs(req[j] - cur);
index = j;
visited[index] = 1;
total += abs(req[index] - cur);
cur = req[index];
printf("-> %d ", cur);
printf("\nTotal Head Movement = %d", total);
return 0;
}
Output – FCFS
Enter number of disk requests: 8
Enter disk request queue:
95 180 34 119 11 123 62 64
Enter initial head position: 50
FCFS Seek Sequence: 50 -> 95 -> 180 -> 34 -> 119 -> 11 -> 123 -> 62 -> 64
Total Head Movement = 644
Output – SSTF
Enter number of disk requests: 8
Enter disk request queue: 95 180 34 119 11 123 62 64
Enter initial head position: 50
SSTF Seek Sequence: 50 -> 62 -> 64 -> 34 -> 11 -> 95 -> 119 -> 123 -> 180
Total Head Movement = 236
Explore Internal and External Commands of Linux
# 1. pwd – Display current working directory
$ pwd
/home/ubuntu/Documents
# 2. mkdir – Create a new directory
$ mkdir demo
# 3. cd – Change current directory
$ cd demo
# 4. touch – Create a new empty file
$ touch [Link]
# 5. ls – List files in current directory
$ ls
[Link]
# 6. cat – Create file and enter content
$ cat > [Link]
Hello World
Welcome to OS LAB
# 7. cat – Display content of a file
$ cat [Link]
Hello World
Welcome to OS LAB
# 8. cat – Append data to a file
$ cat >> [Link]
Thank you
# 9. mv – Rename and move a file
$ mv [Link] [Link]
$ mkdir demo2
$ mv [Link] demo2/
$ ls demo2
[Link]
# 10. grep – Search for pattern in file
$ grep "hello" [Link]
Hello World
$ grep -i "hello" [Link]
Hello World
# 11. rm – Delete a file
$ rm [Link]
# 12. date – Display current system date and time
$ date
Output
$ pwd /home/ubuntu/Documents
$ ls [Link]
$ cat [Link]
Hello World
Welcome to OS LAB
Thank you
$ ls demo2 [Link]
$ grep -i "hello" [Link]
Hello World
$ date
Thu Feb 5 16:56:19 UTC 2026
Shell Script for Arithmetic Operations
Code – Basic Arithmetic
#!/bin/bash
echo "Enter first Number"
read num1
echo "Enter Second Number"
read num2
result=$((num1 + num2))
result1=$((num1 - num2))
result2=$((num1 * num2))
result3=$((num1 / num2))
echo "Addition: $result"
echo "Subtraction: $result1"
echo "Multiplication: $result2"
echo "Division: $result3"
Code – Using Switch Case
#!/bin/bash
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
echo "Choose Operation:"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read choice
case $choice in
1)
result=$((num1 + num2))
echo "Addition: $result"
;;
2)
result=$((num1 - num2))
echo "Subtraction: $result"
;;
3)
result=$((num1 * num2))
echo "Multiplication: $result"
;;
4)
if [ $num2 -eq 0 ]; then
echo "Error: Division by zero not allowed"
else
result=$((num1 / num2))
echo "Division: $result"
;;
*)
echo "Invalid choice"
;;
esac
Output
Enter first Number 5 Enter Second Number 1 Addition: 6 Subtraction: 4 Multiplication: 5 Division: 5
--- (Switch Case) --- Enter first number: 4 Enter second number: 5 Choose Operation: 1. Addition 2.
Subtraction 3. Multiplication 4. Division 1 Addition: 9
Enter first Number
Enter Second Number
Addition: 6
Subtraction: 4
Multiplication: 5
Division: 5
--- (Switch Case) ---
Enter first number: 4
Enter second number: 5
Choose Operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
1 Addition: 9