I have the following code:
int main(int argc, char **argv) { char *program; char stringa[1000] = ""; int num = 123; char snum[10]; if (argc != 2) { printf("Usage: mon fileName\n where fileName is an executable file.\n"); exit(-1); } else { program = argv[1]; sprintf(stringa, "./%s", program); pid_t pid = fork(); if (pid < 0 ) { perror("fork failed."); exit(1); } else if (pid == 0) { char* args[] = {stringa, NULL}; execv(args[0], args); } else { char procmon_str[] = "./procmon"; num = pid; sprintf(snum, "%d",num); pid_t pid2 = fork(); if (pid2 == 0) { char* args2[] = {procmon_str, snum, NULL}; execv(args2[0], args2); } else { printf("PID of child is %s", snum); int parent_pid = getpid(); printf("PID of parent is %d", parent_pid);} }} wait(NULL); return 0; }
The name of this program is myProgram . The argument I provide in the shell is:
./myProgram calc
calc is another program that I want to run using myProgram . myProgram then performs calc , takes its PID and passes that PID to another program called procmon , which does something with it; so i need to deploy twice. However, when I run the code above, I get:
procmon: cannot open /proc/6225/stat, the monitored process is not running anymore .
How can i fix this?
What does calc do? It goes into the for loop, increments the int variable and goes into sleep mode for 3 seconds and repeats this 10 times. Therefore, it should work for about 30 seconds.
What does procmon do? procmon simply takes the process PID as an argument and displays the corresponding file /proc/PID/stat . It works great when you run it yourself.
c linux
gambit20088
source share