Created
November 6, 2018 16:56
-
-
Save lixin9311/368a7880377bd1a27d5b716862579afe to your computer and use it in GitHub Desktop.
fork and get VMA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#include "stdlib.h" | |
#include "sys/types.h" | |
#include "sys/wait.h" | |
#include "unistd.h" | |
int runcmd(char *cmd) { | |
FILE *fp; | |
char buf[1035]; | |
fp = popen(cmd, "r"); | |
if (fp == NULL) { | |
printf("Failed to run command\n"); | |
exit(1); | |
} | |
while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { | |
printf("%s", buf); | |
} | |
pclose(fp); | |
return 0; | |
} | |
int main(void) { | |
pid_t pid = -42; | |
int wstatus = -42; | |
int ret = -1; | |
char cmd[1024]; | |
pid = fork(); | |
switch (pid) { | |
case -1: | |
perror("fork"); | |
return EXIT_FAILURE; | |
case 0: | |
sleep(1); | |
printf("[child]: my id [%d]\n", getpid()); | |
exit(0); | |
default: | |
printf("[father]: my id [%d]\n", getpid()); | |
printf("[father]: my child id [%d]\n", pid); | |
sprintf(cmd, "cat /proc/%d/maps", pid); | |
runcmd(cmd); | |
break; | |
} | |
ret = waitpid(pid, &wstatus, 0); | |
if (ret == -1) { | |
perror("waitpid"); | |
return EXIT_FAILURE; | |
} | |
printf("Child exit status: %d\n", WEXITSTATUS(wstatus)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment