Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <sys/types.h>
  6. void main ()
  7. {
  8. while (1) {
  9. char buf[20];
  10. int returnStatus;
  11. printf("Podaj polecenie do wykonania [d,s,c,t]:\n");
  12. fgets(buf, 20, stdin);
  13. pid_t pid = fork();
  14. if (pid==0) {
  15. printf("Tu potomek pid=%d\n", getpid());
  16. switch(buf[0]){
  17. case 'd':
  18. execlp("date", "date", NULL);
  19. exit(0);
  20. break;
  21. case 's':
  22. execlp("sh","sh", NULL);
  23. exit(0);
  24. break;
  25. case 'c':
  26. execlp("xclock","xclock", NULL);
  27. exit(0);
  28. break;
  29. case 't':
  30. execlp("xterm","xterm", NULL);
  31. exit(0);
  32. break;
  33. default:
  34. printf("Nie istnieje takie polecenie\n");
  35. exit(0); /* obowiazkowe zakonczenie potomka */
  36. break;
  37. }
  38. }
  39. if (buf[0] == 'd' || buf[0] == 's'){
  40. wait(NULL);
  41. }
  42. if (buf[0] == 'c' || buf[0] == 't'){
  43. waitpid(pid, &returnStatus, WNOHANG);
  44. }
  45. printf("Tu rodzic po utworzeniu potomka.\n");
  46. } /* nieskonczona petla rodzica */
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement