Created
November 7, 2019 16:36
-
-
Save surinoel/e72e1544fa009e7413b28db8bc48f559 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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