OS Assignment
OS Assignment
i. Partitioning drives
ii. Configuring boot loader (GRUB/LILO)
iii. Network configuration
iv. Setting time zones
v. Creating password and user accounts
vi. Shutting downSol
i. Partitioning drives: During installation, choose automatic or manual
partitioning to organize storage space. Allocate space for root (/), swap, and
optionally /home partitions.
ii. Configuring boot loader (GRUB/LILO): Install GRUB (Grand Unified Boot
Loader) or LILO (Linux Loader) to the Master Boot Record (MBR) or the EFI
System Partition (ESP) to manage the boot process. Choose the appropriate
options based on your system configuration.
iv. Setting time zones: Select the appropriate time zone during installation or
configure it afterward using system utilities like timedatectl or editing the
/etc/timezone file.
v. Creating password and user accounts: Create a root password and at least one
user account with appropriate privileges. Use the adduser or useradd command
to create users, and passwd to set passwords.
vi. Shutting down: After completing the installation and configuration, shut down
the system using the shutdown command with appropriate options (-h for halt, -r
for reboot) or through the graphical user interface.
Q 2. Study of Unix/Linux general purpose utility command list obtained
from(man, who, cat, cd, cp, ps, ls, mv, rm, mkdir, rmdir, echo, more,
date, time,kill, history, chmod, chown, finger, pwd, cal, logout,
shutdown) commands.
The Unix/Linux general-purpose utility commands are essential for various tasks:
1. man: Access manual pages for commands and programs.
2. who: Display information about users currently logged in.
3. cat: Concatenate and display files.
4. cd: Change directory.
5. cp: Copy files and directories.
6. ps: Display information about active processes.
7. ls: List directory contents.
8. mv: Move or rename files and directories.
9. rm: Remove files and directories.
10.mkdir: Create directories.
11.rmdir: Remove directories.
12.echo: Display text on the command line.
13.more: Display text files one screen at a time.
14.date: Display or set the system date and time.
15.time: Measure the time it takes to execute a command.
16.kill: Terminate processes by ID or name.
17.history: Display command history.
18.chmod: Change file permissions.
19.chown: Change file ownership.
20.finger: Display information about users.
21.pwd: Print the current working directory.
22.cal: Display a calendar.
23.logout: Log out of the current session.
24.shutdown: Shut down or reboot the system.
Q 3. Study of vi editor.
The vi editor is a versatile text editor commonly used in Unix and Linux operating
systems. It operates in two primary modes: command mode for executing
commands and insert mode for typing text. Users can navigate, edit, search, and
manipulate text efficiently using vi's extensive command set. It's favored for its
speed, powerful features, and availability across various Unix-based systems,
making it a valuable tool for programmers, system administrators, and users
working with text files.
Bash (Bourne Again Shell) is the default shell for most Unix/Linux
systems.
It's an enhanced version of the original Bourne shell (sh).
Bash supports features like command history, tab completion, and job
control.
It's highly customizable and widely used due to its compatibility with
Bourne shell scripts.
Bourne shell:
The original Unix shell, developed by Stephen Bourne in the 1970s.
It's a simple and lightweight shell, lacking some advanced features found
in modern shells like Bash.
Bourne shell scripts are still used, especially for portability across different
Unix systems.
Its syntax and features are more limited compared to newer shells like
Bash.
C shell:
1. .bashrc:
User-specific Bash configuration file located in the user's home
directory (~/).
Executes each time a new interactive Bash shell is opened for that
user.
Used to customize the shell environment by defining aliases, setting
environment variables, and running other initialization commands
specific to the user's preferences.
2. /etc/bashrc:
System-wide Bash configuration file located in the /etc directory.
Applies settings globally to all users who use the Bash shell on the
system.
Used by system administrators to set default configurations and
environment variables for all users.
3. Environment Variables:
Dynamic named values affecting the behavior of processes in a
Unix/Linux system.
Key-value pairs defining variables like PATH, HOME, USER, and
LANG.
Can be set, accessed, and modified within shell scripts and by using
the 'export' command.
Define aspects of the user's shell environment, including system
behavior, default paths, and user preferences.
#include <stdio.h>
struct Process {
int pid; // Process ID
int arrival_time; // Arrival time of the process
int burst_time; // Burst time (execution time) of the process
};
void fcfs(struct Process processes[], int n) {
int waiting_time = 0;
float average_waiting_time;
printf("Process\t Arrival Time\t Burst Time\t Waiting Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t %d\t\t %d\t\t %d\n", processes[i].pid, processes[i].arrival_time,
processes[i].burst_time, waiting_time);
waiting_time += processes[i].burst_time;
}
average_waiting_time = (float)waiting_time / n;
printf("\nAverage Waiting Time: %.2f\n", average_waiting_time);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i + 1;
}
fcfs(processes, n);
return 0;
}
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}
int main() {
// Input number of processes and resources
printf("Enter number of processes: ");
scanf("%d", &num_processes);
printf("Enter number of resources: ");
scanf("%d", &num_resources);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
pthread_mutex_init(&mutex, NULL); // Initialize the mutex lock
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]); // Create threads
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL); // Wait for threads to finish
}
pthread_mutex_destroy(&mutex); // Destroy the mutex lock
return 0;
}
Q 12.To write a C program for implementation memory allocation
methods for fixed partition using first fit.
#include <stdio.h>
#define MAX_PARTITIONS 10
i. FIFO
#include <stdio.h>
#include <stdbool.h>
#define NUM_FRAMES 3
#define NUM_PAGES 10
int frames[NUM_FRAMES];
int pages[NUM_PAGES];
int page_faults = 0;
// Function to initialize frames
void initializeFrames() {
for (int i = 0; i < NUM_FRAMES; i++) {
frames[i] = -1; // -1 indicates an empty frame
}
}
// Function to check if a page is present in frames
bool isPagePresent(int page) {
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == page) {
return true;
}
}
return false;
}
// Function to display frames
void displayFrames() {
printf("Frames: ");
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == -1) {
printf("- ");
} else {
printf("%d ", frames[i]);
}
}
printf("\n");
}
// Function to implement FIFO page replacement algorithm
void fifo() {
int frame_index = 0; // Index to keep track of the oldest frame
ii. LRU
#include <stdio.h>
#include <stdbool.h>
#define NUM_FRAMES 3
#define NUM_PAGES 10
int frames[NUM_FRAMES];
int pages[NUM_PAGES];
int page_faults = 0;
// Function to initialize frames
void initializeFrames() {
for (int i = 0; i < NUM_FRAMES; i++) {
frames[i] = -1; // -1 indicates an empty frame
}
}
// Function to check if a page is present in frames
bool isPagePresent(int page) {
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == page) {
return true;
}
}
return false;
}
// Function to display frames
void displayFrames() {
printf("Frames: ");
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == -1) {
printf("- ");
} else {
printf("%d ", frames[i]);
}
}
printf("\n");
}
// Function to implement LRU page replacement algorithm
void lru() {
int counter[NUM_FRAMES] = {0}; // Counter to keep track of page usage
for (int i = 0; i < NUM_PAGES; i++) {
int page = pages[i];
if (!isPagePresent(page)) {
// Find the frame with the lowest usage counter (least recently used)
int min_counter = counter[0];
int min_index = 0;
for (int j = 1; j < NUM_FRAMES; j++) {
if (counter[j] < min_counter) {
min_counter = counter[j];
min_index = j;
}
}
frames[min_index] = page; // Replace the least recently used frame with the new page
page_faults++; // Increment page fault count
}
// Update the usage counter for the accessed page
for (int j = 0; j < NUM_FRAMES; j++) {
if (frames[j] != -1 && frames[j] != page) {
counter[j]++;
} else if (frames[j] == page) {
counter[j] = 0; // Reset counter for the accessed page
}
}
displayFrames(); // Display frames after each page access
}
}
int main() {
// Initialize frames
initializeFrames();
iii. LFU
#include <stdio.h>
#include <stdbool.h>
#define NUM_FRAMES 3
#define NUM_PAGES 10
int frames[NUM_FRAMES];
int pages[NUM_PAGES];
int page_faults = 0;
// Structure to represent a page entry in frames
struct PageEntry {
int page;
int frequency;
};
// Function to initialize frames
void initializeFrames() {
for (int i = 0; i < NUM_FRAMES; i++) {
frames[i] = -1; // -1 indicates an empty frame
}
}
// Function to check if a page is present in frames
bool isPagePresent(int page) {
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == page) {
return true;
}
}
return false;
}
// Function to display frames
void displayFrames() {
printf("Frames: ");
for (int i = 0; i < NUM_FRAMES; i++) {
if (frames[i] == -1) {
printf("- ");
} else {
printf("%d ", frames[i]);
}
}
printf("\n");
}
// Function to implement LFU page replacement algorithm
void lfu() {
struct PageEntry page_entries[NUM_FRAMES];
for (int i = 0; i < NUM_FRAMES; i++) {
page_entries[i].page = -1;
page_entries[i].frequency = 0;
}
for (int i = 0; i < NUM_PAGES; i++) {
int page = pages[i];
if (!isPagePresent(page)) {
// Find the page with the least frequency in frames
int min_frequency = page_entries[0].frequency;
int min_index = 0;
for (int j = 1; j < NUM_FRAMES; j++) {
if (page_entries[j].frequency < min_frequency) {
min_frequency = page_entries[j].frequency;
min_index = j;
}
}
frames[min_index] = page; // Replace the page with least frequency with the new
page
page_entries[min_index].page = page;
page_entries[min_index].frequency = 1;
page_faults++; // Increment page fault count
} else {
// Increment the frequency of the accessed page
for (int j = 0; j < NUM_FRAMES; j++) {
if (frames[j] == page) {
page_entries[j].frequency++;
break;
}
}
}
displayFrames(); // Display frames after each page access
}
}
int main() {
// Initialize frames
initializeFrames();
i. Sequential
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_BLOCKS 100
#define MAX_FILENAME_LENGTH 20
struct File {
char filename[MAX_FILENAME_LENGTH];
int start_block;
int num_blocks;
};
int disk[MAX_BLOCKS]; // Array to represent disk blocks
struct File files[MAX_BLOCKS]; // Array to store file information
int num_files = 0; // Number of files stored on disk
// Function to initialize disk
void initializeDisk() {
for (int i = 0; i < MAX_BLOCKS; i++) {
disk[i] = -1; // -1 indicates an empty block
}
}
// Function to display disk
void displayDisk() {
printf("Disk Status:\n");
for (int i = 0; i < MAX_BLOCKS; i++) {
printf("%3d ", i);
}
printf("\n");
for (int i = 0; i < MAX_BLOCKS; i++) {
printf("%3d ", disk[i]);
}
printf("\n");
}
// Function to check if a block is free
bool isBlockFree(int block) {
return disk[block] == -1;
}
// Function to allocate blocks sequentially for a file
void allocateBlocksSequential(char *filename, int num_blocks) {
int start_block = -1;
for (int i = 0; i < MAX_BLOCKS; i++) {
if (isBlockFree(i)) {
if (start_block == -1) {
start_block = i;
}
num_blocks--;
disk[i] = num_files; // Assign the file ID to the block
if (num_blocks == 0) {
break;
}
} else {
start_block = -1; // Reset start block if the sequence is broken
}
}
if (start_block != -1) {
// Store file information
strcpy(files[num_files].filename, filename);
files[num_files].start_block = start_block;
files[num_files].num_blocks = num_blocks + 1;
num_files++;
printf("File %s allocated at blocks %d-%d\n", filename, start_block, start_block +
files[num_files - 1].num_blocks - 1);
} else {
printf("Error: Insufficient free blocks for file allocation\n");
}
}
int main() {
initializeDisk();
// Example: Allocate files
allocateBlocksSequential("file1", 3);
allocateBlocksSequential("file2", 4);
allocateBlocksSequential("file3", 2);
// Display disk status
displayDisk();
return 0;
}
ii. Indexed
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_BLOCKS 100
#define MAX_FILENAME_LENGTH 20
#define MAX_INDEX_BLOCKS 10
struct IndexBlock {
int file_id;
int blocks[MAX_INDEX_BLOCKS];
int num_blocks;
};
struct File {
char filename[MAX_FILENAME_LENGTH];
int index_block;
int size;
};
int disk[MAX_BLOCKS]; // Array to represent disk blocks
struct File files[MAX_BLOCKS]; // Array to store file information
struct IndexBlock index_blocks[MAX_BLOCKS]; // Array to store index block information
int num_files = 0; // Number of files stored on disk
int num_index_blocks = 0; // Number of index blocks used
// Function to initialize disk
void initializeDisk() {
for (int i = 0; i < MAX_BLOCKS; i++) {
disk[i] = -1; // -1 indicates an empty block
}
}
// Function to display disk
void displayDisk() {
printf("Disk Status:\n");
for (int i = 0; i < MAX_BLOCKS; i++) {
printf("%3d ", i);
}
printf("\n");
for (int i = 0; i < MAX_BLOCKS; i++) {
printf("%3d ", disk[i]);
}
printf("\n");
}
// Function to check if a block is free
bool isBlockFree(int block) {
return disk[block] == -1;
}
// Function to allocate an index block for a file
int allocateIndexBlock(int file_id) {
int index_block = -1;
for (int i = 0; i < MAX_BLOCKS; i++) {
if (isBlockFree(i)) {
index_block = i;
disk[i] = file_id; // Assign the file ID to the block
num_index_blocks++;
break;
}
}
if (index_block != -1) {
printf("Index block allocated for file %d at block %d\n", file_id, index_block);
} else {
printf("Error: Insufficient free blocks for index block allocation\n");
}
return index_block;
}
// Function to allocate blocks for a file using an index block
void allocateBlocksIndexed(int file_id, int size) {
int index_block = allocateIndexBlock(file_id);
if (index_block != -1) {
int num_blocks = (size + MAX_INDEX_BLOCKS - 1) / MAX_INDEX_BLOCKS; // Calculate
number of index blocks needed
index_blocks[index_block].file_id = file_id;
index_blocks[index_block].num_blocks = num_blocks;
for (int i = 0; i < num_blocks; i++) {
for (int j = 0; j < MAX_INDEX_BLOCKS; j++) {
int block = i * MAX_INDEX_BLOCKS + j;
if (block < size) {
// Allocate data blocks for the file
for (int k = 0; k < MAX_BLOCKS; k++) {
if (isBlockFree(k)) {
index_blocks[index_block].blocks[j] = k;
disk[k] = file_id; // Assign the file ID to the block
size--;
break;
}
}
} else {
index_blocks[index_block].blocks[j] = -1; // -1 indicates end of file
}
}
}
files[num_files].index_block = index_block;
files[num_files].size = size;
printf("File %d allocated using indexed allocation\n", file_id);
num_files++;
}
}
int main() {
initializeDisk();
// Example: Allocate files
allocateBlocksIndexed(0, 7);
allocateBlocksIndexed(1, 11);
allocateBlocksIndexed(2, 5);
// Display disk status
displayDisk();
return 0;
}
iii. Linked
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct Block {
int data;
struct Block *next;
};
if (start_block != -1) {
// Store file information
strcpy(files[num_files].filename, filename);
num_files++;
printf("File %s allocated using linked allocation\n", filename);
} else {
printf("Error: Insufficient free blocks for file allocation\n");
}
}
int main() {
initializeDisk();
return 0;
}