Question

I read cat /proc/[pid]/maps gives the informaion about its address space. So I want to write a program which will print its own address space.

My program is this;

  pid_t pid;
  int fd;
  char *buf;

  pid = getpid();

  fd = open("/proc/????/maps", O_RDONLY);

I'm getting the PID, but it won't help me to open the file. How to convert the pid from pid_t to string and add it to open call?

Or is there any other way to open the file?

Was it helpful?

Solution

All modern procfs systems implement "/proc/self/" for the running process. Just

fd = open("/proc/self/maps", O_RDONLY);

If you still wish to create the path string yourself then you have to use sprintf

char filename[128];
sprintf(filename, "/proc/%d/maps", getpid());
fd = open(filename, O_RDONLY);

OTHER TIPS

If you just want to print mapping information for review, one simple method:

you can use system library call to execute cat /proc/[pid]/maps

A simple C code is as follows:

char command[256];
sprintf(command, "cat /proc/%d/maps", getpid());
system(command);

You can refer to one example in my Gist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top