AOS Pratical File3
AOS Pratical File3
Theory-
Unix commands are instructions that are used to interact with the Unix operating system. They are
used to perform various tasks, such as navigating the file system, managing files and directories, and
executing programs.
2. External Commands: Programs that are stored in files and executed by the shell.
3. Arguments: Files, directories, or other data that the command operates on.
Key Concepts
2. Terminal: The interface through which users interact with the shell.
All commands will be run as the root user. Example command lines are prefixed with the # symbol
signifying the shell prompt; you should type in only text after the prompt.
Compiling the benchmark
The laboratory I/O benchmark source code has been preinstalled onto your RPi4 board. However, you
will need to build it before you can begin work. Once you have logged into your RPi4 (see Advanced
Operating Systems: Lab Setup), build the bundle: # make - C io
Benchmark arguments
If run without any parameters, the benchmark will list its potential arguments and default
settings: usage: io-benchmark -c|-r|-w [-Bdjqsv] [-b buffersize][-n iterations] [-t totalsize] path
Modes (pick one):
-j Output as JSON
-q Just run the benchmark, don’t print stuff out
-s Call fsync() on the file descriptor when complete Provide a
-v verbose benchmark description
ALGORITHM:
1. Initialize semaphores x and y to 1.
2. Reader Process:
o Wait on x and increment readercount.
o If it's the first reader, wait on y to block writers.
o Post x.
o Read data.
o Wait on x, decrement readercount.
o If it's the last reader, post y.
o Post x.
3. Writer Process:
o Wait on y.
o Write data.
o Post y.
PROGRAM CODE:
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
sem_t x, y;
pthread_t tid;
pthread_t writerthreads[100], readerthreads[100];
int readercount = 0;
sem_wait(&x);
readercount--;
if(readercount == 0)
sem_post(&y); // Last reader allows writer
sem_post(&x);
int main()
{
int n, i;
printf("Enter the number of readers and writers: ");
scanf("%d", &n);
sem_init(&x, 0, 1);
sem_init(&y, 0, 1);
return 0;
}
SAMPLE OUTPUT:
Practical No.3
THEORY:
Producer-Consumer Problem is a classic example of a multi-process synchronization problem.
A producer adds items to a buffer, and a consumer removes items.
The buffer has a limited size. Synchronization is required to avoid:
o Overwriting buffer (Producer should wait if the buffer is full)
o Reading empty buffer (Consumer should wait if the buffer is empty)
We use:
mutex – mutual exclusion lock to protect critical sections.
full – counts how many items are in the buffer.
empty – counts how many empty spaces are available.
ALGORITHM:
1. Initialize mutex = 1, full = 0, empty = buffer_size, x = 0
2. Display a menu:
1. Producer
2. Consumer
3. Exit
3. On Producer:
o If mutex == 1 and empty > 0, produce an item.
o Otherwise, buffer is full.
4. On Consumer:
o If mutex == 1 and full > 0, consume an item.
o Otherwise, buffer is empty.
5. Use wait() to decrement and signal() to increment semaphores safely.
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 3; // buffer size
int x = 0; // item counter
int wait(int s) {
return --s;
}
int signal(int s) {
return ++s;
}
void producer() {
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("\nProducer produces item %d", x);
mutex = signal(mutex);
}
void consumer() {
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d", x);
x--;
mutex = signal(mutex);
}
int main() {
int n;
while (1) {
printf("\n\nEnter your choice: ");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("\nBuffer is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("\nBuffer is empty!!");
break;
case 3:
printf("\nExiting...\n");
exit(0);
default:
printf("\nInvalid choice!");
}
}
return 0;
}
OUTPUT:
Practical No. 4
THEORY:
Shared memory is the fastest form of IPC because the data is not copied between processes.
shmget() creates or accesses a shared memory segment.
shmat() attaches the shared memory to the process’s address space.
shmdt() detaches the shared memory.
shmctl() is used to control or delete shared memory.
PROGRAM CODE:
Writer Code (writer.c):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
int main() {
void *shared_memory;
char buff[100];
int shmid;
return 0;
}
int main() {
void *shared_memory;
int shmid;
return 0;
}
OUTPUT:
Writer Terminal:
Reader Terminal:
Practical No. 5
Theory:
A Page Replacement Algorithm is used when a page fault occurs and memory is full.
FIFO (First-In-First-Out) algorithm removes the page that has been in memory the longest.
PROGRAM CODE:
#include <iostream>
#include <deque>
#include <unordered_set>
using namespace std;
int main() {
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(arr) / sizeof(arr[0]);
unordered_set<int> pages_in_memory;
queue<int> page_order;
int page_faults = 0;
page_faults++;
if (pages_in_memory.size() == capacity) {
int oldest_page = page_order.front();
page_order.pop();
pages_in_memory.erase(oldest_page);
}
pages_in_memory.insert(page);
page_order.push(page);
}
// If page is already in memory, do nothing (no page fault)
}
cout << "Total Page Faults: " << page_faults << endl;
return 0;
}
OUTPUT:
Program No. 6
Theory:
Shared Memory is an IPC mechanism where multiple processes can access the same memory segment.
It is one of the fastest IPC techniques.
Common system calls used:
o ftok() – Generates a unique key.
o shmget() – Allocates a shared memory segment.
o shmat() – Attaches the shared memory to the process’s address space.
o shmdt() – Detaches the shared memory.
o shmctl() – Performs control operations (like remove).
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
int main() {
int shmid;
key_t key;
char *segptr;
char buff[] = "poooda……";
// Generate unique key
OUTPUT:
Practical No. 7
Theory:
A Bash script is a plain text file containing a series of commands that the shell executes in sequence. The line
#!/bin/bash at the top (called a shebang) tells the operating system to use the Bash interpreter.
OUTPUT:
var=$((3 + 9))
echo "The sum is: $var"
OUTPUT:
Explanation:
#!/bin/bash – Declares the Bash shell to interpret the script.
echo – Prints output to the terminal.
$((expression)) – Evaluates the arithmetic expression inside.
Practical No. 8
THEORY:
A shell script is a program executed by the command-line shell to automate tasks by running a series of
commands.
Factorial:
The factorial of a number n (written as n!) is the product of all positive integers from 1 to n. It is usually
calculated using loops.
Even and Odd Numbers:
An even number is divisible by 2 with no remainder; an odd number leaves a remainder of 1 when divided by 2.
The modulus operator % is used to check this.
Loops and Conditionals:
Bash uses loops like for and while to repeat commands, and if statements to execute code based on conditions.
PROGRAM CODE:
(a) Program to Find Factorial of a Number
#!/bin/bash
# Script to find factorial of a number
done
echo "Factorial of $num is: $fact"
OUTPUT:
fi
((i++))
Done
OUTPUT:
Practical No. 9
Introduction:
The Transmission Control Protocol (TCP) is a fundamental communication protocol that provides reliable,
ordered, and error-checked delivery of a stream of data between applications running on hosts in an IP network.
TCP achieves this through a complex series of mechanisms including connection setup, data transfer, and
connection termination. Once a TCP connection reaches the ESTABLISHED state, it primarily focuses on
efficient data transfer.
The Benchmark
In this lab, you will run an IPC benchmark over TCP to observe and measure the effect of different socket
buffer sizes on TCP throughput and latency. By running the benchmark both with default buffer sizes and with
manually set socket buffer sizes, you will gain insights into how buffer tuning affects performance.
The benchmark sends data over TCP on a fixed port (10141), so data segments and acknowledgments can
be tracked by their source and destination ports.
Ensure the system's maximum socket buffer sizes are increased to allow for larger buffer settings and
prevent artificial limits on throughput.
Summary
This lab explores the crucial roles of latency and bandwidth in TCP’s operation. By understanding and
measuring TCP’s rate control mechanisms and how they interact with network conditions, you gain valuable
insights into network performance tuning and protocol behavior.