Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I hate this assignment so much right now.
- Loading branch information
JP Villanueva
committed
Apr 14, 2012
1 parent
8d57be1
commit e0b2dc6
Showing
4 changed files
with
192 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lab5: lab5_p1.c lab5_p2.c | ||
gcc -o lab5_p1 lab5_p1.c | ||
gcc -o lab5_p2 lab5_p2.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
void decimal_to_octal(unsigned long long); | ||
|
||
int main(int argc, char** argv) { | ||
unsigned long long virtual_address; | ||
unsigned long long page_number; | ||
unsigned long long offset; | ||
|
||
virtual_address = atoll(argv[1]); | ||
|
||
/* Bit shifting. */ | ||
page_number = virtual_address >> 15; | ||
|
||
/* Bit masking. */ | ||
offset = virtual_address & 0x7FFF; | ||
|
||
printf("The address %lld is equal to ", virtual_address); | ||
decimal_to_octal(virtual_address); | ||
|
||
printf("Page number: %lld decimal and ", page_number); | ||
decimal_to_octal(page_number); | ||
|
||
printf("Offset = %lld decimal and ", offset, offset); | ||
decimal_to_octal(offset); | ||
|
||
return 0; | ||
} | ||
|
||
void decimal_to_octal(unsigned long long decimal) { | ||
char octal[64]; | ||
char temp[64]; | ||
int i = 0; | ||
int k = 0; | ||
int remainder; | ||
|
||
memset(octal, 0, sizeof(char)); | ||
memset(temp, 0, sizeof(char)); | ||
|
||
while(decimal > 0) { | ||
remainder = decimal % 8; | ||
decimal /= 8; | ||
temp[i++] = remainder + '0'; | ||
} | ||
|
||
while(i >= 0) { | ||
octal[k++] = temp[--i]; | ||
} | ||
|
||
octal[k] = 0; | ||
|
||
printf("%s octal\n", octal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define SIZE 100 | ||
#define FRAME_SIZE 2 | ||
|
||
int head = 0; | ||
int fifo_page_faults = 0; | ||
int lru_page_faults = 0; | ||
|
||
struct Page { | ||
int index; | ||
int reference; | ||
}; | ||
|
||
void fifo(int*, int); | ||
int frame_exists(int*, int); | ||
/* | ||
void lru(struct Page *, int, int); | ||
void opt(int*, int); | ||
int frame_exists_lru(struct Page *, int, int); | ||
int lru_lookup(struct Page *); | ||
*/ | ||
|
||
int main() { | ||
int i; | ||
int page_frames[FRAME_SIZE]; | ||
int page_reference[SIZE]; | ||
struct Page page_frames_lru[FRAME_SIZE]; | ||
|
||
/* Generates entropy for rand(). */ | ||
srand(time(0)); | ||
|
||
for(i = 0; i < SIZE; i++) { | ||
page_reference[i] = rand() % 14; | ||
printf("%d ", page_reference[i]); | ||
} | ||
printf("\n"); | ||
|
||
for(i = 0; i < FRAME_SIZE; i++) { | ||
page_frames[i] = -1; | ||
} | ||
|
||
for(i = 0; i < SIZE; i++) { | ||
fifo(page_frames, page_reference[i]); | ||
} | ||
|
||
printf("FIFO page faults: %d\n", fifo_page_faults); | ||
|
||
/* | ||
for(i = 0; i < FRAME_SIZE; i++) { | ||
page_frames_lru[i].index = -1; | ||
page_frames_lru[i].reference = -1; | ||
} | ||
for(i = 0; i < SIZE; i++) { | ||
lru(page_frames_lru, page_reference[i], i); | ||
} | ||
printf("LRU page faults: %d\n", lru_page_faults); | ||
*/ | ||
|
||
return 0; | ||
} | ||
|
||
void fifo(int* page_frames, int page_number) { | ||
int current_page_number; | ||
|
||
if(!frame_exists(page_frames, page_number)) { | ||
current_page_number = page_frames[head]; | ||
page_frames[head] = page_number; | ||
head = (head + 1) % FRAME_SIZE; | ||
fifo_page_faults++; | ||
} | ||
} | ||
|
||
int frame_exists(int* page_frames, int page_number) { | ||
int i; | ||
|
||
for(i = 0; i < FRAME_SIZE; i++) { | ||
if(page_frames[i] == page_number) { | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
void lru(struct Page *page_frames_lru, int page_reference, int index) { | ||
int current_index; | ||
int current_page_reference; | ||
if(!frame_exists_lru(page_frames_lru, page_reference, index)) { | ||
current_index = lru_lookup(page_frames_lru); | ||
current_page_reference = page_frames_lru[current_index].reference; | ||
page_frames_lru[current_index].reference = page_reference; | ||
page_frames_lru[current_index].index = index; | ||
lru_page_faults++; | ||
} | ||
} | ||
int frame_exists_lru(struct Page *page_frames_lru, int page_number, int index) { | ||
int i; | ||
for(i = 0; i < FRAME_SIZE; i++) { | ||
if(page_frames_lru[i].reference == page_number) { | ||
page_frames_lru[i].index = index; | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
int lru_lookup(struct Page *page_frames_lru) { | ||
int i; | ||
int k = 0; | ||
int min = -1; | ||
for(i = 1; i < SIZE; i++) { | ||
if(min > page_frames_lru[i].index) { | ||
min = page_frames_lru[i].index; | ||
k = i; | ||
} | ||
} | ||
return k; | ||
} | ||
void opt(int* page_frames, int page_number) { | ||
} | ||
*/ |