CSE 460 Lab 7: Process Memory Addresses Highlights of this lab: 1. Preamble 2. User Versus Kernel Space 3. Three Segments of User Space 4. Lab Exercise 1. Preamble Every Linux process has its own...

1 answer below »
Working for a full-time job and being an essential worker couldn't make it in time in deadlines. Just follow the instruction below turn in Code that asks for in pdf.


CSE 460 Lab 7: Process Memory Addresses Highlights of this lab: 1. Preamble 2. User Versus Kernel Space 3. Three Segments of User Space 4. Lab Exercise 1. Preamble Every Linux process has its own dedicated memory address space. The kernel maps these "virtual" addresses in the process address space to actual physical memory addresses as necessary. In other words, the data stored at address 0xDEADBEEF in one process is not necessarily the same as the data stored at the same address in another process. When an attempt is made to access the data, the kernel translates the virtual address into an appropriate physical address in order to retrieve the data from memory... we are not really concerned with physical addresses. Rather, we are investigating exactly how the operating system allocates more virtual addresses to a process. 2. User Versus Kernel Space A process may operate in user mode or kernel mode. The programs that we have been programming actually switch between these two modes. When a process changes mode, it is termed a context switch. To summarize these two modes: 1. User Mode application processes. These are executed in an isolated environment so that multiple processes cannot interfere with each other's resources. 2. Kernel Mode when the kernel acts on behalf of the process. This would be when a system call is made, or when an exception or interrupt occurs. Processes running in kernel mode are privileged and have access to key system data structures. When a program is loaded as a process, it is allocated a section of virtual memory which is known as user space. By contrast, there is another section of memory which is known as the kernel space. This is where the kernel executes and provides its services. The remainder of these notes focus on user space. 3. Three Segments of User Space The user context of a process is made up of the portions of the address space that are accessible to the process while it is running in user mode. There are a few definitions that come in handy when we are talking about memory: global variable — one declared outside of any function local static variable — one declared inside a function with the static keyword. Such a variable keeps its value across function calls. For instance, if it has the value 57 after you return from the function call, when that function is called again, it will still be 57. local automatic variable — any variable declared inside a function without the static keyword heap—is dynamically allocated space. In C, you use malloc or calloc In C++, you use new The portions of address space are: text, data, and stack. They are described in further detail below: 1. Text sometimes called the instruction or code segment contains executable program code and constant data is read only multiple processes can share this segment if a second copy of the program is to be executed concurrently 2. Data contiguous (in a virtual sense) with the text segment subdivided in to three sections: i. initialized data (static or global variables) ii. uninitialized data (static or global variables) iii. heap (dynamically allocated) addresses increase as the heap grows 3. Stack used for function call information including: address to return to the values or addresses of all parameters local automatic variables addresses decrease as the stack grows Notice that the stack grows towards the uninitialized data and the heap grows towards the stack. Some compilers and linkers call uninitialized data and heap bss (Block Started by Symbol—not bs) for historical reasons. Memory references to Text, Data and Stack in a user space program are done with virtual addresses. The kernel translates these virtual addresses to physical memory addresses. In working with these virtual addresses, you have access to three external variables: etext—first valid address above the text segment edata—first valid address above initialized data segment end—first valid address above the uninitialized data segment The following program, in Interprocess Communications in UNIX: The Nooks & Crannies, provides an example of displaying these external variables. The program also displays the address of some key identifiers to verify that the identifiers (variables) are put into the correct segments. // Example from Interprocess Communications in UNIX: The Nooks & Crannies. // memoryexample.cpp #include #include //needed for exit() #include #include #include #define SHOW_ADDRESS(ID, I) printf("The id %s \t is at:%8X\n", ID, &I) extern int etext, edata, end; char *cptr = "Hello World\n"; // static by placement char buffer1[25]; int main(void) { void showit(char *); // function prototype int i=0; //automatic variable, display segment adr printf("Adr etext: %8X\t Adr edata: %8X\t Adr end: %8X\n\n", &etext, &edata, &end); // display some addresses SHOW_ADDRESS("main", main); SHOW_ADDRESS("showit", showit); SHOW_ADDRESS("cptr", cptr); SHOW_ADDRESS("buffer1", buffer1); SHOW_ADDRESS("i", i); strcpy(buffer1, "A demonstration\n"); // library function write(1, buffer1, strlen(buffer1) + 1); // system call for (; i<1; ++i)="" showit(cptr);="" *="" function="" call="" */="" return="" 0;="" }="" void="" showit(char="" *p)="" {="" char="" *buffer2;="" show_address("buffer2",="" buffer2);="" buffer2="(char" *)malloc(strlen(p)+1);="" if="" (buffer2="" !="NULL)" {="" strcpy(buffer2,="" p);="" copy="" the="" string="" printf("%s",="" buffer2);="" display="" the="" string="" free(buffer2);="" release="" location="" }="" else="" {="" printf("allocation="" error.\n");="" exit(1);="" }="" }="" a="" sample="" run="" on="" a="" linux="" machine="" produced="" the="" following="" output:="" adr="" etext:="" 4013d5="" adr="" edata:="" 404068="" adr="" end:="" 4040a0="" the="" id="" main="" is="" at:="" 401196="" the="" id="" showit="" is="" at:="" 4012b3="" the="" id="" cptr="" is="" at:="" 404060="" the="" id="" buffer1="" is="" at:="" 404080="" the="" id="" i="" is="" at:1ff8134c="" a="" demonstration="" the="" id="" buffer2="" is="" at:1ff81328="" hello="" world="" 4.="" lab="" exercise="" the="" purpose="" of="" this="" lab="" is="" to="" explore="" the="" memory="" associated="" with="" the="" user="" process:="" text,="" data,="" and="" stack.="" 1.="" copy="" and="" paste="" the="" following="" source="" code:="" memory_segments.c="" #include=""> #include int g0; /* global variable, uninitialized */ int g1 = 14; /* global variable, initialized */ int g2[1500]; /* global variable, uninitialized */ int g3 = 16; /* global variable, initialized */ int g4; /* global variable, uninitialized */ void proc1(); void proc2(); int main() { extern int etext[], edata[], end[]; int lc0; /* local variable, stored on stack */ int lc1 = 27; /* local variable, stored on stack; mix init and uninit */ int lc2; /* local variable, stored on stack */ static int ls0; /* local static variable, uninitialized data */ static int ls1 = 19; /* local static variable, initialized data */ int *pheap1; int *pheap2; pheap1 = (int *) malloc(sizeof(int)); pheap2 = (int *) malloc(sizeof(int)); printf("\n\n---------- main -------------------\n\n"); printf("%14p (%15lu): Last address\n", 0xffffffffffff, 999999999999999); printf("%14p (%15lu): Address etext\n", etext, etext); printf("%14p (%15lu): Address edata\n", edata, edata); printf("%14p (%15lu): Address end\n", end, end); printf("%14p (%15lu): Address of code for proc1\n", &proc1, &proc1); printf("%14p (%15lu): Address of code for proc2\n", &proc2, &proc2); printf("%14p (%15lu): Address of uninitialized global variable g0\n", &g0, &g0); printf("%14p (%15lu): Address of initialized global variable g1\n", &g1, &g1); printf("%14p (%15lu): Address of uninitialized global array g2\n", &g2[0], &g2[0]); printf("%14p (%15lu): Address of initialized global variable g3\n", &g3, &g3); printf("%14p (%15lu): Address of uninitialized global variable g4\n", &g4, &g4); printf("%14p (%15lu): Address heap1 in heap space\n", pheap1, (unsigned long) pheap1); printf("%14p (%15lu): Address heap2 in heap space\n", pheap2, (unsigned long) pheap2); printf("%14p (%15lu): Address of local variable lc0\n", &lc0, &lc0); printf("%14p (%15lu): Address of local variable lc1\n", &lc1, &lc1); printf("%14p (%15lu): Address of local variable lc2\n", &lc2, &lc2); printf("%14p (%15lu): Address of local uninitialized static var ls0\n", &ls0, &ls0); printf("%14p (%15lu): Address of local initialized static var ls1\n", &ls1, &ls1); proc1(); proc2(); return 0; } void proc1() { int lc3; int lc4 = 37; printf("\n\n----------- proc1 ------------------\n\n"); printf("%14p (%15lu): Address of code for proc1\n", &proc1, &proc1); printf("%14p (%15lu): Address of global variable g0\n", &g0, &g0); printf("%14p (%15lu): Address of global variable g1\n", &g1, &g1); printf("%14p (%15lu): Address of global variable g2\n",
Answered Same DayJun 02, 2021

