Created
August 7, 2017 14:33
-
-
Save hamadu/a5450aa838d7cf042f45252d050f3a63 to your computer and use it in GitHub Desktop.
fork_exec
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 <unistd.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
void print_error_and_exit() { | |
printf("error(%d): %s\n", errno, strerror(errno)); | |
exit(1); | |
} | |
int main(int argc, char* argv[]) { | |
pid_t result = fork(); | |
if (result == -1) { | |
print_error_and_exit(); | |
} | |
if (result == 0) { | |
execl("/bin/ls", ".", (char*)NULL); | |
} else { | |
sleep(3); | |
printf("hi. this is parent process\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment