Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<unistd.h>
- #include<sys/wait.h>
- void split_word(char *string, char **word)
- {
- int i;
- word[0] = strtok(string, " \n");
- for(i=0; word[i]; i++)
- {
- word[i+1] = strtok(NULL, " \n");
- }
- }
- int main()
- {
- char cmd[80];
- char *args[20];
- while (1)
- {
- // cetak prompt "$ "
- printf("$ ");
- // baca string perintah
- gets(cmd);
- // jika perintah == "exit", keluar
- if(strcmp(cmd,"exit") == 0 ) return 0;
- // potong string perintah per argumen
- split_word(cmd, args);
- // buat proses child untuk:
- pid_t pid;
- pid = fork();
- wait(NULL);
- if(pid == 0)
- {
- int status;
- // exec string perintah
- status = execvp(args[0], args);
- // cetak pesan error jika exec gagal
- if(status == -1)
- {
- puts("Error");
- // keluar dengan kode status -1
- kill(pid, SIGQUIT);
- }
- }
- // sinkronisasi proses child
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement