0% found this document useful (0 votes)
12 views23 pages

6 15 3 5output

Uploaded by

Renee Winters
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)
12 views23 pages

6 15 3 5output

Uploaded by

Renee Winters
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/ 23

3

4 5
Experiment 11

Write a program for process creation using C. (Use of gcc compiler).

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <unistd.h>
4. #include <sys/types.h>
5. #include <sys/wait.h>
6.
7. int main() {
8. pid_t pid;
9.
10. // Create a child process
11. pid = fork();
12.
13. if (pid < 0) {
14. // Fork failed
15. fprintf(stderr, "Failed to create child process\n");
16. return 1;
17. } else if (pid == 0) {
18. // Child process
19. printf("Child process: Hello! I am the child process.\n");
20. exit(0);
21. } else {
22. // Parent process
23. wait(NULL); // Wait for the child process to complete
24. printf("Parent process: Child process has completed.\n");
25. printf("Parent process: Goodbye!\n");
26. return 0;
27. }
28.}
Experiment 12

Write a script to reverse a number and string given by user

#!/bin/bash

reverseNumber() {
local num=$1
local reversedNum=0

while [[ $num -gt 0 ]]; do


remainder=$((num % 10))
reversedNum=$((reversedNum * 10 + remainder))
num=$((num / 10))
done

echo "$reversedNum"
}

reverseString() {
local str=$1
local reversedStr=""

for (( i=${#str}-1; i>=0; i-- )); do


reversedStr="${reversedStr}${str:i:1}"
done

echo "$reversedStr"
}

echo -n "Enter a number: "


read number

reversedNumber=$(reverseNumber "$number")
echo "Reversed number: $reversedNumber"

echo -n "Enter a string: "


read string

reversedString=$(reverseString "$string")
echo "Reversed string: $reversedString"

Output
Experiment 13

Write a script to nd the smallest of three numbers as well as largest among


three
29..
#!/bin/bash
echo -n "Enter the rst number: "
read num1
echo -n "Enter the second number: "
read num2
echo -n "Enter the third number: "
read num3

smallest=$num1
if (( num2 < smallest )); then
smallest=$num2

if (( num3 < smallest )); then


smallest=$num3

echo "Smallest number: $smallest”

largest=$num1

if (( num2 > largest )); then


largest=$num2

if (( num3 > largest )); then


largest=$num3

echo "Largest number: $largest"

Output
fi
fi
fi
fi
fi
fi
Experiment 14

Write script that prints names of all sub directories present in the current
directory.

#!/bin/bash

# Loop through all items in the current directory


for item in *; do
# Check if the item is a directory
if [[ -d "$item" ]]; then
echo "$item"

done

Output
fi
Experiment 15

Allocate/free memory to processes in whole pages, nd max allocatable


pages,
incorporate address translation into the program.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>

#de ne PAGE_SIZE 4096

int main() {
// Determine the maximum allocatable pages
long max_pages = sysconf(_SC_AVPHYS_PAGES);
printf("Maximum allocatable pages: %ld\n", max_pages);

// Allocate memory for one page


void *mem = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANonymous, -1, 0);
if (mem == MAP_FAILED) {
perror("Failed to allocate memory");
return 1;
}
printf("Memory allocated at address: %p\n", mem);

// Translate the virtual address to physical address


int fd = open("/proc/self/pagemap", O_RDONLY);
if (fd < 0) {
perror("Failed to open pagemap");
return 1;
}

o _t o set = (o _t)mem / PAGE_SIZE * sizeof(uint64_t);


uint64_t entry;
if (lseek(fd, o set, SEEK_SET) != o set || read(fd, &entry, sizeof(entry)) != sizeof(entry)) {
perror("Failed to read pagemap");
return 1;
}

uint64_t page_frame_number = entry & ((1ULL << 54) - 1);


uint64_t physical_address = page_frame_number * PAGE_SIZE;
printf("Physical address: %lx\n", physical_address);

// Free the allocated memory


ff
fi
ff
ff
ff
ff
fi
if (munmap(mem, PAGE_SIZE) == -1) {
perror("Failed to free memory");
return 1;
}

return 0;
}
Experiment 7

Simulate all File Organization Techniques a) Single level directory b) Two level
directory

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#de ne MAX_FILES 100


#de ne MAX_NAME_LENGTH 50

typedef struct {
char name[MAX_NAME_LENGTH];
int location;
} File;

typedef struct {
char name[MAX_NAME_LENGTH];
int num_ les;
File les[MAX_FILES];
} Directory;

Directory singleLevelDirectory;
Directory twoLevelDirectory[MAX_FILES];

void initializeDirectories() {
singleLevelDirectory.num_ les = 0;
for (int i = 0; i < MAX_FILES; i++) {
twoLevelDirectory[i].num_ les = 0;
}
}

