Os Lab Manual
Os Lab Manual
Process Management
Networking
#!/bin/ba
echo "Hello, World!"
Looping in ell
#!/bin/ba
for i in {1..5}
do
echo "Iteration $i"
done
If-Else Conditions
#!/bin/ba
echo "Enter a number:"
read num
if [ $num -gt 0 ]; then
echo "Positive number"
else
echo "Negative number or zero"
fi
EXPERIMENT NO: 2
Write programs using the following UNIX operating system calls fork, exec,
getpid, exit, wait, close,stat, opendir and readdir.
1. fork()
Definition:
The fork() system call creates a new child process that runs concurrently with the parent. The
child process gets a copy of the parent’s memory space.
Program:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork(); // Create a child process
if (pid > 0) {
printf("Parent Process: PID = %d, Child PID = %d\n", getpid(), pid);
} else if (pid == 0) {
printf("Child Process: PID = %d, Parent PID = %d\n", getpid(),
getppid());
} else {
printf("Fork failed!\n");
}
return 0;
}
Output:
Parent Process: PID = 1234, Child PID = 1235
Child Process: PID = 1235, Parent PID = 1234
2. exec()
Definition:
The exec() family of functions replaces the current process image with a new process image.
Program (execl())
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Before exec()\n");
execl("/bin/ls", "ls", "-l", NULL); // Replace process with ls command
printf("This will not be printed if exec is successful.\n");
return 0;
}
3. getpid()
Definition:
The getpid() system call returns the process ID of the calling process.
Program:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Process ID: %d\n", getpid());
return 0;
}
Expected Output:
Process ID: 2345
4. exit()
Definition:
The exit() function terminates a process and returns a status to the operating system.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Exiting the program...\n");
exit(0);
}
Expected Output:
Exiting the program...
5. wait()
Definition:
The wait() system call makes the parent process wait until one of its child processes terminates.
Program:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid > 0) {
int status;
wait(&status); // Parent waits for child
printf("Parent process: Child exited with status %d\n",
WEXITSTATUS(status));
} else if (pid == 0) {
printf("Child process: Executing...\n");
exit(5);
}
return 0;
}
Output:
Child process: Executing...
Parent process: Child exited with status 5
6. close()
Definition:
The close() system call closes an open file descriptor.
Program:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("testfile.txt", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
printf("Error opening file!\n");
return 1;
}
printf("File opened with descriptor %d\n", fd);
close(fd);
printf("File descriptor %d closed.\n", fd);
return 0;
}
Output:
File opened with descriptor 3
File descriptor 3 closed.
7. stat()
Definition:
The stat() function retrieves information about a file, such as its size, permissions, and last
modification time.
Program:
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat fileStat;
if (stat("testfile.txt", &fileStat) == 0) {
printf("File Size: %ld bytes\n", fileStat.st_size);
printf("Permissions: %o\n", fileStat.st_mode & 0777);
} else {
printf("Error getting file information.\n");
}
return 0;
}
Expected Output:
File Size: 1024 bytes
Permissions: 644
Definition:
The opendir() function opens a directory, and readdir() reads entries from the directory.
Program:
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *d = opendir("."); // Open current directory
if (d == NULL) {
printf("Error opening directory.\n");
return 1;
}
closedir(d);
return 0;
}
Expected Output:
Files in current directory:
.
..
file1.txt
file2.c
a.out
EXPERIMENT NO:3
AIM:Simulate UNIX commands like cp, ls, grep, etc.,
Description:
The cp command copies the contents of one file to another. This program achieves that using
UNIX system calls like open(), read(), and write().
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
return 0;
}
gcc cp_simulation.c -o cp_sim
./cp_sim source.txt destination.txt
Description:
The ls command lists all files and directories inside a specified directory. This program uses
opendir() and readdir() system calls to read and display the directory contents.
Code:
c
CopyEdit
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
// Open directory
dir = opendir(dirPath);
if (dir == NULL) {
perror("Error opening directory");
return 1;
}
// Close directory
closedir(dir);
return 0;
}
gcc ls_simulation.c -o ls_sim
./ls_sim
To list files in a specific directory:
./ls_sim /home/user/Documents
Description:
The grep command searches for a specific word or phrase inside a file and displays matching
lines. This program achieves that using fgets() and strstr().
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char line[MAX_LINE];
int lineNum = 0;
fclose(file);
return 0;
}
EXP NO: 4
EXP NO: 5
a)mvt b)mft
EXP NO: 6
a)FIFO:
Aim:
DESCRIPTION:
PROGRAM:
OUTPUT:
a) LRU:
Aim: Write a C Program to implement LRU Page Replacement
algorithms
LRU stands for Least Recently Used. As the name suggests, this
algorithm is based on the strategy that whenever a page fault occurs, the
least recently used page will be replaced with a new page. So, the page not
utilized for the longest time in the memory (compared to all other pages)
gets replaced. This strategy is known as LRU paging.
#include <stdio.h>
int main()
{
int i, j, k, min, rs[25], m[10], count[10], n, f, pf = 0, next = 1, pos;
// Input: Length of reference string
printf("Enter the length of reference string: ");
scanf("%d", &n);
if (flag == 0)
printf("PF No. %d", pf);
printf("\n");
}
// Display total page faults
printf("\nTotal number of page faults: %d\n", pf);
return 0;
}
EXERCISE-8
AIM: Simulate the Banker’s algorithm for Dead Lock Avoidance
DESCRIPTION:
Banker’s Algorithm
PROGRAM:
/*C Program to implement Dead Lock Avoidance using Banker’s
algorithm*/
#include<stdio.h>
int main() {
printf("%d\t", need[i][j]);
printf("\n");
/* once process execute variable done will stop them for again execution */
done[i] = 0;
}
if (done[i] == 0) {
break;
//when need matrix is not greater then available matrix then if j==c will
true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in
available resources */
available[j] += alc[i][j];
count++;
terminate = 0;
} else {
terminate++;
if (terminate == (p - 1)) {
break;
}
if (terminate != (p - 1)) {
printf("%d\t", available[i]);
printf("p%d\t", safe[i]);
return 0;
}
EXPERIMENT NO:9
1. Worst-Fit Algorithm:
The worst-fit algorithm allocates the largest block available that can accommodate the process.
This may leave the largest remaining fragments after allocation, potentially leading to inefficient
memory usage.
#include<stdio.h>
#define max 25
void main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d", &nb);
printf("Enter the number of files:");
scanf("%d", &nf);
Input:
Enter the number of blocks: 3
Enter the number of files: 2
Output:
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
2. Best-Fit Algorithm:
The best-fit algorithm allocates the smallest available block that can accommodate the process.
This results in minimal unused space but can create very small fragments.
#include<stdio.h>
#define max 25
void main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
static int bf[max], ff[max];
Input:
Enter the number of blocks: 3
Enter the number of files: 2
Output:
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
3. First-Fit Algorithm:
The first-fit algorithm allocates the first block that is large enough to fit the process.
#include<stdio.h>
#define max 25
void main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
Input:
Enter the number of blocks: 3
Enter the number of files: 2
Output: