Jump to content

Help On C Programme..pleze


Recommended Posts

Posted

[color=#ff0000][b]Question :-[/b][/color]

Please implement the memory management functions (allocation, deallocation and compaction) provided by operating systems. The input example is showed below, and it consists of multiple lines, every comment line is started with a # character. The first non-comment input line contains the total size of physical memory (memory address always from 0 to size – 1), and the rest non-comment input lines contain pid and either memory allocations with sizes or memory deallocations with starting addresses. The allocation using a best-fit algorithm, and the deallocation will automatically merge with adjacent free spaces if any. When a memory allocation request cannot be satisfied, compaction will be performed. If the request cannot be satisfied after the compaction, “out of memory” error message will be printed. After each compaction, your program should print revised starting addresses for each process. An example input as below: # the total size of physical memory (units are KB, MB, GB)
256MB
# here are memory allocations and deallocations
3012 alloc 128MB
215 alloc 32MB
1094 alloc 64MB
3012 dealloc 0
5 alloc 64MB
878 alloc 96MB
9014 alloc 64MB
And the correct output should looks like this: total size of physical memory:256M process 3012 allocated 128MB with starting addess 0 process 215 allocated 32MB with starting addess 128M process 1094 allocated 64MB with starting addess 160M process 3012 deallocated 128MB with starting addess 0 process 5 allocated 64MB with starting addess 0 memory compaction done: process 215 changed starting address to 64M, process 1094 changed starting address to 96M process 878 allocated 96MB with starting addess 160M memory compaction done: out of memory

Posted

[color=#ff0000][b]Answer idi correct or wrong:-[/b][/color]

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

Void display1(int mem, char string[]);
Void display1(int mem1, char string1[]);

struct freeblock {
int startAddress;
int size;
struct freeblock *next;
};

struct allocatedblock {
char process[64];
int startAddress;
int size;
struct allocatedblock *next;
};

typedef struct freeblock fb;
typedef struct allocatedblock ab;
int main(){

char process[3][64];
char str[512];
int i,j;
char s1[5] = “KB”;
char s2[5] = “MB”;
char s3[5] = “GB”;
char s4[20];

char ch;
int index;
int totalmemorysize = 0;
fb firstfree;
ab *firstallocated = NULL;

while(!feof(stdin)) {
fgets(str, sizeof(str), stdin);
if (str[0] == '#') {
continue;
}
for (i = 0, j = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
totalmemorysize = totalmemorysize * 10 + str[i] - '0';
} else {
if ((str[i] == 'M') || (str[i] == 'm')) {
totalmemorysize *= 1024;

// strcat(totalmemorysize, s2);
//printf("\nTotal Size of Physical memory: %s\n", s2);
display(totalmemorysize, s2);


} else if ((str[i] == 'G') || (str[i] == 'g')) {
totalmemorysize *= (1024 * 1024);
s3[5] = str[i];
display(totalmemorysize, s3);
//strcat(totalmemorysize, s3);
//printf("\nTotal Size of Physical memory: %s\n", s3);

}
else {
display(totalmemorysize, s1);
strcat(totalmemorysize, s1);
printf("\nTotal Size of Physical memory: %s\n", s1);

}
}
break;
}

//sprintf(s4, "%s%s%s%s", "this", " is", " my", " story");

fprintf(stderr, "Memory: %d", totalmemorysize);
firstfree.startAddress = 0;
firstfree.size = totalmemorysize;
firstfree.next = NULL;

while(!feof(stdin)) {
fgets(str, sizeof(str), stdin);
if (str[0] == '#') {
continue;
}
index = 0;
for (i = 0, j = 0; i < strlen(str); i++) {
ch = str[i];
if (isblank(ch)) {
if (!j)
continue;
}

if (isblank(ch)) {
process[index][j] = '\0';
j = 0;
index++;
if (index == 3)
break;
} else {
process[index][j++] = ch;
}
}
if (i == strlen(str)) {
process[index][j] = '\0';
index++;
}
if (index == 3) {
//fprintf(stderr, "\nLine to process: process %s, func %s, memory %s.", process[0], process[1], process[2]);
if (strcmp("alloc", process[1]) == 0) {
int memory = 0;
for (i = 0; i < strlen(process[2]); i++) {
if (isdigit(process[2][i]))
memory = memory * 10 + process[2][i] - '0';
else {
if ((process[2][i] == 'M') || (process[2][i] == 'm')) {
memory *= 1024;
} else if ((process[2][i] == 'G') || (process[2][i] == 'g')) {
memory *= (1024 * 1024);
}
break;
}
}
fb *freeblock = &firstfree;
ab *allocblock = firstallocated;
while (freeblock) {
if (freeblock->size >= memory) {
while (allocblock)
allocblock = allocblock->next;
allocblock = (ab *) malloc(sizeof(ab));

if (!firstallocated)
firstallocated = allocblock;
else {
ab *temp = firstallocated;
while (temp->next != NULL)
temp = temp->next;
temp->next = allocblock;
}
allocblock->startAddress = freeblock->startAddress;
allocblock->size = memory;
allocblock->next = NULL;
strncpy(allocblock->process, process[0], 64);
fprintf(stderr, "\nprocess %s allocated %d with starting address %d", process[0], display(memory, s2), freeblock->startAddress);
if (freeblock == &firstfree || freeblock->size > memory) {
freeblock->startAddress += memory;
freeblock->size = freeblock->size - memory;
} else {
fb *temp = &firstfree;
while (temp->next != freeblock);
temp->next = freeblock->next;
}
break;
}
freeblock = freeblock->next;
}
} else if (strcmp("dealloc", process[1]) == 0) {
int memory = 0;
for (i = 0; i < strlen(process[2]); i++) {
if (isdigit(process[2][i]))
memory = memory * 10 + process[2][i] - '0';
else {
if ((process[2][i] == 'M') || (process[2][i] == 'm')) {
memory *= 1024;
} else if ((process[2][i] == 'G') || (process[2][i] == 'g')) {
memory *= (1024 * 1024);
}
break;
}
}
fb *freeblock = &firstfree;
ab *allocblock = firstallocated;
while (allocblock != NULL && strcmp(allocblock->process, process[0]))
allocblock = allocblock->next;
if (allocblock != NULL) {
fprintf(stderr, "\nprocess %s deallocated %d with starting address %d", process[0], memory, allocblock->startAddress);
/*
while ((freeblock->startAddress+freeblock->size) != allocblock->startAddress)
freeblock = freeblock->next;
freeblock->size = freeblock->size + allocblock->size;
*/
}
}
}
}
}
Void display(int mem, char string[]) {
Char S[30];
While(mem >= 1024) {
mem = mem / 1024;
S = strcat(mem , string);
Printf(("\nTotal Size of Physical memory: %s\n", S);
//Printf(("\nTotal Size of Physical memory: %s\n", strcat(mem , string));
}


Void display1(int mem1, char string1[]) {
Char S[30];
While(mem1 >= 1024) {
mem1 = mem1 / 1024;
S = strcat(mem1 , string1);
Printf(("%s\t", S);
//Printf(("%s\t", strcat(mem , string1));
}

Posted

[img]http://www.bewarsetalk.net/discus/movieanimated5/muniga.gif[/img]

Posted

edhi code snippet lo peduthava? its hard to read without indentation...code tags feature undi chudu add that
[quote name='livelemons' timestamp='1381683942' post='1304408792']
[color=#ff0000][b]Answer idi correct or wrong:-[/b][/color]

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

Void display1(int mem, char string[]);
Void display1(int mem1, char string1[]);

struct freeblock {
int startAddress;
int size;
struct freeblock *next;
};

struct allocatedblock {
char process[64];
int startAddress;
int size;
struct allocatedblock *next;
};

typedef struct freeblock fb;
typedef struct allocatedblock ab;
int main(){

char process[3][64];
char str[512];
int i,j;
char s1[5] = “KB”;
char s2[5] = “MB”;
char s3[5] = “GB”;
char s4[20];

char ch;
int index;
int totalmemorysize = 0;
fb firstfree;
ab *firstallocated = NULL;

while(!feof(stdin)) {
fgets(str, sizeof(str), stdin);
if (str[0] == '#') {
continue;
}
for (i = 0, j = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
totalmemorysize = totalmemorysize * 10 + str[i] - '0';
} else {
if ((str[i] == 'M') || (str[i] == 'm')) {
totalmemorysize *= 1024;

// strcat(totalmemorysize, s2);
//printf("\nTotal Size of Physical memory: %s\n", s2);
display(totalmemorysize, s2);


} else if ((str[i] == 'G') || (str[i] == 'g')) {
totalmemorysize *= (1024 * 1024);
s3[5] = str[i];
display(totalmemorysize, s3);
//strcat(totalmemorysize, s3);
//printf("\nTotal Size of Physical memory: %s\n", s3);

}
else {
display(totalmemorysize, s1);
strcat(totalmemorysize, s1);
printf("\nTotal Size of Physical memory: %s\n", s1);

}
}
break;
}

//sprintf(s4, "%s%s%s%s", "this", " is", " my", " story");

fprintf(stderr, "Memory: %d", totalmemorysize);
firstfree.startAddress = 0;
firstfree.size = totalmemorysize;
firstfree.next = NULL;

while(!feof(stdin)) {
fgets(str, sizeof(str), stdin);
if (str[0] == '#') {
continue;
}
index = 0;
for (i = 0, j = 0; i < strlen(str); i++) {
ch = str[i];
if (isblank(ch)) {
if (!j)
continue;
}

if (isblank(ch)) {
process[index][j] = '\0';
j = 0;
index++;
if (index == 3)
break;
} else {
process[index][j++] = ch;
}
}
if (i == strlen(str)) {
process[index][j] = '\0';
index++;
}
if (index == 3) {
//fprintf(stderr, "\nLine to process: process %s, func %s, memory %s.", process[0], process[1], process[2]);
if (strcmp("alloc", process[1]) == 0) {
int memory = 0;
for (i = 0; i < strlen(process[2]); i++) {
if (isdigit(process[2][i]))
memory = memory * 10 + process[2][i] - '0';
else {
if ((process[2][i] == 'M') || (process[2][i] == 'm')) {
memory *= 1024;
} else if ((process[2][i] == 'G') || (process[2][i] == 'g')) {
memory *= (1024 * 1024);
}
break;
}
}
fb *freeblock = &firstfree;
ab *allocblock = firstallocated;
while (freeblock) {
if (freeblock->size >= memory) {
while (allocblock)
allocblock = allocblock->next;
allocblock = (ab *) malloc(sizeof(ab));

if (!firstallocated)
firstallocated = allocblock;
else {
ab *temp = firstallocated;
while (temp->next != NULL)
temp = temp->next;
temp->next = allocblock;
}
allocblock->startAddress = freeblock->startAddress;
allocblock->size = memory;
allocblock->next = NULL;
strncpy(allocblock->process, process[0], 64);
fprintf(stderr, "\nprocess %s allocated %d with starting address %d", process[0], display(memory, s2), freeblock->startAddress);
if (freeblock == &firstfree || freeblock->size > memory) {
freeblock->startAddress += memory;
freeblock->size = freeblock->size - memory;
} else {
fb *temp = &firstfree;
while (temp->next != freeblock);
temp->next = freeblock->next;
}
break;
}
freeblock = freeblock->next;
}
} else if (strcmp("dealloc", process[1]) == 0) {
int memory = 0;
for (i = 0; i < strlen(process[2]); i++) {
if (isdigit(process[2][i]))
memory = memory * 10 + process[2][i] - '0';
else {
if ((process[2][i] == 'M') || (process[2][i] == 'm')) {
memory *= 1024;
} else if ((process[2][i] == 'G') || (process[2][i] == 'g')) {
memory *= (1024 * 1024);
}
break;
}
}
fb *freeblock = &firstfree;
ab *allocblock = firstallocated;
while (allocblock != NULL && strcmp(allocblock->process, process[0]))
allocblock = allocblock->next;
if (allocblock != NULL) {
fprintf(stderr, "\nprocess %s deallocated %d with starting address %d", process[0], memory, allocblock->startAddress);
/*
while ((freeblock->startAddress+freeblock->size) != allocblock->startAddress)
freeblock = freeblock->next;
freeblock->size = freeblock->size + allocblock->size;
*/
}
}
}
}
}
Void display(int mem, char string[]) {
Char S[30];
While(mem >= 1024) {
mem = mem / 1024;
S = strcat(mem , string);
Printf(("\nTotal Size of Physical memory: %s\n", S);
//Printf(("\nTotal Size of Physical memory: %s\n", strcat(mem , string));
}


Void display1(int mem1, char string1[]) {
Char S[30];
While(mem1 >= 1024) {
mem1 = mem1 / 1024;
S = strcat(mem1 , string1);
Printf(("%s\t", S);
//Printf(("%s\t", strcat(mem , string1));
}
[/quote]

Posted

I may logout now....calling cherlapalli, innovative in case

just by looking free, allocation code technically looks good...but I didnt understand the way u are getting the last address in the memory for allocation...sbrk(0) function use cheyi for getting the starting point of the free memory not sure if you are finding it in some other way. point 2: \continuous memory allocation laga ledhu nee code which is code...edina errors vaste ekada veyi with line number..code snippet lo peti veyi easy to read..

Posted

[quote name='GatisKandis' timestamp='1381684399' post='1304408837']
edhi code snippet lo peduthava? its hard to read without indentation...code tags feature undi chudu add that
[/quote]

ikkada chudu betharu...

[url="http://pastebin.com/vAhScV0U#"]http://pastebin.com/vAhScV0U#[/url]

[img]http://www.bewarsetalk.net/discus/movieanimated6/bemmi.rolu.gif[/img]

Posted

[quote name='GatisKandis' timestamp='1381684938' post='1304408879']
I may logout now....calling cherlapalli, innovative in case

just by looking free, allocation code technically looks good...but I didnt understand the way u are getting the last address in the memory for allocation...sbrk(0) function use cheyi for getting the starting point of the free memory not sure if you are finding it in some other way. point 2: \continuous memory allocation laga ledhu nee code which is code...edina errors vaste ekada veyi with line number..code snippet lo peti veyi easy to read..
[/quote]


[img]http://www.bewarsetalk.net/discus/movieanimated5/muniga.gif[/img]

Posted

em ayindi mayya [img]http://lh3.ggpht.com/--o7mXz3u-j4/T9VVBGzBJAI/AAAAAAAAGo0/kmj8a1-XW2g/s150/PK-1.gif[/img]
[quote name='livelemons' timestamp='1381685086' post='1304408894']


[img]http://www.bewarsetalk.net/discus/movieanimated5/muniga.gif[/img]
[/quote]

×
×
  • Create New...