void addFileSingleLevel(char* name, int location) {


if (singleLevelDirectory.num_ les < MAX_FILES) {
File le;
strcpy( le.name, name);
le.location = location;
singleLevelDirectory. les[singleLevelDirectory.num_ les] = le;
singleLevelDirectory.num_ les++;
printf("File '%s' added to Single Level Directory.\n", name);
} else {
printf("Single Level Directory is full. Cannot add le '%s'.\n", name);
}
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
void addFileTwoLevel(char* name, int location, int directoryIndex) {
if (directoryIndex < MAX_FILES &&
twoLevelDirectory[directoryIndex].num_ les < MAX_FILES) {
File le;
strcpy( le.name, name);
le.location = location;

twoLevelDirectory[directoryIndex]. les[twoLevelDirectory[directoryIndex].nu
m_ les] = le;
twoLevelDirectory[directoryIndex].num_ les++;
printf("File '%s' added to Directory %d in Two Level Directory.\n",
name, directoryIndex);
} else {
printf("Directory %d in Two Level Directory is full. Cannot add le '%s'.
\n", directoryIndex, name);
}
}

void printSingleLevelDirectory() {
printf("\nSingle Level Directory:\n");
for (int i = 0; i < singleLevelDirectory.num_ les; i++) {
printf("File Name: %s, Location: %d\n",
singleLevelDirectory. les[i].name, singleLevelDirectory. les[i].location);
}
}

void printTwoLevelDirectory() {
printf("\nTwo Level Directory:\n");
for (int i = 0; i < MAX_FILES; i++) {
if (twoLevelDirectory[i].num_ les > 0) {
printf("Directory %d:\n", i);
for (int j = 0; j < twoLevelDirectory[i].num_ les; j++) {
printf("File Name: %s, Location: %d\n",
twoLevelDirectory[i]. les[j].name, twoLevelDirectory[i]. les[j].location);
}
}
}
}

int main() {
initializeDirectories();

// Single Level Directory


addFileSingleLevel("File1.txt", 100);
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
addFileSingleLevel("File2.txt", 200);
addFileSingleLevel("File3.txt", 300);
printSingleLevelDirectory();

// Two Level Directory


addFileTwoLevel("File4.txt", 400, 0);
addFileTwoLevel("File5.txt", 500, 0);
addFileTwoLevel("File6.txt", 600, 1);
addFileTwoLevel("File7.txt", 700, 1);
printTwoLevelDirectory();

return 0;
}
Experiment 8

Simulate all le allocation strategies a) Sequential b) Indexed c) Linked.

a) Sequential File Allocation:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#de ne MAX_FILES 100


#de ne MAX_NAME_LENGTH 50

typedef struct {
char name[MAX_NAME_LENGTH];
int start_block;
int length;
} File;

File les[MAX_FILES];
int num_ les = 0;

void addFileSequential(char* name, int length) {


if (num_ les < MAX_FILES) {
File le;
strcpy( le.name, name);
le.length = length;

if (num_ les == 0) {
le.start_block = 0;
} else {
le.start_block = les[num_ les - 1].start_block + les[num_ les - 1].length;
}

les[num_ les] = le;


num_ les++;
printf("File '%s' added using Sequential File Allocation.\n", name);
} else {
printf("Maximum number of les reached. Cannot add le '%s'.\n", name);
}
}

void printSequentialAllocation() {
printf("\nSequential File Allocation:\n");
for (int i = 0; i < num_ les; i++) {
printf("File Name: %s, Start Block: %d, Length: %d\n", les[i].name,
les[i].start_block, les[i].length);
}
}

int main() {
addFileSequential("File1.txt", 100);
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
addFileSequential("File2.txt", 200);
addFileSequential("File3.txt", 150);
printSequentialAllocation();

return 0;
}

b) Indexed File Allocation:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#de ne MAX_FILES 100


#de ne MAX_NAME_LENGTH 50
#de ne MAX_INDEX_BLOCKS 10

typedef struct {
char name[MAX_NAME_LENGTH];
int index_blocks[MAX_INDEX_BLOCKS];
int num_index_blocks;
} File;

File les[MAX_FILES];
int num_ les = 0;

void addFileIndexed(char* name, int* index_blocks, int num_index_blocks) {


if (num_ les < MAX_FILES) {
File le;
strcpy( le.name, name);
le.num_index_blocks = num_index_blocks;
memcpy( le.index_blocks, index_blocks, num_index_blocks * sizeof(int));

les[num_ les] = le;


num_ les++;
printf("File '%s' added using Indexed File Allocation.\n", name);
} else {
printf("Maximum number of les reached. Cannot add le '%s'.\n", name);
}
}

