What you want is the pid
of the process that is starting this program.
The signature of fork
function is the following:
#include <unistd.h>
pid_t fork(void);
and it returns:
0
in the child
the pid of the child
in the parent
-1
if an error ocurred
If you want to get the pid
of the new process created (the child), you must check if the returned value is greather than 0
.
In your example:
pid_t pid = fork()
if (pid == 0) {
struct rlimit limits;
limits.rlim_cur = 10000000; // set data segment limit to 10MB
limits.rlim_max = 10000000; // make sure the child can't increase it again
setrlimit(RLIMIT_DATA, &limits);
execv(...);
}
else if (pid > 0) {
/* That's the value of the pid you are looking for */
}
This can be confusing, but the thing is that when fork()
is executed, it creates a child process, so the program kind of splits in two. That's why you must check for the pid
value and do what you want depending of if you are in the child or in the parent.