SSP
SSP
h>
#define pagesize 10
//Toby Mathew| 59 | Paging
#include<stdlib.h>
void main()
{
Printf(“Enter the size of pagetable”);
Int n;
Scanf(“%d”,&n);
Printf(“Enter the page table values\n”);
Int pagetable[n];
For(int i=0;i<n;i++)
{
Scanf(“%d”,&pagetable[i]);
}
Printf(“Page table\n”);
Printf(“Pageno|frameno\n”);
Printf(“-----------------\n”);
For(int i=0;i<n;i++)
{
Printf(“ %d |%d\n”,i,pagetable[i]);
Printf(“-----------------\n”);
}
Printf(“Enter the logical address\n”);
Int log;
Scanf(“%d”,&log);
Int pageno=log/pagesize;
If(pageno>pagesize)
{
Printf(“Page limit exceeded\n”);
}
Int offset;
Offset=log%pagesize;
If(offset>pagesize)
{
Printf(“Limit exceeded”);
Exit(0);
}
Int frameno=pagetable[pageno];
Int phys=(frameno*pagesize)+offset;
Printf(“Pageno:%d\n”,pageno);
Printf(“Offset:%d\n”,offset);
Printf(“Frameno:%d\n”,frameno);
Printf(“Physical address:%d\n”,phys);
}
/*OUTPUT
Enter the size of pagetable5
Enter the page table values
1
3
4
5
6
Page table
Pageno|frameno
0|1
1|3
2|4
3|5
4|6
Enter the logical address
8
Pageno:2
Offset:2
Frameno:4
Physical address:14
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Toby Mathew| 59| File allocation
struct Node {
int block;
struct Node* next;
};
struct index {
char filename[20];
int block_array[100];
};
struct table {
char filename[20];
int startlocation;
int block_array[100];
struct Node* blocks;
};
int freespace = 100;
int choice;
void init_table(struct table tab[]) {
for (int i = 0; i < 100; i++) {
strcpy(tab[i].filename, "");
tab[i].startlocation = -1;
memset(tab[i].block_array, -1, sizeof(tab[i].block_array));
tab[i].blocks = NULL;
}
}
void init_index(struct index disk[]) {
for (int i = 0; i < 100; i++) {
strcpy(disk[i].filename, "");
memset(disk[i].block_array, -1, sizeof(disk[i].block_array));
}
}
void init_harddisk(char harddisk[100][20]) {
for (int i = 0; i < 100; i++) strcpy(harddisk[i], "");
}
void display_table(struct table tab[], int file_count) {
printf("\tFile Allocation Table\t\nFilename\tStart_location\
tAllocated_Blocks\n");
for (int i = 0; i < file_count; i++) {
if (strcmp(tab[i].filename, "") != 0) {
printf("%s\t\t%d\t\t", tab[i].filename, tab[i].startlocation);
for (int j = 0; tab[i].block_array[j] != -1; j++) {
printf("%d", tab[i].block_array[j]);
if (tab[i].block_array[j + 1] != -1) {
printf(", ");
}
}
printf("\n");
}
}
}
void display_linked_allocation(struct table tab[], int file_count) {
printf("\tLinked Allocation Table\t\nFilename\tStart_location\
tAllocated_Blocks\n");
for (int i = 0; i < file_count; i++) {
if (strcmp(tab[i].filename, "") != 0) {
printf("%s\t\t%d\t\t", tab[i].filename, tab[i].startlocation);
struct Node* current = tab[i].blocks;
printf("%d", current->block);
current = current->next;
while (current != NULL) {
printf(" -> %d", current->block);
current = current->next;
}
printf("\n");
}
}
}
void contiguous_allocation(char harddisk[100][20]) {
struct table tab[100];
init_table(tab);
int n;
printf("Enter the number of files\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char name[20];
int startloc, length;
printf("Enter the name, starting location, and length of file %d\n",
i + 1);
scanf("%s %d %d", name, &startloc, &length);
strcpy(tab[i].filename, name);
tab[i].startlocation = startloc;
if (startloc + length > 100) {
printf("Allocation not possible\n");
break;
}
int flag = 1;
for (int j = startloc; j < startloc + length; j++) {
if (strcmp(harddisk[j], "") != 0) {
flag = 0;
break;
}
}
if (flag) {
printf("Allocating blocks: ");
for (int k = startloc; k < startloc + length; k++) {
strcpy(harddisk[k], name);
printf("%d,", k);
tab[i].block_array[k - startloc] = k;
}
printf("\n");
} else {
printf("Allocation not possible\n");
}
}
display_table(tab, n);
}
void linked_allocation(char harddisk[100][20]) {
struct table tab[100];
init_table(tab);
int n;
printf("Enter the number of files\n");
scanf("%d", &n);
int allocated_files = 0;
for (int i = 0; i < n; i++) {
char name[20];
int startloc, length;
printf("Enter the name, starting location, and length of file %d\n",
i + 1);
scanf("%s %d %d", name, &startloc, &length);
if (strcmp(harddisk[startloc], "") != 0) {
printf("Starting location is not free; assigning a new start
location.\n");
do {
startloc = rand() % 100;
} while (strcmp(harddisk[startloc], "") != 0);
}
printf("Allocating blocks: %d", startloc);
strcpy(harddisk[startloc], name);
tab[allocated_files].startlocation = startloc;
strcpy(tab[allocated_files].filename, name);
freespace--;
length--;
struct Node* head = malloc(sizeof(struct Node));
head->block = startloc;
head->next = NULL;
tab[allocated_files].blocks = head; // Store head in the table
struct Node* current = head;
while (length > 0) {
int block;
do {
block = rand() % 100;
} while (strcmp(harddisk[block], "") != 0);
strcpy(harddisk[block], name);
struct Node* new_node = malloc(sizeof(struct Node));
new_node->block = block;
new_node->next = NULL;
current->next = new_node; // Link the new block
current = new_node;
printf(" -> %d", block);
freespace--;
length--;
}
printf("\n");
allocated_files++;
}
display_linked_allocation(tab, allocated_files);
}
void indexed_allocation() {
struct index disk[100];
struct table tab[100];
init_table(tab);
init_index(disk);
int n;
printf("Enter the number of files\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char name[20];
int index_block, length;
printf("Enter the name, index block, and length of file %d\n", i +
1);
scanf("%s %d %d", name, &index_block, &length);
if (strcmp(disk[index_block].filename, "") == 0) {
printf("Index block is: %d\n", index_block);
strcpy(disk[index_block].filename, name);
} else {
printf("The given index block is in use; assigning new index block\
n");
do index_block = rand() % 100; while
(strcmp(disk[index_block].filename, "") != 0);
printf("New index block is %d\n", index_block);
strcpy(disk[index_block].filename, name);
}
strcpy(tab[i].filename, name);
tab[i].startlocation = index_block;
memset(tab[i].block_array, -1, sizeof(tab[i].block_array)); //
Initialize blocks for this entry
if (freespace >= length) {
printf("Allocating blocks: ");
for (int j = 0; j < length; j++) {
int block;
do {
block = rand() % 100;
} while (strcmp(disk[block].filename, "") != 0);
printf("%d -> %d\n", index_block, block);
tab[i].block_array[j] = block; // Store block in array
strcpy(disk[block].filename, name);
freespace--;
}
} else {
printf("Allocation not possible; not enough free space\n");
}
}
display_table(tab, n);
}
int main() {
char harddisk[100][20];
init_harddisk(harddisk);
printf("File allocations\n1. Contiguous allocation\n2. Linked
allocation\n3. Indexed allocation\nEnter your choice\n");
scanf("%d", &choice);
switch (choice) {
case 1: contiguous_allocation(harddisk); break;
case 2: linked_allocation(harddisk); break;
case 3: indexed_allocation(); break;
default: printf("Invalid choice\n"); break;
}
return 0;
}
/*OUTPUT
1
Enter the number of files
3
Enter the name, starting location, and length of file 1
file1 0 4
Enter the name, starting location, and length of file 2
file2 5 3
Enter the name, starting location, and length of file 3
file3 9 4
Allocating blocks: 0,1,2,3,
Allocating blocks: 5,6,7,
Allocating blocks: 9,10,11,12,
File Allocation Table
Filename Start_location Allocated_Blocks
file1 0 0, 1, 2, 3
file2 5 5, 6, 7
file3 9 9, 10, 11, 12
2
Enter the number of files
2
Enter the name, starting location, and length of file 1
file1 0 3
Enter the name, starting location, and length of file 2
file2 1 2
Allocating blocks: 0 -> 22 -> 43
Starting location is not free; assigning a new start location.
Allocating blocks: 65 -> 17
Linked Allocation Table
Filename Start_location Allocated_Blocks
file1 0 0 -> 22 -> 43
file2 65 65 -> 17
3
Enter the number of files
2
Enter the name, index block, and length of file 1
file1 5 3
Enter the name, index block, and length of file 2
file2 7 2
Index block is: 5
Allocating blocks: 5 -> 12
5 -> 34
5 -> 76
Index block is: 7
Allocating blocks: 7 -> 48
7 -> 99
File Allocation Table
Filename Start_location Allocated_Blocks
file1 5 12, 34, 76
file2 7 48, 99
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define memsize 1000
#define mobj 120
//Toby Mathew || Absoulute Loader ||59
char mem[memsize][3];
void inimem(){
for(int i=0;i<memsize;i++){
strcpy(mem[i],"**");
}
}
void main(){
FILE *fp;
char txtrstrt[4];
char buffer[200], prgmname[10], strtadd[10], lent[10];
int crrtadd, rcrdadd, frstobj=-1, endadd=-1;
int errflg=0;
inimem();
fp=fopen("sample.txt","r");
if(fp==NULL){
printf("ERROR IN OPENING FILE\n");
exit(0);
}
fgets(buffer,sizeof(buffer),fp);
sscanf(buffer,"H^%[^'^']^%[^'^']^%s",prgmname,strtadd,lent);
crrtadd=strtol(strtadd,NULL,16);
int i;
while(fgets(buffer,sizeof(buffer),fp)){
if(buffer[0]== 'E'){
break;
}
if(buffer[0]== 'T'){
char recrdstrt[10], objctcode[200];
sscanf(buffer,"T^%[^'^']^%s",recrdstrt,objctcode);
rcrdadd=strtol(recrdstrt,NULL,16);
if(frstobj==-1){
frstobj=rcrdadd;
for(i=crrtadd;i<frstobj;i++){
//printf("%04X **\n",i);
}
}
char *token=strtok(objctcode,"^");
while(token!=NULL){
for(i=0;i<strlen(token);i+=2){
if(token[i]== '\0' || token[i+1] == '\0'){
break;
}
char code[3]={token[i], token[i+1], '\0'};
strcpy(mem[rcrdadd-crrtadd],code);
rcrdadd++;
}
token=strtok(NULL,"^");
}
endadd=rcrdadd;
}
}
if(frstobj!=-1){
pri(crrtadd,endadd);
}
fclose(fp);
}
/*