Os Lab Experiments Br23
Os Lab Experiments Br23
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
EXP NO: 6
EXP NO:7
EXP NO: 8