Paging Program Simulator:

This is a discussion on Paging Program Simulator: within the C Programming forums, part of the General Programming Boards category; [code] Hi I am new to C programming, on the search for some guidelines in how to approach this program. ...

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thumbs up Paging Program Simulator:

    [code]
    Hi I am new to C programming, on the search for some guidelines in how to approach this program. Can anyone give me any useful information (link or book), so I can do this program in C.
    Currently I am trying to develop a Pseudo code and understanding the program before starting any coding. Now in stage 0 but have hope in progressing.

    I have included the program in the following link:->http://www.angelfire.com/planet/zoefm/

    Iam not sure what is the output to the screen and how do you get for FIFO Faults: 11.

    Any help will be appreciated. Thank you very much.

    [code]
    Last edited by wise_ron; 10-22-2006 at 12:50 AM.
    Wise_ron
    One man's constant is another man's variable

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    What's wrong with including the code here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question Fifo demand_Stream

    Code:
    >> Salem  - What's wrong with including the code here?
    I have got some understanding in how to work the program. I have been working on developing a called function_fifo(). Fifo - First page in is first page out.
    Iam stuck trying to understand this code, that is my guide:

    Code:
    int FIFOrep(int * mainmem[2][NPages],demandpage);
    Iam trying to use a multidimension array array of 2 arrays of 4 pages. But I need to find away in how write when a page fault is made. Like for example every time the page fault is made it has an *.

    This is my guide of the final output.
    Code:
     
    
    FIFO
    FLT	1	2	3	4
    *	1			
    *	1	2		
    *	1	2	3	
    	1	2	3	
    *	1	2	3	5
    	1	2	3	5
    *	7	2	3	5
    	7	2	3	5
    	7	2	3	5
    	7	2	3	5
    	7	2	3	5
    *	7	4	3	5
    *	7	4	1	5
    *	7	4	1	3
    *	5	4	1	3
    *	5	6	1	3
    	5	6	1	3
    *	5	6	7	3
    Faults: 	11

    Code:
    #include<stdio.h>
    
    function_fifo()
    {
    	//int FIFOrep(int * mainmem[2][NPages],demandpage);
    
    }
    
    main()
    {
    	char demand_stream[30]="1232527353724135637";  //the string to be demanded
    	int NPages=4; // number of pages
    	
    	printf("Main Memory Pages? %d\n",NPages);  
    	printf("Demand Stream? %s\n",demand_stream);
        
    	int mainmem[2][NPages];  //array of 2 arrays of 4 ints which are Number of Pages
    	function_fifo();	
    }
    Do you have any suggestions in how to approach or improve this code? It would be kindly appreciated. Thanks for your help and support.
    Wise_ron
    One man's constant is another man's variable

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    You need a structure containing
    - a boolean indicating whether the page is valid
    - an integer representing the page number.

    You then have an array of 4 of them. Initially, all 4 entries have .valid = false

    When you request a new page, do the following
    - check whether any entry has .valid = true and the page number matches. If it does, do nothing
    - if there is an entry with .valid = false, then load the page into that slot, set valid = true and raise a page fault.
    - if there is no entry with .valid = false, then remove the oldest entry and make the newly freed slot the requested page, and raise a page fault.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question How to access integers of the array so i know is a page fault?

    Hi I am stuck in dividing the stream into digits of the array for example if I enter 1 2 3 4 each of those numbers will be a page fault. When I enter a new value (each digit) will be assign as a pager fault because is not repeated. My question is how can I grab each digit from the Demand Stream and write a * if there is a page Fault in front of it.

    I am trying to use a Boolean expression as Salem said but I donít know how to do it? I know thatís going to be the trick as he mention above if there is a new entry write page fault but how to implement it?

    Thanks for your help.

    Code:
    #include <stdio.h>
    #define SIZE 4
    int main(void)
    {
    	int some_data[SIZE] = {0}; //all for values are initialize to false
    	int i;
        printf("Enter Demand Steam: ");
        scanf("%d",some_data);
        
    	printf("FLT   1  2  3  4\n\n");
    	printf("    "); //spaces before the to keep things line up *page faults
    	
          for (i = 0; i < SIZE; i++)
        
        //if(page_fault != some_data);
        printf("  %d", some_data[i]);
        printf("\n\n\n");//spaces after array
        system("pause");
      return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > char demand_stream[30]="1232527353724135637";
    Do you have working code with this as your input?

    If you do, then all you need is
    Code:
    fgets( demand_stream, sizeof demand_stream, stdin );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question Using fgets to do a fifo simulation

    Hi Salem, thank you for been the only one to reply to my posts, I really appreciate it. This is my new code I have and is currently working.
    Code:
     fgets( demand_stream, sizeof deman_stream, stdin);
    I use the fgets Ė but why should I use it to write to a file or to read to a file. Thatís what I am thinking when I implement it. Or can I just use it to read from the variable demand_stream and do the algorithm. What you think is more efficient?

    Can you give me some hints about how I am doing this and what I am doing wrong?
    Using FIFO (with 4 main memory pages) = how do I take the first page value and read

    Do you think i should keep track of your position in the string and remember that the string is actually an array and not write to any external file....

    Thanks

    // demand_stream.c
    Code:
    #include <stdio.h>
    
    /* 
     * This is a program to read a file containing a demand_steam
     * 
     * The data line format is like this:
     * 
     *     Field\tData\n
     * 
     * Where `Field' is white-space-less, and `Data' may contain anything (up to
     * a newline.)  See the file `stored.data'. 
     */
    
    int main(int argc, char *argv[]) {
        FILE *data_file_ptr;     /* The file containing the label data. */
        char line[100];          /* A line read from the data file. */
        char *line_ptr;          /* Used with fgets to detect an EOF or error. */
        char good_file = 0;      /* 0 if the data file is invalid; otherwise 1. */
        char scan_count;         /* Number of matches with sscanf. */
        char keyword[50];        /* A keyword in the data file. */
        char value[50];          /* A value corresponding to a keyword. */
        char inside_record = 0;  /* 1 if we're parsing inside a record. */
        unsigned int labelId;    /* The label unique identifier. */
        unsigned int label_num;  /* Index into `for' loop. */
    
        /*
         * Each label lives inside a `label' structure.  Start out by allowing
         * enough space for 100 of them.
         */
    
        struct label_struct {     
            char demand_stream[50];
             } label[4];
    
        /* Open the data file. */
    
        data_file_ptr = fopen("stored.data", "r");
        if (data_file_ptr == NULL) {
            fprintf(stderr, "%s:  Failed to open file %s for reading.\n",
                argv[0], "stored.data");
            exit(8);
        }
    
        /* Read the records in, assigning them to `label_struct's. */
    
        while (1) {
            line_ptr = fgets(line, sizeof(line), data_file_ptr);       
            if (line_ptr == NULL) 
                break;
            if (strcmp(line, "Labels\tConfiguration file\n") == 0) {
                good_file = 1;  /* This is a good data file. */
    
    #ifdef DEBUG
                printf("debug ==>  The data file is good.\n");
    #endif   
    
            } else if (good_file == 1) {  /* The file is already known good. */
                scan_count = sscanf(line, "%s\t%[^\n]\n", &keyword, &value);
                if (scan_count != 2) {
                    continue;  /* This must have been a blank line. */
    
    #ifdef DEBUG
                    printf("debug ==>  Blank line in data file.\n");
    #endif
    
                }
    
               
    
                if (strcmp(keyword, "LabelID") == 0) {
                    labelId = atoi(&value);  /* Index into `label' array. */
                    continue;                /* Continue to the next line. */
                }
                
                if (strcmp(keyword, "Demand_Stream") == 0) {
                    strcpy(label[labelId].demand_stream, value);
                    continue;                /* Continue to the next line. */
                }     
                   
            }        
        }
        fclose(data_file_ptr);
    
        /* Print out the formatted strings so i can generate FIFO table with page faults */
    
        for (label_num = 0; label_num <= labelId; ++label_num) { 
            printf("--------------------------------------------------\n");
            printf("%s\n", label[label_num].demand_stream);
        }
    		printf("--------------------------------------------------\n");
        return(0);
    }
    //stored.data
    Code:
    Labels	Configuration file
    Version	1
    
    LabelID	0
    Demand_Stream	1
    EndLabel
    
    LabelID	1
    Demand_Stream	2
    EndLabel
    
    LabelID	2
    Demand_Stream	3
    EndLabel
    
    LabelID	3
    Demand_Stream	4
    EndLabel
    
    EndFile
    Wise_ron
    One man's constant is another man's variable

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    So from your rather verbose config file, this would be equivalent in your original code?

    char demand_stream[ ]="1234";

    If it is, then yes you're on the right track.
    Just pull out of the file the bits you need to assemble the data that the rest of the program expects.


    Code:
        while (1) {
            line_ptr = fgets(line, sizeof(line), data_file_ptr);       
            if (line_ptr == NULL) 
                break;
    Is more usually written as
    Code:
    while ( fgets(line, sizeof(line), data_file_ptr) != NULL )
    > scan_count = sscanf(line, "%s\t%[^\n]\n", &keyword, &value);
    A fairly minor nit is that you don't need the & when using %s with true arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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