473,799 Members | 3,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Least recently used (LRU) memory question

kkk
I am practicing LRU by writing a simulating c programme, but encounter
a problem.

The problem is when a page hit occurred (simulated process request
(contains requested page) a page, which has the same page number as
requesting process), I try to obtain the page number previously stored
in the linked list with its position. The linked list store pages that
have been used by simulated physical memory (the linked list size is
10). The code snippet looks like:
========= code beg
....
struct linked_list{
int page_number;
struct linked_list *next;
} *list;

....
struct linked_list *object;
int found = FALSE;
int count=0;
while(found==0) {

object = get_object(coun t);
if(object->page_number == request[i]){
found == 1;
remove_from_lis t(count);
add_to_last_of_ list(page_numbe r);
}else{
count++;
}
}

....
struct linked_list
*get_object(int position)
{
int count = 0;
if(*head==NULL) {
printf("linked list is null!\n");
exit(EXIT_FAILU RE);
}
while(next(list )!=NULL){
if(count == position){
return list;
}else{
list = next(list);
}
count++;
}
}
========== code end

But the problem is that because I assign value to the list with next()
function, which point to the next object, then next time when trying
to traverse through the linked list, the object return by get_object()
is NULL. That cause the error of the programme.

So my question is how to avoid overwriting the pointer stored in the
linked list?

I sincerely appreciate any help.

below is the source:
========== source beg
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MEMORY_SIZE 10
#define PAGE_SIZE 500
#define REQUEST_SIZE 1000
#define NOT_INIT -1
#define FIFO 99
#define LRU 98
#define FIFO_STR "FIFO"
#define LRU_STR "LRU"
#define TRUE 1
#define FALSE 0

struct linked_list{
int page_number;
struct linked_list *next;
} *list;

static int mem[MEMORY_SIZE]; //physical memory
static int page_table[PAGE_SIZE]; //page table capacity
static int request[REQUEST_SIZE];//preocess request number
int LINKED_LIST_SIZ E = 0;

struct linked_list
*next(struct linked_list *object)
{
return object->next;
}

int
iterate(struct linked_list **head)
{
int count=0;
if(*head==NULL) {
printf("linked list is null!");
exit(EXIT_FAILU RE);
}
while(next(*hea d) != NULL){
printf("\n<iter atecount: %d / page_number: %d \n", count,
next(*head)->page_number) ;
*head = next(*head);
count++;
}
return count;
}

struct linked_list
*get_last_objec t(struct linked_list *object)
{
struct linked_list *o;
if(object==NULL ){
printf("linked list passed in is NULL\n");
exit(EXIT_FAILU RE);
}
if(next(object) ==NULL){
//line 79 its next should be null
return object;
}
while( next(object) != NULL){
if( next(next(objec t)) == NULL){// the last object in the linked
list
return next(object);
}else{
object = next(object);
}
}
}

void
add_to_last(int requested_page)
{
struct linked_list *object;
struct linked_list *last_object;
if(list == NULL){// not yet init
list = (struct linked_list *)malloc(sizeof (struct linked_list));
list->page_number = requested_page;
list->next = NULL;
}else{// new request page
object = (struct linked_list *)malloc(sizeof (struct linked_list));
object->next = NULL;
object->page_number = requested_page;
last_object = get_last_object (list);
last_object->next = object;
}
}

int
remove_first()
{
struct linked_list *first_object;
if(list==NULL){
printf(" First object in linked list is NULL!\n");
exit(EXIT_FAILU RE);
}
first_object = list;
list = list->next;
first_object->next = NULL;
/* free unused memroy */
return first_object->page_number;
}

int
find_free_mem_p osition()
{
int i;
for(i=0; i<MEMORY_SIZE; i++){
if(mem[i] == NOT_INIT){
return i;
}
}
}
void
init_request()
{
int i;
for(i=0; i<REQUEST_SIZE ; i++){
request[i] = nrand(0, PAGE_SIZE);
}
}

struct linked_list
*get_object(int position, struct linked_list **head)
{
int count = 0;
if(*head==NULL) {
printf("linked list is null!\n");
exit(EXIT_FAILU RE);
}
while(next(*hea d)!=NULL){
if(count == position){
return *head;
}else{
*head = next(*head);
}
count++;
}
}