void printIndexedAllocation() {
printf("\nIndexed File Allocation:\n");
for (int i = 0; i < num_ les; i++) {
printf("File Name: %s, Index Blocks: ", les[i].name);
for (int j = 0; j < les[i].num_index_blocks; j++) {
printf("%d ", les[i].index_blocks[j]);
}
printf("\n");
}
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
int main() {
int blocks1[] = {1, 2, 3};
addFileIndexed("File1.txt", blocks1, sizeof(blocks1) / sizeof(blocks1[0]));

int blocks2[] = {4, 5, 6, 7};


addFileIndexed("File2.txt", blocks2, sizeof(blocks2) / sizeof(blocks2[0]));

int blocks3[] = {8};


addFileIndexed("File3.txt", blocks3, sizeof(blocks3) / sizeof(blocks3[0]));

printIndexedAllocation();

return 0;
}

c) Linked File Allocation:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#de ne MAX_FILES 100


#de ne MAX_NAME_LENGTH 50

typedef struct {
char name[MAX_NAME_LENGTH];
int start_block;
} File;

typedef struct {
int block_number;
int next_block;
} Block;

File les[MAX_FILES];
Block blocks[MAX_FILES];
int num_ les = 0;
int num_blocks = 0;

void addFileLinked(char* name, int num_blocks) {


if (num_ les < MAX_FILES && num_blocks <= MAX_FILES - num_blocks) {
File le;
strcpy( le.name, name);
le.start_block = num_blocks;

les[num_ les] = le;


num_ les++;

for (int i = num_blocks; i < num_blocks + num_blocks; i++) {


Block block;
block.block_number = i;
block.next_block = i + 1;
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
blocks[i] = block;
}
blocks[num_blocks + num_blocks - 1].next_block = -1;

num_blocks += num_blocks;

printf("File '%s' added using Linked File Allocation.\n", name);


} else {
printf("Maximum number of les or blocks reached. Cannot add le '%s'.\n", name);
}
}

void printLinkedAllocation() {
printf("\nLinked File Allocation:\n");
for (int i = 0; i < num_ les; i++) {
printf("File Name: %s\n", les[i].name);
printf("Blocks: ");
int current_block = les[i].start_block;
while (current_block != -1) {
printf("%d ", current_block);
current_block = blocks[current_block].next_block;
}
printf("\n");
}
}

int main() {
addFileLinked("File1.txt", 3);
addFileLinked("File2.txt", 5);
addFileLinked("File3.txt", 2);
printLinkedAllocation();

return 0;
}
fi
fi
fi
fi
fi
Experiment 9

Write a script to translate the string from capital letters to small and small
letters to capital using awk command.

#!/bin/bash

input_string="Hello World"

translated_string=$(echo "$input_string" | awk '{


for (i = 1; i <= length($0); i++) {
char = substr($0, i, 1)
if (char ~ /[[:upper:]]/) {
char = tolower(char)
} else if (char ~ /[[:lower:]]/) {
char = toupper(char)
}
printf "%s", char
}
printf "\n"
}')

echo "Translated String: $translated_string"

Output
Experiment 10

Write a script to do the sorting of given numbers (use command line


argument).

#!/bin/bash

# Check if there are command-line arguments


