Skip to content

Instantly share code, notes, and snippets.

@MikaelFangel
Last active December 26, 2022 23:09
Show Gist options
  • Save MikaelFangel/90740a430d692fb819d103bb9334636f to your computer and use it in GitHub Desktop.
Save MikaelFangel/90740a430d692fb819d103bb9334636f to your computer and use it in GitHub Desktop.
Unix fork-exec technique
#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