/*
struct linked_list
*get(int position)
{
int count = 0;
if(list==NULL){
printf("linked list is null!\n");
exit(EXIT_FAILU RE);
}
//iterate(&list);
while( next(list)!=NUL L){
if(count == position){
return next(list);
}else{
list = next(list);
}
count++;
}
}
*/

int
is_found(struct linked_list *o, int page_number_pas sed){
if(o==NULL){
printf(" get object is null\n");
return FALSE;
}
if(o->page_number == page_number_pas sed){
return TRUE;
}else{
return FALSE;
}
}

void
remove_from_lis t(int position)
{
int count = 0;
struct linked_list *n;
if(list==NULL) {
printf(" list is NULL\n");
exit(EXIT_FAILU RE);
}
while(next(list )!=NULL){
count++;
if(count == position){
list->next = next(list);
list->page_number = next(list)->page_number;
free(next(list) );
}else{
list = next(list);
}
}
}

/*
void
reorg_linked_li st(int page_number)
{
int count = 0;
int found = FALSE;
struct linked_list *object;
if(list==NULL){
printf(" List is NULL!\n");
exit(EXIT_FAILU RE);
}
while(found == FALSE){
object = get(page_number );
if( is_found(object , page_number)){// page is found
found == TRUE;
remove_from_lis t(object->page_number) ;
add_to_last(pag e_number);
}else{//page not found in lined list
count++;
}
}
}
*/
void
page_replacemen t_policy(int policy)
{
int i, position, page_number;
int page_hit = 0, page_miss = 0, page_swap = 0;
int free_mem = MEMORY_SIZE;
char banner[10];

int found = FALSE;
struct linked_list *object;
int count = 0;

for(i=0; i<MEMORY_SIZE; i++) mem[i] = NOT_INIT;
for(i=0; i<PAGE_SIZE; i++) page_table[i] = NOT_INIT;

memset(banner, 0, sizeof(banner)) ;
if(policy == FIFO)
strncpy(banner, FIFO_STR, strlen(FIFO_STR ));
else if(policy == LRU)
strncpy(banner, LRU_STR, strlen(LRU_STR) );

printf("======= === %s BEG ==========\n", banner);

for(i=0; i<REQUEST_SIZE ; i++){ // loop through each process
printf(" request[%d]: %d", i, request[i]);
if(page_table[request[i]]!=NOT_INIT){//page is used
page_hit++;
printf(" << page hit! >");
if(policy==LRU) {
//reorg_linked_li st(request[i]);
while(found == FALSE){
object = get_object(coun t, &list);
if(object == NULL){
printf("object is null!\n");
exit(EXIT_FAILU RE);
}
printf("count:% d / page_number:%d / request[%d]:%d\n", count, object-
>page_number, i, request[i]);
if( is_found(object , request[i])){// page is found
found == TRUE;
remove_from_lis t( count);
add_to_last(pag e_number);
}else{//page not found in linked list
count++;
}
}//while
}
}else{// page is not used // mem is not yet init
page_miss++;
printf(" << page miss! >");
if(free_mem 0){// has free memory
position = find_free_mem_p osition();
printf(" free space at position: %d", position);
// book room
mem[position] = request[i];
page_table[request[i]] = position;
//add to the linked list
add_to_last(req uest[i]);
//decrement free memory space by 1
free_mem--;
}else{// need to swap mem
//remove page from mem
page_number = remove_first();
position = page_table[page_number];
page_table[page_number] = NOT_INIT;
page_swap++;
printf(" old page (%d) at position (%d) swapped out!",
page_number, position);
//associate new page with physical mem
// and page_table
page_table[request[i]] = position;
mem[position] = request[i];
add_to_last(req uest[i]);
}
}
//iterate(list);
printf("\n");
}
printf("======= === %s END ==========\n", banner);

}

int
nrand(int min, int max)
{
int n;
n = (min + rand() % max);
return n;
}

int
main(int *argc, char **argv)
{
srand(time((tim e_t)NULL));
list = NULL;
init_request();
page_replacemen t_policy(LRU);
//page_replacemen t_policy(FIFO);
}

========== source end

Mar 4 '07 #1
3 3884
On 4 Mar 2007 09:42:17 -0800, "kkk" <ne*******@yaho o.com.twwrote:
>I am practicing LRU by writing a simulating c programme, but encounter
a problem.
snip 300+ lines

