1        /************** mem.c ****************/
 2        #define MIDAPAGINA 4096
 3        int matrix[MIDAPAGINA*16];
 4        void fgolafre(int j) {
 5        int i;
 6        for (i=j;i<MIDAPAGINA*16;i++) {
 7        matrix[i]=23;
 8        }
 9        }
10        int main() {
11        int *vector;
12        vector=(int*)malloc(MIDAPAGINA*8);
13        if (fork()==0) {
14        execlp("miprog","miprog",NULL);
15        }
16        else if (fork()==0) {
17        vector[4]=73;
18        exit(0);
19        }
20        fgolafre(MIDAPAGINA*4);
21        waitpid(-1,NULL,0);
22        waitpid(-1,NULL,0);
23        free(vector);
24        return 0;
25        }
26
  1. A quina regió de memòria està el contingut de la variable matrix? I el contingut de vector?

    Resposta

    matrix és una variable global no inicialitzada, per tant, estarà a la zona de dades.

    vector és una variable local, per tant, estarà a la pila, però el seu contingut estarà el heap.

  2. Quines regions de memòria es modificaran en executar-se la crida a la funció fgolafre?

    Resposta

    El codi de la funció recorre part de matrix, que és a la zona de dades. Però també té paràmetres i variables locals que són a la pila.

  3. Just després del malloc, part del contingut del fitxer maps del procés pare és el següent:

/proc/29390$ cat maps
55a6e75e9000-55a6e75ea000 r-xp 00000000 08:01 1074816         /home/alumne/final/mem
55a6e77e9000-55a6e77ea000 r--p 00000000 08:01 1074816         /home/alumne/final/mem
55a6e77ea000-55a6e77eb000 rw-p 00001000 08:01 1074816         /home/alumne/final/mem
55a6e9248000-55a6e9269000 rw-p 00000000 00:00 0               [heap]
7fff35c97000-7fff35cb8000 rw-p 00000000 00:00 0               [stack]
  1. Quantes pàgines mesura la regió del heap? Quant feia abans del malloc?

    Resposta
    stem:[55a6e9269000 - 55a6e9248000 = 21000], és a dir, stem:[21_16] páginas (1000 en hexa és 4096 en base 10). Moltes més de les 8 demanades. Abans d'aquesta línia el heap no existia.
  2. Quin procés provoca més fallades de pàgina a causa de l’ús de COW? Si no hi hagués COW, quin procés seria el més perjudicat?

Resposta
 El procés pare escriu a més pàgines compartides, cosa que provocarà errors de pàgina per demanar noves pàgines per al procés fill i copiar-les.
Si no hi hagués COW, el primer procés fill seria el més perjudicat quant a temps de creació al fork. Caldria demanar memòria i copiar totes les pàgines del pare, per immediatament rebutjar-les en canviar la imatge del procés a la crida a execlp.
Region Memory

Text

1 page

Data

3 pages

Stack

1 page

Heap

0 pages

JUSTIFICACTION: There is only one process in execution at this point. The size of the Code is 2KB , but the assignment unit is a page (and different regions do not share pages) for this reason, we need a whole page for the text region. We have just one global variable: buffer1, which is an array of 2500 integers (10000 Bytes), so we need 3 pages for the region of the global variables (Data). The region of local variables (Stack) only holds 1 pointer and 2 integers (12 Bytes), so this region fits in one page. At this ppoint,the program has not allocated dynamic memory, so the heap is empty.

Table 2. Point B
Region Memory
Answer
Region Memory

Text

1 page

Data

3 pages

Stack

2 pages

Heap

0 pages

JUSTIFICATION: In this point the parent has allocated and initialized a dynamic memory region to store 2500 integers (10000 Bytes), so we need 3 pages for the heap. In addition, a new process has been created. The system implements the COW optimization and thus, parent and child share physical memory until some of them writes into it. In this point, only the variable ret has been modified (which is stored in the Stack), for this reason, the stack is the only region duplicated.

Table 3. Point C
Region Memory
Answer
Region Memory

Text

1 page

Data

6 pages

Stack

2 pages

Heap

3 pages

JUSTIFICATION: In this point, child process has finished the execution, so we only need memory to store the address space of the parent. This memory is the same used at point B because the process has not modified the heap.

Table 4. Point D
Region Memory
Answer
Region Memory

Text

1 page

Data

3 pages

Stack

1 page

Heap

3 pages

JUSTIFICATION: In this point, child process has finished the execution, so we only need memory to store the address space of the parent. This memory is the same used at point B because the process has not modified the heap.

[user@login]$ ./memoria
Limite 0x1971000
Limite 0x1975000
Limite XXXXXXXXX
Limite YYYYYYYYY

Contesta a las siguientes preguntas:

  1. Rellena las líneas punteadas con la respuesta correcta.

    1. La variable p1 está en la región de memoria …​

      Respuesta

      data

    2. la variable limit está en la región de memoria …

      Respuesta

      pila/stack

    3. El valor que veremos en las XXXXXXXX es …​

      Respuesta

      0x1971000

    4. en las YYYYYYYYY es …

      Respuesta

      0x1971000

      Si nos dan la siguiente información: el tamaño de la pila es 4096 bytes, el código de este programa ocupa 1000 bytes, la variable p1 ocupa 8 bytes y el tamaño de un int son 4 bytes.

    5. ¿Cuantos marcos de página (páginas físicas) tendrán reservados en el punto A para la región de código?

      Respuesta

      El código se puede compartir. Como las páginas se reservan completas será 1 página en total.

    6. ¿Cuantos marcos de página (páginas físicas) tendrán reservados en el punto A para la región de data?

      Respuesta

      P1 es una variable global que se inicializa antes del fork por lo que una única página será suficiente. Como no se modifica, no es necesario reservar una privada por proceso por lo que en total será una única página.