联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-08-31 10:31


Question 3 (50%): The Memory Layout of a Process (Back to Beginning)

The ultimate aim of this question is to find out how your Unix system layouts different parts of your process in the virtual memory. The question consists of three parts. The first part is to complete a program that prints out the memory addresses of various components of the process (eg, global variables, local variables, dynamically allocated variables, functions, command line arguments, and environment variables). This program is then used in the second part to recreate a snapshot of the memory layout of the process. Finally in the third, you will answer a number of questions related to the memory management based on your observation and analysis.

1.(10 marks) Complete the following C program, memory.c, so that it displays the addresses of

a.functions f1, f2, and main;

b.all string literals such as "Hello, world!";

c.all initialized global variables;

d.all un-initialized global variables;

e.all dynamically allocated memories;

f.all formal parameters in functions;

g.all local variables;

h.start and end of its command line arguments;

i.start and end of its environment (variables and values).

2./*

3. *  name;memory.c

4. */

5.

6.#include <stdlib.h>

7.#include <stdio.h>

8.#include <math.h>

9.#include <string.h>

10.

11.extern char **environ;

12.

13.int global_x = 10;  // initialised global variable

14.int global_y;       // un-initialised global variable

15.char global_array1[] = "Hello, world!"; // initialised global array

16.char global_array2[10];  // un-initialised global array

17.char *global_pointer1 = "bye!";  // global pointer to a string literal

18.char *global_pointer2;  // un-initialised global pointer

19.float global_float = 100.1;// initialised global variable

20.double global_double;// un-initialised global variable

21.

22.int f2(int x)

23.{

24.    char * f2_p;

25.    int f2_x = 21;

26.

27.    f2_p = malloc(1000);         // dynamically allocated memory

28.

29.    // print out the address of x

30.    // print out the addresses of f2_p, and f2_x

31.    // print out the starting address of the dynamically allocated memory

32.    .....

33.  

34.    L: f2_x = 10;

35.    return f2_x;

36.}

37.

38.void f1(int x1, int x2, float x3, char x4, double x5, int x6)

39.{

40.    int f1_x = 10;

41.    int f1_y;

42.    char *f1_p1 = "This is inside f1";   // pointer to another string literal

43.    char *f1_p2;

44.

45.    f1_p2 = malloc(100);         // dynamically allocated memory

46.

47.    // print out the addresses of x1, x2, x3, x4, x5, x6

48.    // print out the addresses of f1_x, f1_y, f1_p1, f1_p2

49.    // print out the address of the string literal "This is inside f1"

50.    .....

51.

52.    f1_y = f2(10);

53.    return;

54.}

55.

56.int main(int argc, char *argv[])

57.{

58.    

59.    printf("My OS bit size: %lu\n", sizeof(void *) * 8);

60.

61.    // print out the addresses of argc, argv

62.    // print out the starting address and end address of the command line arguments of this process

63.    // print out the starting address and end address of the environment of this process

64.    // print out the starting addresses of function main, f1, and f2

65.    // print out the addresses of global_x, global_y, global_array1, global_array2, global_pointer1,

66.    //           global_pointer2, global_float, global_double

67.    // print out the addresses of string literals "Hello, world!" and "bye"

68.

69.    .....

70.

71.    // call function f1 with suitable arguments such as 12, -5, 33.7, 'A', 1.896e-10, 100

72.    f1( .... );

73.

74.    exit(0);

75.}

The following example shows how to display the virtual addresses in hexadecimal notation, eg:

int x;

char *p="this is a string literal";


printf("address of variable x starts at %p\n",  &x));

printf("address of variable p starts at %p\n",  &p));

printf("address of the string literal starts at %p\n", p);


76.(30 marks) Based on the information generated from the above program, produce a memory map table showing the layout of literals, initialised global variables, uninitialised global variables, formal parameters of each function, local variables, dynamically allocated variables, functions, environment and command line arguments in the memory when the program runs at label L in function f2.

The memory map table must show the addresses of each variable, literal, and function and their sizes. It should also show the start and end addresses of the environment and the command line arguments and their sizes.

The memory map table must contain at least the following columns:

a.The start address of an entity such as a variable or a function

b.The length of the storage space of the entity in bytes

c.The name of the entity, such as global_pointer1 or Hello, world!

d.The nature of the entity, such as function, or uninitialsed global variable

e.The memory section, eg, environment, command line arguments, code (or text), global initialised data, global uninitialed data, stack, heap etc.

In addition, you must use seven different background colours to highlight the following seven memory sections as indicated below::

[Green] initialised global variables (including constants and literals)

[Red] uninitialised global variables

[Blue] stack (containing the local variables and returning addresses of function calls)

[Magenta] heap (containing the dynamically allocated memories)

[Yellow] code (functions)

[Cyan] process environment

[White] command line arguments

Please also note that in your memory table, the memory addresses must be strictly sequential, from the lowest address to the highest address to reflect how different components of a running program are layout in the virtual memory. Your memory table will not be accepted if the addresses are not lined up sequentially in the table. If you find that components from one section are split in more than one continuous area of memory, it is a sure indication that there is something wrong with your memory map and you should find out what went wrong and fix it.

(10 marks) Based on the experiment and analysis you have carried out in 1 and 2 above, answer the following questions:

.what is the size of the virtual address space of that process?

a.how does the operating system on your machine layouts the following process components in the virtual address space: command line arguments, environment, literals, initialised global variables, uninitialised global variables, functions, formal parameters and local variables of a function, and dynamically allocated variables (or memories)?


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp