Problem with C++ in linux

This is a discussion on Problem with C++ in linux within the C++ Programming forums, part of the General Programming Boards category; Uhm, i'm new to linux as well as C++ so i was supposed to change the if none are available ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Problem with C++ in linux

    Uhm, i'm new to linux as well as C++ so i was supposed to change the if none are available part to a random replacement algorithm from the previous LRU algorithm but i can't get the program to run in linux RedHat 9.0, when i used the terminal to gcc my input file...something about not recognising the rand and srand..?? Plus i also can't output the hit/miss ratio for this code using scanf or cout, what gives??
    Can anyone please help me?

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #define MAXSWAP 500
    #define MAXPAGE 20
    
    typedef struct{
    		int age;
            int state; /* 1 is used, 0 is unused */
    		char pageframe[5];
    	}vm;
    
    
    int find_next_available_page(vm * memory, vm * swap)
    {
    	int i,position;
    
    	int highest=0;
        int j=-1;
    
    
    	/* see if there are any empty page frames in memory */
    	for(i=0;i<MAXPAGE;i++)
    	{
    		if(memory[i].state==0)
    		{
    				return i;
    		}
    	}
    	
    	/* if none are available, a suitable candidate must be chosen... */
    
    	srand (time(NULL));
    	
    	position = rand()%20; /* any position could be chosen to be replaced */
    
    	/* find an empty swap spot */
    
    	for(i=0;i<MAXSWAP;i++)
        {
    		if(swap[i].state==0)
    			j=i;
    	}
    
    	if(j<0)
    	{
    		printf("Out of memory condition in simulation. Exiting\n");
    		exit(0);
    	}
    
    	strncpy(swap[j].pageframe, memory[position].pageframe, 5);
    	memory[position].state=0;
    	memory[position].age=0;
    	swap[j].state=1;
    				
    	/* returns free spot */
    	return position;
    }
    
    
    int find_page_in_memory(char * page, vm * memory)
    {
    	int i;
    
        for(i=0;i<MAXPAGE;i++)
    	{
    		if(memory[i].state=1){
    			memory[i].age = memory[i].age+1;
            }
        }
    
    	for(i=0;i<MAXPAGE;i++)
    	{
    		if(strncmp(page, memory[i].pageframe, 5)==0){
       		  memory[i].age = 0;
    	
    		  return 1;
    		}
    	}
    
    	return 0;
    }
    
    int find_page_in_swap(char * page, vm * swap, vm * memory)
    {
    	int i,availablepage;
    
    	for(i=0;i<MAXSWAP;i++)
    	{
    		if(strncmp(page, swap[i].pageframe, 5)==0){
    
                availablepage = find_next_available_page(memory, swap);
    			strncpy(memory[availablepage].pageframe, page, 5);
    			memory[availablepage].state=1;
    			memory[availablepage].age=0;
    			swap[i].state=0;
    			
    		  return 1;
    		}
    	}
    
    	return 0;
    }
    
    void dump_memory_map(vm *memory, vm *swap)
    {
    	int i;
    	for (i=0;i<MAXPAGE;i++)
    	{
    		printf(" RAM Frame[%2i] = %s\n",i,memory[i].pageframe);
    	}
    	printf("\n");
    	for (i=0;i<MAXSWAP;i++)
    	{
    		printf("Swap Frame[%2i] = %s\n",i,swap[i].pageframe);
    	}
    	printf("\n");
    }
    
    
    int main(int argc, char *argv[])
    {
    	FILE * fptr;	
    	vm memory[MAXPAGE];
    
    	char page[5];
    
    	vm swap[MAXSWAP];
    
    	int i,hit,miss,availablepage;
    
    	hit = miss = 0;
    
        int available_page = 0;
    
    	if(argc!=2){
    		fprintf(stderr, "Please specify trace file\n");
    		exit(1);
    	}
    
    	/* initialize all age as 0 and all state as unused */
    
    	for(i=0;i<MAXPAGE;i++)
    	{
    		memory[i].age=0;
    		memory[i].state=0;		
    		memory[i].pageframe[0]='\0';
    	}
    
    	for(i=0;i<MAXSWAP;i++)
    	{
    		swap[i].age=0;
    		swap[i].state=0;
    		swap[i].pageframe[0]='\0';
    	}
    
    
    	fptr = 	fopen(argv[1], "r");
    	if(fptr==NULL){
    	    fprintf(stderr, "Unable to open trace file");
    		exit(1);
    	}
    
    	while(!feof(fptr)){ 
    		fscanf(fptr,"%5s",page);
    		dump_memory_map(memory,swap); /* remove or comment this debugging line if 
    										 program output gets too verbose */
    		printf("Page Requested: %s\n",page);
    		if(!(find_page_in_memory(page,memory))){
    			miss++;
    			printf("Page miss!\n");
    			if(!(find_page_in_swap(page,swap,memory))){
                	availablepage = find_next_available_page(memory, swap);
    				strncpy(memory[availablepage].pageframe, page, 5);
    				memory[availablepage].state=1;
    				memory[availablepage].age=0;
    			}
    		}
    		else
    		{
    			hit++;
    			printf("Page hit!\n");
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27

    Re: Problem with C++ in linux

    Originally posted by MelaOS
    Uhm, i'm new to linux as well as C++ so i was supposed to change the if none are available part to a random replacement algorithm from the previous LRU algorithm but i can't get the program to run in linux RedHat 9.0, when i used the terminal to gcc my input file...something about not recognising the rand and srand..?? Plus i also can't output the hit/miss ratio for this code using scanf or cout, what gives??
    Can anyone please help me?
    Doing a man on any of these functions will tell you what header they are in. Googling "man rand" will bring up the man page for rand and this works for most any C function and alot of other things too. Scanf is not what you want for outputing the hit/miss ratio. 'man scanf' for details. Include iostream.h or iostream to get access to cout, and compile with g++ not gcc, unless you are passing the C++ language flag to gcc.
    Last edited by grady; 12-27-2003 at 09:57 PM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    gcc compiles as C++ if the file ending is cpp, cc or cxx.

    The problem here is that rand and srand are in <stdlib.h>, which he doesn't include.

    And of course that it's true C code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ in linux problem (long program)
    By Goldrak in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2006, 05:45 PM
  2. OpenGL Linux Line Drawing Problem
    By swanley007 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2006, 09:31 AM
  3. Replies: 1
    Last Post: 10-18-2005, 10:20 AM
  4. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  5. Linux modem problem
    By VooDoo in forum Linux Programming
    Replies: 5
    Last Post: 08-19-2002, 05:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21