---
# System prepended metadata

title: Segmentation exercise
tags: [Segmentation, SOA]

---

# Segmentation exercise


```c=
int bigdata[10000]

int main() {
    int index=0;
    
    for (i=0; i<10000; i++) {
        check_and_increment(&bigdata[i]);
    }
    for (i=0; i<10000; i++) {
        printf("%d ", &bigdata[i]);
    }
}

int check_and_increment(int* p){
    if (*p % 10 == 0) {
        *p ++;
    }
    return 0;
}

```

This code is compiled into `test` executable ELF file. From the file we can gather the following information:

```
Code segment size: 8KiB
Code segment offset in file: 0x1000

Global data symbols:
bigdata - 32bit integer, size: 40000 bytes
```

Status of the physical memory:

| Start    | Stop     | Status
| -------- | -------- | --------
| 0x0000   | 0x0FFF   | P3, Code Segment CS=1
| 0x1000   | 0x3FFF   | Free
| 0x4000   | 0x4100   | P3, Stack Segment SS=3
| 0x4101   | 0x8FFF   | Free
| 0x9000   | 0xAFFF   | Free
| 0xB000   | 0xFFFF   | Kernel

## Scenario #1
The process `P0` is forked. Describe how the segment descriptor and the physical memory will look like after the following:
- Code segment will be `CS=0` is fully loaded
- Data segment `DS=1` is not loaded (not present)
- Stack segment `SS=2` is not loaded (not present)
- The code execution has not yet started

:::spoiler One possible answer

Status of the physical memory:

| Start    | Stop     | Status
| -------- | -------- | --------
| 0x0000   | 0x0FFF   | P3, Code Segment CS=1
| 0x1000   | 0x2FFF   | P2, Code Segment CS=0
| 0x3000   | 0x3FFF   | Free
| 0x4000   | 0x4100   | P3, Stack Segment SS=3
| 0x4101   | 0x8FFF   | Free
| 0x9000   | 0xAFFF   | Free
| 0xB000   | 0xFFFF   | Kernel

| #Segment | Base     | Limit    | Misc               |
| -------- | -------- | -------- | ---------------    |
| 0        | 0x1000   | 25600    | Code, Present      |
| 1        |          | 40000    | Data, Not Present  |
| 2        |          |          | Stack, Not Present |

Limit is expressed in bytes
:::

## Scenario #2

The process starts executing. When execution reaches line number 7, the Memory Management Unit (MMU) interrupts the CPU, claiming `SS=2` is not present. Explain what is happening and give some indications of what will the operating system do in order to resume the execution of the process.

## Scenario #3

After solving the previous scenario, the code continues until the line 15. When trying to do read from the memory position pointed by `p`, the MMU interrupts again. Explain what is happening and give some indications of what will the operating system do in order to resume the execution of the process.

## Scenario #4

In some other part of the code, the process tries to execute the next fragment of code:

```c=
    int* n;
    ...
    ...
    n[0] = 10;
```

The variable `n` is a pointer to integer, but is has not been initialized, therefore, its value is 0. If this variable has been assigned to `DS=3`, what would happen then?

###### tags: `SOA` `Segmentation`