#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
/* Adder function*/
int z =10;
z = x + y;
return z;
}
main(int argc, char **argv)
{
/* Takes two user input from command line converts them into integer */
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c;
char buffer[100];
/* Gets user input */
gets(buffer);
puts(buffer);
c = add(a,b);
printf("Sum of %d + %d = %d\n",a,b,c);
exit(0);
}
We then run the above program on the command line.
%./tst4 3 4
Then we get the process id of the running program tst4 by running
%ps -aux |grep tst4
user 26709 0.0 0.0 2012 284 pts/12 S+ 23:48 0:00 ./tst4 4 5
user 26812 0.0 0.0 4396 824 pts/2 S+ 23:51 0:00 grep --color=auto tst4
The program tst4 has a running PID of 26709. Now if we have to see the memory map of process 26709 we just need to peek in the linux /proc/26709 directory at the file maps and we will see the following memory map of the process
user @ubuntu:/proc/26709$ more maps
08048000-08049000 r-xp 00000000 08:01 1212250 /home/user/GDB/tst4
08049000-0804a000 r--p 00000000 08:01 1212250 /home/user/GDB/tst4
0804a000-0804b000 rw-p 00001000 08:01 1212250 /home/user/GDB/tst4
b75bd000-b75be000 rw-p 00000000 00:00 0
b75be000-b7762000 r-xp 00000000 08:01 1461785 /lib/i386-linux-gnu/libc-2.15.so
b7762000-b7763000 ---p 001a4000 08:01 1461785 /lib/i386-linux-gnu/libc-2.15.so
b7763000-b7765000 r--p 001a4000 08:01 1461785 /lib/i386-linux-gnu/libc-2.15.so
b7765000-b7766000 rw-p 001a6000 08:01 1461785 /lib/i386-linux-gnu/libc-2.15.so
b7766000-b7769000 rw-p 00000000 00:00 0
b777f000-b7782000 rw-p 00000000 00:00 0
b7782000-b7783000 r-xp 00000000 00:00 0 [vdso]
b7783000-b77a3000 r-xp 00000000 08:01 1461799 /lib/i386-linux-gnu/ld-2.15.so
b77a3000-b77a4000 r--p 0001f000 08:01 1461799 /lib/i386-linux-gnu/ld-2.15.so
b77a4000-b77a5000 rw-p 00020000 08:01 1461799 /lib/i386-linux-gnu/ld-2.15.so
bfccf000-bfcf0000 rw-p 00000000 00:00 0 [stack]
Notice how the source code of the program is in the lower level memory starting at 08048000 which is exactly what we have shown earlier is the segment which stores the program source listing. After the program listing the libc library is loaded into memory and then last of all the stack is loaded in the highest point in the memory.
We can look at the diagram below and see that it corresponds to the memory map given above for process 26709
No comments:
Post a Comment