1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int fstrlen (const char *p) { pid_t child = vfork(); switch (child) { case -1: perror("fork() failed"); exit(EXIT_FAILURE); case 0: exit(*p ? 1 + fstrlen(p + 1) : 0); default: { int status; waitpid(child, &status, 0); return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; } } } int main () { printf("%d\n", fstrlen("abcde")); return 0; } |