if [ $# -eq 0 ]; then
echo "No numbers provided. Please provide a list of numbers as command-line
arguments."
exit 1

# Store the numbers in an array


numbers=("$@")

# Sort the numbers using bubble sort algorithm


for (( i = 0; i < ${#numbers[@]} - 1; i++ )); do
for (( j = 0; j < ${#numbers[@]} - i - 1; j++ )); do
if (( numbers[j] > numbers[j+1] )); then
# Swap the numbers
temp=${numbers[j]}
numbers[j]=${numbers[j+1]}
numbers[j+1]=$temp

done
done

# Print the sorted numbers


echo "Sorted Numbers:"
for number in "${numbers[@]}"; do
echo "$number"
done

Output
fi
fi
Experiment 6

Simulate all page replacement algorithms a)FIFO b) LRU c) OPTIMAL

a. FIFO

#include <stdio.h>

#de ne MAX_FRAMES 3
#de ne MAX_PAGES 20

int fo(int pages[], int num_pages) {


int frames[MAX_FRAMES] = { -1 };
int page_faults = 0;
int next_frame = 0;

for (int i = 0; i < num_pages; i++) {


int current_page = pages[i];
int is_page_fault = 1;

// Check if the page is already in a frame


for (int j = 0; j < MAX_FRAMES; j++) {
if (frames[j] == current_page) {
is_page_fault = 0;
break;
}
}

// Page fault occurred, replace the oldest page


if (is_page_fault) {
frames[next_frame] = current_page;
next_frame = (next_frame + 1) % MAX_FRAMES;
page_faults++;
}

// Print the current state of frames


printf("Page: %2d Frames: ", current_page);
for (int j = 0; j < MAX_FRAMES; j++) {
printf("%2d ", frames[j]);
}
printf("\n");
}

return page_faults;
}

int main() {
int pages[MAX_PAGES] = {1, 3, 0, 3, 5, 6, 3, 7, 8, 3, 6, 8, 9, 1, 0, 3, 6, 2, 3, 0};
int num_pages = sizeof(pages) / sizeof(pages[0]);

printf("FIFO Page Replacement Algorithm\n");


fi
fi
fi
printf("-------------------------------\n");

int page_faults = fo(pages, num_pages);

printf("\nTotal Page Faults: %d\n", page_faults);

return 0;
}
fi
B. LRU

#include <stdio.h>

#de ne MAX_FRAMES 3
#de ne MAX_PAGES 20

int ndLRU(int frames[], int counter[], int num_frames) {


int min_counter = counter[0];
int lru_page_index = 0;

for (int i = 1; i < num_frames; i++) {


if (counter[i] < min_counter) {
min_counter = counter[i];
lru_page_index = i;
}
}

return lru_page_index;
}

int lru(int pages[], int num_pages) {


int frames[MAX_FRAMES] = { -1 };
int counter[MAX_FRAMES] = { 0 };
int page_faults = 0;
int counter_value = 1;

for (int i = 0; i < num_pages; i++) {


int current_page = pages[i];
int is_page_fault = 1;

// Check if the page is already in a frame


for (int j = 0; j < MAX_FRAMES; j++) {
if (frames[j] == current_page) {
is_page_fault = 0;
counter[j] = counter_value;
break;
}
}

// Page fault occurred, replace the least recently used page


if (is_page_fault) {
int lru_page_index = ndLRU(frames, counter, MAX_FRAMES);
frames[lru_page_index] = current_page;
counter[lru_page_index] = counter_value;
page_faults++;
}

counter_value++;

// Print the current state of frames


fi
fi
fi
fi
printf("Page: %2d Frames: ", current_page);
for (int j = 0; j < MAX_FRAMES; j++) {
printf("%2d ", frames[j]);
}
printf("\n");
}

return page_faults;
}

int main() {
int pages[MAX_PAGES] = {1, 3, 0, 3, 5, 6, 3, 7, 8, 3, 6, 8, 9, 1, 0, 3, 6, 2, 3, 0};
int num_pages = sizeof(pages) / sizeof(pages[0]);

printf("LRU Page Replacement Algorithm\n");


printf("------------------------------\n");

int page_faults = lru(pages, num_pages);

printf("\nTotal Page Faults: %d\n", page_faults);

return 0;
}
C. Optimal

#include <stdio.h>
#include <limits.h>

#de ne MAX_FRAMES 3
#de ne MAX_PAGES 20

int ndOptimal(int pages[], int frames[], int start_index, int num_frames, int
num_pages) {
int optimal_index = -1;
int farthest_page_index = -1;

for (int i = 0; i < num_frames; i++) {


int page = frames[i];
int found = 0;

for (int j = start_index; j < num_pages; j++) {


if (pages[j] == page) {
found = 1;
if (j > farthest_page_index) {
farthest_page_index = j;
optimal_index = i;
}
break;
}
}

if (!found) {
return i;
}
}

return optimal_index;
}

int optimal(int pages[], int num_pages) {


int frames[MAX_FRAMES] = { -1 };
int page_faults = 0;

for (int i = 0; i < num_pages; i++) {


int current_page = pages[i];
int is_page_fault = 1;

// Check if the page is already in a frame


fi
fi
fi
for (int j = 0; j < MAX_FRAMES; j++) {
if (frames[j] == current_page) {
is_page_fault = 0;
break;
}
}

// Page fault occurred, replace the page with optimal choice


if (is_page_fault) {
int frame_index = ndOptimal(pages, frames, i + 1, MAX_FRAMES,
num_pages);
frames[frame_index] = current_page;
page_faults++;
}

// Print the current state of frames


printf("Page: %2d Frames: ", current_page);
for (int j = 0; j < MAX_FRAMES; j++) {
printf("%2d ", frames[j]);
}
printf("\n");
}

return page_faults;
}

int main() {
int pages[MAX_PAGES] = {1, 3, 0, 3, 5, 6, 3, 7, 8, 3, 6, 8, 9, 1, 0, 3, 6, 2, 3,
0};
int num_pages = sizeof(pages) / sizeof(pages[0]);

printf("Optimal Page Replacement Algorithm\n");


printf("---------------------------------\n");

int page_faults = optimal(pages, num_pages);

printf("\nTotal Page Faults: %d\n", page_faults);

return 0;
}
fi

You might also like