Skip to content

Instantly share code, notes, and snippets.

@hamadu
Created August 7, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamadu/a5450aa838d7cf042f45252d050f3a63 to your computer and use it in GitHub Desktop.
Save hamadu/a5450aa838d7cf042f45252d050f3a63 to your computer and use it in GitHub Desktop.
fork_exec
#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