It is therefore convenient to use separate segments for logically separate

It is therefore convenient to use separate segments

This preview shows page 57 - 60 out of 102 pages.

are made on each segment. It is therefore convenient to use separate segments for logically separate parts of a program/process. Code segment - program code Data segment - the program stack and dynamically allocated data. Arrays can conveniently be placed in a segment of their own - that way, array bound-checking will be performed automatically by the hardware of the system.
Image of page 57
Figure 5.3: The UNIX process model, showing the various segments used by each process. The stack contains all local (automatic) variables and the heap is allocated by malloc() . The segment idea can all be built on top of the page/frame concept above by demanding that segments be a whole number of pages. That way, we retain the advantages of the page system. Segmentation is an additional overhead which relates to the sharing of logical memory between processes. The page overhead relates to the mapping of logical to physical addresses. Memory addressing with segments is like plotting points in a plane with coordinates . Addresses are written (segment,offset) . 5.1.8 The malloc() function The operator new which dynamically allocates memory is a wrapper function for the C library function malloc() . When we use new , the compiler translates this into a call to malloc() . As an example, let's ask what happens when we call the function
Image of page 58
malloc() . malloc is part of the standard C library on any system, but we shall only be concerned with how it is implemented in BSD UNIX. The function is used to obtain a pointer to (the address of) a block of memory bytes long. For example, pointer= malloc(n); Since malloc is a user-level command, it obtains logical memory for the caller. The acquisition of physical memory is taken care or by the system on behalf of malloc , by deeper level kernel commands. In order to obtain bytes of memory, malloc must normally acquire too much memory. This is because the smallest unit of memory is a page. This when malloc is called, it checks to see if the data segment of the current process has free bytes. If the space already exists within the pages already allocated to the process, malloc uses this space and updates the free-memory list. If there is not sufficient space, malloc makes a call to the brk() function, which tries to extend the size of the data segment. In most cases, not all the memory obtained is required. The most extreme example would be the allocation of one char variable (one single byte). Then the remainder of the page is free, and is added to the free memory list. The next time malloc is called, it tries to use the remainder of the last allocated page, or any memory in the same segment which it allocated earlier, but has since been freed. The fact that malloc divides up pages of logical memory is of no consequence to the memory management system, since each process maintains its own free memory list for the data segment. Since the segment always consists of a whole number of pages there is no conflict with the page mapping algorithm.
Image of page 59
Image of page 60

You've reached the end of your free preview.

Want to read all 102 pages?

  • One '20

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes