Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created November 7, 2019 16:36
Show Gist options
  • Save surinoel/e72e1544fa009e7413b28db8bc48f559 to your computer and use it in GitHub Desktop.
Save surinoel/e72e1544fa009e7413b28db8bc48f559 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
char buf[128];
pid_t pid;
while(1) {
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf) - 1, stdin);
buf[strlen(buf) - 1] = '\0';
if(!strncmp(buf, "exit", strlen(buf))) {
return -1;
}
pid = fork();
if(pid < 0) {
perror("fork error\n");
return -1;
}
else if(pid == 0) {
execlp(buf, buf, NULL);
exit(0);
}
else {
wait(NULL);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment