In Computer Operating Systems

download In Computer Operating Systems

of 5

Transcript of In Computer Operating Systems

  • 8/3/2019 In Computer Operating Systems

    1/5

    In computer operating systems, paging is one of the memory-management schemes by which acomputer can store and retrieve data from secondary storage for use in main memory. In the

    paging memory-management scheme, the operating system retrieves data from secondary storagein same-size blocks called pages . The main advantage of paging over memory segmentation isthat it allows the physical address space of a process to be noncontiguous . Before the time pagingwas used, systems had to fit whole programs into storage contiguously , which caused variousstorage and fragmentation problems.

    Static memory allocation refers to the process of allocating memory at compile-time before theassociated program is executed, unlike dynamic memory allocation or automatic memoryallocation where memory is allocated as required at run-time .[1]

    An application of this technique involves a program module (e.g. function or subroutine )declaring static data locally, such that these data are inaccessible in other modules unlessreferences to it are passed as parameters or returned. A single copy of static data is retained andaccessible through many calls to the function in which it is declared. Static memory allocationtherefore has the advantage of modularising data within a program design in the situation wherethese data must be retained through the runtime of the program.

    Dynamic allocation is a pretty unique feature to C (amongst high level languages). It enables usto create data types and structures of any size and length to suit our programs need within the

    program.

    We will look at two common applications of this:

    dynamic arrays dynamic data structure e.g. linked lists

    Malloc, Sizeof, and Free

    The Function malloc is most commonly used to attempt to ``grab'' a continuous portion of memory. It is defined by:

    void *malloc(size_t number_of_bytes)

    That is to say it returns a pointer of type void * that is the start in memory of the reserved portion of size number_of_bytes . If memory cannot be allocated a NULL pointer is returned.

    Since a void * is returned the C standard states that this pointer can be converted to any type.The size_t argument type is defined in stdlib.h and is an unsigned type .

    So:

    char *cp;cp = malloc(100);

    http://en.wikipedia.org/wiki/Computerhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Memory_managementhttp://en.wikipedia.org/wiki/Memory_segmentationhttp://en.wikipedia.org/wiki/Contiguous#Computer_sciencehttp://en.wikipedia.org/wiki/Contiguouslyhttp://en.wikipedia.org/wiki/Computer_data_storagehttp://en.wikipedia.org/wiki/Fragmentation_(computer)http://en.wikipedia.org/wiki/Compile_timehttp://en.wikipedia.org/wiki/Compile_timehttp://en.wikipedia.org/wiki/Dynamic_memory_allocationhttp://en.wikipedia.org/wiki/Dynamic_memory_allocationhttp://en.wikipedia.org/wiki/Automatic_memory_allocationhttp://en.wikipedia.org/wiki/Automatic_memory_allocationhttp://en.wikipedia.org/wiki/Automatic_memory_allocationhttp://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase)http://en.wikipedia.org/wiki/Static_memory_allocation#cite_note-0http://en.wikipedia.org/wiki/Static_memory_allocation#cite_note-0http://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Parameters_(computer_science)http://en.wikipedia.org/wiki/Parameters_(computer_science)http://en.wikipedia.org/wiki/Computerhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Memory_managementhttp://en.wikipedia.org/wiki/Memory_segmentationhttp://en.wikipedia.org/wiki/Contiguous#Computer_sciencehttp://en.wikipedia.org/wiki/Contiguouslyhttp://en.wikipedia.org/wiki/Computer_data_storagehttp://en.wikipedia.org/wiki/Fragmentation_(computer)http://en.wikipedia.org/wiki/Compile_timehttp://en.wikipedia.org/wiki/Dynamic_memory_allocationhttp://en.wikipedia.org/wiki/Automatic_memory_allocationhttp://en.wikipedia.org/wiki/Automatic_memory_allocationhttp://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase)http://en.wikipedia.org/wiki/Static_memory_allocation#cite_note-0http://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Parameters_(computer_science)
  • 8/3/2019 In Computer Operating Systems

    2/5

    attempts to get 100 bytes and assigns the start address to cp.

    Also it is usual to use the sizeof() function to specify thenumber of bytes:

    int *ip;ip = (int *) malloc(100*sizeof(int));

    Some C compilers may require to cast the type of conversion. The(int *) means coercion to an integer pointer. Coercion to thecorrect pointer type is very important to ensure pointerarithmetic is performed correctly. I personally use it as a meansof ensuring that I am totally correct in my coding and use castall the time.

    It is good practice to use sizeof() even if you know the actualsize you want -- it makes for device independent (portable) code.

    sizeof can be used to find the size of any data type, variable orstructure. Simply supply one of these as an argument to thefunction.

    SO:

    int i;struct COORD {float x,y,z};typedef struct COORD PT;

    sizeof(int), sizeof(i),sizeof(struct COORD) andsizeof(PT) are all ACCEPTABLE

    In the above we can use the link between pointers and arrays totreat the reserved memory like an array. i.e we can do thingslike:

    ip[0] = 100;

    or

    for(i=0;i

  • 8/3/2019 In Computer Operating Systems

    3/5

    The function free() takes a pointer as an argument and frees thememory to which the pointer refers.

    Calloc and Realloc

    There are two additional memory allocation functions, Calloc() and Realloc() . Their prototypes are given below:

    void *calloc(size_t num_elements, size_t element_size};

    void *realloc( void *ptr, size_t new_size);

    Malloc does not initialise memory (to zero ) in any way. If you wish to initialise memory thenuse calloc . Calloc there is slightly more computationally expensive but, occasionally, moreconvenient than malloc. Also note the different syntax between calloc and malloc in thatcalloc takes the number of desired elements, num_elements , and element_size,element_size , as two individual arguments.

    Thus to assign 100 integer elements that are all initially zero you would do:

    int *ip;ip = (int *) calloc(100, sizeof(int));

    Realloc is a function which attempts to change the size of aprevious allocated block of memory. The new size can be larger orsmaller. If the block is made larger then the old contents remainunchanged and memory is added to the end of the block. If thesize is made smaller then the remaining contents are unchanged.

    If the original block size cannot be resized then realloc willattempt to assign a new block of memory and will copy the oldblock contents. Note a new pointer (of different value) willconsequently be returned. You must use this new value. If newmemory cannot be reallocated then realloc returns NULL.

    Thus to change the size of memory allocated to the *ip pointerabove to an array block of 50 integers instead of 100, simply do:

    ip = (int *) calloc( ip, 50);

    Linked Lists

    Let us now return to our linked list example:

    typedef struct { int value;ELEMENT *next;

    } ELEMENT;

  • 8/3/2019 In Computer Operating Systems

    4/5

    We can now try to grow the list dynamically:

    link = (ELEMENT *) malloc(sizeof(ELEMENT));

    This will allocate memory for a new link.

    If we want to deassign memory from a pointer use the free()function:

    free(link)

    In a paging system, programs and data stored on disk are divided into equal, fixed sized blocks called pages, and main memory is divided into blocks of the same size calledframes. Exactly one page can fit in one frame.

    Physical memory is divided into parts called FRAME and logical memory is divided into parts called PAGE.

    Segment was the old way of dividing memory such that programs can be easily relocatedfrom one memory location to another without any modification. It was introduced with8086 microprocessor. The memory address was represented by a segment:offset pair andthe actual memory location was found by a simple calculation:

    Memory Address = (Segment * 16) + Offset

    This calculation applies to 16-bit computing with 20-bit address lines. Both segment andoffset are 16-bits long which makes total memory accessible in one segment a maximumof 64 KB.

    In 80386, this calculation was overridden in favor of segment descriptors which wereavailable only in protected mode. The segment registers point to these descriptors which

    point to the start of actual memory location. This allowed upto 4 GB of memory to beaddressed in the same segment.

    The memory blocks pointed by these descriptors are a group of pages. Pages are usually

    of 4 KB of size and they can be present anywhere in the physical memory. They can also be easily swapped to the hard-disk and back into the memory at a different locationwithout causing any harm to the running program. Virtual Memory Manager of Processor and OS take care of appropriately loading pages to/from the swap device (usually harddisk).

    Frame is usually a page to which you have a point of reference. It is commonly used tomark positions of data in stack segment.

  • 8/3/2019 In Computer Operating Systems

    5/5