Holberton School and The Low-Level Algorithms

The memory and the processes

How processes interact with the virtual memory

This image is a combination of Filindmitriy86, and oldschoolroleplaying.

The process

Running ps.

The binary

#include <string.h>             /* strdup */
#include <unistd.h> /* sleep, getpid */
#include <stdio.h> /* printf */
/**
* main - simple program which shows
* the PID and allocated memory
* Return: 0
*/
int main(void)
{
char *msg;
pid_t pid;
int counter;
msg = strdup("Welcome to Holberton");
pid = getpid();
counter = 0;
while(1)
{
printf("[%d] %4d: %s\n", pid, counter++, msg);
sleep(1);
}
}
Creating Infinite program.
[68461]   0: Welcome to Holberton
[68461] 1: Welcome to Holberton
[68461] 2: Welcome to Holberton
.
.
[68637] 1: Welcome to Holberton
[68637] 2: Welcome to Holberton
.
.

/proc

Content of /proc/<PID>.

Let’s meet maps and mem

$ cat maps
Content of maps file.
Virtual memory representation
$ sudo cat mem
cat: mem: Input/output error

Say hi to my little xxd

Fragment of xxd’s manual.

Read the heap

$ cat maps | grep heap
55996d276000-55996d297000 rw-p 00000000 00:00 0 [heap]
$ sudo xxd -s 0x55996d276000 -l $((-0x55996d276000+0x55996d297000)) mem | less
Finding the start of the allocated string.

Edit with dd

The first line of dd’s manual.
$ python3 -c "print(int(0x55996d2762a0))"
94117449654944
$ printf "Welcome to Bogota :)" | sudo dd of=mem conv=notrunc seek=94117449654944 bs=1
20+0 records in
20+0 records out
20 bytes copied, 0.000119827 s, 167 kB/s
...
$
Changing virtual memory.

Conclusions

References

Articles

Thanks

About the Author

Tavo