Answer To: CSE 460 Lab 7: Process Memory Addresses Highlights of this lab: 1. Preamble 2. User Versus Kernel...

Ria answered on Jun 04 2021
128 Votes
lab7log.txt
---------- main -------------------
0xffffffffffff (999999999999999): Last address
0x55c43f96dded ( 94301368802797): Address etext
0x55c43fb6f020 ( 94301370904608): Address edata
0x55c43fb707e0 ( 94301370910688): Address end
0x55c43f96dac6 ( 94301368801990): Address of code for proc1
0x55c43f96dbf9 ( 94301368802297): Address of code for
proc2
0x55c43fb6f040 ( 94301370904640): Address of uninitialized global variable g0
0x55c43fb6f010 ( 94301370904592): Address of initialized global variable g1
0x55c43fb6f060 ( 94301370904672): Address of uninitialized global array g2
0x55c43fb6f014 ( 94301370904596): Address of initialized global variable g3
0x55c43fb707d0 ( 94301370910672): Address of uninitialized global variable g4
0x55c43fd3f260 ( 94301372805728): Address heap1 in heap space
0x55c43fd3f280 ( 94301372805760): Address heap2 in heap space
0x7fff11ebd3b8 (140733494055864): Address of local variable lc0
0x7fff11ebd3bc (140733494055868): Address of local variable lc1
0x7fff11ebd3c0 (140733494055872): Address of local variable lc2
0x55c43fb707d4 ( 94301370910676): Address of local uninitialized static var ls0
0x55c43fb6f018 ( 94301370904600): Address of local initialized static var ls1
----------- proc1 ------------------
0x55c43f96dac6 ( 94301368801990): Address of code for proc1
0x55c43fb6f040 ( 94301370904640): Address of global variable g0
0x55c43fb6f010 ( 94301370904592): Address of global variable g1
0x55c43fb6f060 ( 94301370904672): Address of global variable g2
0x55c43fb6f014 ( 94301370904596): Address of global variable g3
0x55c43fb707d0 ( 94301370910672): Address of global variable g4
0x7fff11ebd390 (140733494055824): Address of local variable lc3
0x7fff11ebd394 (140733494055828): Address of local variable lc4
------------ proc2 -----------------
0x55c43f96dbf9 ( 94301368802297): Address of code for proc2
0x55c43fb6f040 ( 94301370904640): Address of global variable g0
0x55c43fb6f010 ( 94301370904592): Address of global variable g1
0x55c43fb6f060 ( 94301370904672): Address of global variable g2
0x55c43fb6f014 ( 94301370904596): Address of global variable g3
0x55c43fb707d0 ( 94301370910672): Address of global variable g4
0x7fff11ebd390 (140733494055824): Address of local variable lc5
0x7fff11ebd394 (140733494055828): Address of local variable lc6
0x55c43fb707d8 ( 94301370910680): Address of local uninitialized static var ls2
0x55c43fb6f01c ( 94301370904604): Address of local initialized static var ls3
------------ Factorial -------------
0x55c43f96d78a ( 94301368801162): Address of code for factorial
0x7fff11ebd3c4 (140733494055876): Address of local variable n
10! = 3628800
---------- main -------------------
0xffffffffffff (999999999999999): Last address
0x557242d7aded ( 93949236063725): Address etext
0x557242f7c020 ( 93949238165536): Address edata
0x557242f7d7e0 ( 93949238171616): Address end
0x557242d7aac6 ( 93949236062918): Address of code for proc1
0x557242d7abf9 ( 93949236063225): Address of code for proc2
0x557242f7c040 ( 93949238165568): Address of uninitialized global variable g0
0x557242f7c010 ( 93949238165520): Address of initialized global variable g1
0x557242f7c060 ( 93949238165600): Address of uninitialized global array g2
0x557242f7c014 ( 93949238165524): Address of initialized global variable g3
0x557242f7d7d0 ( 93949238171600): Address of uninitialized global variable g4
0x55724493b260 ( 93949265162848): Address heap1 in heap space
0x55724493b280 ( 93949265162880): Address heap2 in heap space
0x7ffd976643b8...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here