Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #define clearBuffer() while(getchar() != '\n');
  5.  
  6. int main()
  7. {
  8. pid_t pid;
  9. char choice;
  10. do
  11. {
  12. printf("Select Menu\n");
  13. printf("a. ls\nb. ps\nc. who\n0. exit\nEnter >> ");
  14. choice = getchar();
  15. clearBuffer();
  16. printf("You select : %c\n", choice);
  17. pid = fork();
  18.  
  19. if(pid == 0)
  20. {
  21. switch(choice)
  22. {
  23. case 'a': execlp("/bin/ls", "ls", "-al", NULL); break;
  24. case 'b': execlp("/bin/ps", "ps", NULL); break;
  25. case 'c': execlp("/usr/bin/who", "who", NULL); break;
  26. case '0': break;
  27. }
  28. exit(0);
  29. }
  30. if(pid < 0)
  31. {
  32. fprintf(stderr, "Fork Failed");
  33. exit(EXIT_FAILURE);
  34. }
  35. printf("exec() complete\n");
  36.  
  37. } while(choice != '0');
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement