[go: up one dir, main page]

0% found this document useful (0 votes)
25 views11 pages

Os Lab Experiments Br23

The document outlines various experiments focused on practicing basic UNIX commands, shell scripting, and simulating UNIX commands and system calls. It includes detailed descriptions and code examples for commands like fork, exec, and file management operations, as well as simulations for cp, ls, and grep commands. Additionally, it mentions experiments related to CPU scheduling algorithms, memory management, file allocation strategies, and deadlock avoidance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Os Lab Experiments Br23

The document outlines various experiments focused on practicing basic UNIX commands, shell scripting, and simulating UNIX commands and system calls. It includes detailed descriptions and code examples for commands like fork, exec, and file management operations, as well as simulations for cp, ls, and grep commands. Additionally, it mentions experiments related to CPU scheduling algorithms, memory management, file allocation strategies, and deadlock avoidance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

EXPERIMENT - 1

AIM: PRACTICING BASIC UNIX COMMANDS AND SHELL PROGRAMS

1. Basic UNIX Commands

File and Directory Management

 pwd – Print working directory


 ls -l – List files with details
 cd <dir> – Change directory
 mkdir <dir> – Create a directory
 rmdir <dir> – Remove an empty directory
 rm -r <dir> – Remove a directory with contents
 cp <source> <destination> – Copy a file
 mv <source> <destination> – Move/Rename a file
 rm <file> – Delete a file

File Viewing & Editing

 cat <file> – Display file contents


 less <file> – View file page by page
 head -n <file> – Display first n lines
 tail -n <file> – Display last n lines
 nano <file> or vim <file> – Edit a file

Permissions & Ownerip

 chmod 755 <file> – Change file permissions


 chown user:group <file> – Change file owner
 ls -l – Check file permissions

Process Management

 ps aux – List running processes


 top – Monitor system processes
 kill <PID> – Terminate a process
 fg – Bring a background process to foreground
 bg – Resume a process in the background

Networking

 ping <host> – Check network connectivity


 curl <URL> – Fetch webpage content
 wget <URL> – Download a file
Disk & Memory Management

 df -h – Check disk space


 du - <dir> – Check directory size
 free -m – Check memory usage

2. ell Scripting Practice

Basic ell Script

Write a script to print "Hello, World!"

#!/bin/ba
echo "Hello, World!"

Run: chmod +x script. && ./script.

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;
}

Expected Output (Directory Listing):


Before exec()
total 12
-rwxr-xr-x 1 user user 1234 Feb 07 12:00 a.out
-rw-r--r-- 1 user user 5678 Feb 07 12:01 program.c

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

8. opendir() and readdir()

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;
}

struct dirent *dir;


printf("Files in current directory:\n");
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}

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.,

1. Simulating cp (Copy File)

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>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}

int src_fd, dest_fd;


char buffer[BUFFER_SIZE];
ssize_t bytesRead;

// Open source file in read-only mode


src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("Error opening source file");
return 1;
}

// Open destination file in write mode (create if doesn't exist, truncate


if exists)
dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_fd < 0) {
perror("Error opening destination file");
close(src_fd);
return 1;
}

// Read from source and write to destination


while ((bytesRead = read(src_fd, buffer, BUFFER_SIZE)) > 0) {
write(dest_fd, buffer, bytesRead);
}
// Close file descriptors
close(src_fd);
close(dest_fd);
printf("File copied successfully.\n");

return 0;
}
gcc cp_simulation.c -o cp_sim
./cp_sim source.txt destination.txt

2. Simulating ls (List Directory Contents)

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>

int main(int argc, char *argv[]) {


DIR *dir;
struct dirent *entry;

// Use current directory if no argument is provided


char *dirPath = (argc > 1) ? argv[1] : ".";

// Open directory
dir = opendir(dirPath);
if (dir == NULL) {
perror("Error opening directory");
return 1;
}

// Read directory entries


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

// 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

3. Simulating grep (Search for a Pattern in a File)

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>

#define MAX_LINE 1024

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <pattern> <file>\n", argv[0]);
return 1;
}

FILE *file = fopen(argv[2], "r");


if (file == NULL) {
perror("Error opening file");
return 1;
}

char line[MAX_LINE];
int lineNum = 0;

// Read file line by line


while (fgets(line, MAX_LINE, file) != NULL) {
lineNum++;
if (strstr(line, argv[1]) != NULL) {
printf("Line %d: %s", lineNum, line);
}
}

fclose(file);
return 0;
}

Compile & Run:


gcc grep_simulation.c -o grep_sim
./grep_sim "pattern" filename.txt

This searches for "pattern" inside filename.txt

EXP NO: 4

Simulate the following CPU scheduling algorithms

a) FCFS b) SJF c) Priority d) Round Robin

EXP NO: 5

Simulate Paging Technique of memory management

EXP NO: 6

Simulate the following file allocation strategies

a) Sequential b) Indexed c) Linked

EXP NO:7

Simulate the following page replacement algorithms

a) FIFO b) LRU c) LFU

EXP NO: 8

Implement Bankers Algorithm for Dead Lock avoidance

You might also like