You posted two messages a little over 1 hour apart. The second added
50 lines. Would you care to describe what you added? You should also
recognize that Usenet is not a chat room. It may take several hours
or more for your message to propagate. Similarly for any responses.
Remove del for email
Mar 5 '07 #2
kkk
On 3¤ë5¤é, ¤W¤È8®É45¤À, Barry Schwarz <schwa...@doezl ..netwrote:
On 4 Mar 2007 09:42:17 -0800, "kkk" <newbie...@yaho o.com.twwrote:
I am practicing LRU by writing a simulating c programme, but encounter
a problem.

snip 300+ lines

You posted two messages a little over 1 hour apart. The second added
50 lines. Would you care to describe what you added? You should also
recognize that Usenet is not a chat room. It may take several hours
or more for your message to propagate. Similarly for any responses.

Remove del for email
I think it is because of google. My previous two messages was post
through google's usenet web interface. It told me that my first
message will be deleted and will not appeared on the usenet after
deleted. However, it seems not so. I am sorry for that problem because
I just thought my first message was too general without specifying the
code with which I am confused.

Thanks your reply indeed.

Mar 5 '07 #3

kkk wrote:
On 3¤ë5¤é, ¤W¤È8®É45¤À, Barry Schwarz <schwa...@doezl .netwrote:
On 4 Mar 2007 09:42:17 -0800, "kkk" <newbie...@yaho o.com.twwrote:
>I am practicing LRU by writing a simulating c programme, but encounter
>a problem.
snip 300+ lines

You posted two messages a little over 1 hour apart. The second added
50 lines. Would you care to describe what you added? You should also
recognize that Usenet is not a chat room. It may take several hours
or more for your message to propagate. Similarly for any responses.

I think it is because of google. My previous two messages was post
through google's usenet web interface. It told me that my first
message will be deleted and will not appeared on the usenet after
deleted. However, it seems not so.
Google Groups *has* apparently honoured your request and deleted the
message from it's servers, since it doesn't appear on Groups. However,
it has no authority to do the same for all the other thousands of
servers that comprise Usenet. Most Usenet servers ignore CANCEL
messages, since they're prone to abuse.

Mar 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4414
by: Flavio Ribeiro | last post by:
Hi, Most of my application's operations (including SQL queries and numerical computation) can be cached. An LRU (least recently used) cache would fit the bill perfectly. This LRU cache should be preserved over multiple requests from the same session. If possible, it should be seen across multiple sessions so that all users can benefit from it.
4
2537
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int i = 0; i < 10; ++i){ std::cout << i << std::endl; for(int j = 0; j < 0x7fffffff; ++j){} } }
3
1767
by: Spencer | last post by:
One of the things that I have become interested in lately is questioning in general what percentage of DB2 UDB implementations are configured to run at least near optimally given the hardware infrastructure at said site. DB2 UDB continually is releasing performance enhancements with each release especially with the most recent 8.1 and 8.2 Lots of new features introduced and many to aid in tuning performance. With that said I have to...
20
9180
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
2
1923
by: Asfar | last post by:
Hi, I have a VS2005 windows program written in c#. In this program I have an array list which stores many DataTable's. When I first run the pogram the arraylist is loaded with datatables. At this point when the see the memory used by the program in task manager it is 135,654K. Now when I minimize the application the memory goes down to 1,456K. Again when I maximize the program and start using different menu options the memory used...
22
2513
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
0
687
by: kkk | last post by:
I am practicing LRU by writing a simulating programme in c, but encounter a problem. When obtaining data from a linked list (which stores the requested page number), seemingly the programme falls into an infinite loop. I guess that is because the object obtained through function get() always remains the same. However, I have no idea how to solve it. Would anyone is able to give me an hint? I sincerely appreciate it.
5
2739
by: Paul Rubin | last post by:
Anyone got a favorite LRU cache implementation? I see a few in google but none look all that good. I just want a dictionary indexed by strings, that remembers the last few thousand entries I put in it. It actually looks like this is almost a FAQ. A well-written implementation would probably make a good standard library module.
1
1733
by: UKishore | last post by:
Hi All, Am generating a new profile by running my standalone application using the API "NetUserAdd". After logging to the newly created profile , I couldn't see default "Recently Used Programs" in StartMenu where as by Windows newly created profiles Recently Used Programs got displayed. Note: "Recently Used Programs" items are visible in windows XP and above versions Kindly let me know if any come across this and what is the...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.