Last active
December 26, 2022 23:09
-
-
Save MikaelFangel/90740a430d692fb819d103bb9334636f to your computer and use it in GitHub Desktop.
Unix fork-exec technique
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 <stdlib.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
void fork_exec(void) | |
{ | |
switch (fork()) | |
{ | |
case -1: // Error | |
perror("fork"); | |
exit(EXIT_FAILURE); | |
case 0: // Child | |
char *argv[] = {"ls", "-ls", NULL}; | |
execvp("ls", argv); | |
perror("exec"); | |
exit(EXIT_FAILURE); | |
default: // Parent | |
waitpid(-1, NULL, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment