Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/wait.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h> // open()
  5. #include <signal.h> // signal()
  6. #include <fcntl.h> // open()
  7. #include <stdio.h> // printf(), ...
  8. #include <time.h> // time(), ...
  9. #include <string.h>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <unistd.h> // strtok()
  13. #define MAXLINE 100
  14. #define MOD "exit with CTR C"
  15.  
  16. time_t start;
  17.  
  18. int read_command(char *command,char *parameters[]) { // prompt for user input and read a command line
  19. char dir[FILENAME_MAX];
  20. getcwd(dir,FILENAME_MAX); //get current dir and safe into dir
  21. fprintf(stdout, "%s >",dir); //Print out current dir
  22. int noParam=0;
  23. char linearray[MAXLINE];
  24. fgets(linearray, MAXLINE, stdin);
  25.  
  26. char* pointer;
  27.  
  28. pointer = strtok(linearray," \n"); //Delimit at blank and \n (Enter)
  29. while(pointer != NULL){
  30. if(noParam == 0)
  31. strcpy(command,pointer);
  32.  
  33. parameters[noParam] = pointer;
  34. noParam++;
  35. pointer = strtok(NULL," \n");
  36. }
  37.  
  38. parameters[noParam] = NULL;
  39. return(noParam);
  40. } // read_command
  41.  
  42. void childhandler(int) {
  43. pid_t pid;
  44. int status;
  45.  
  46. //waitpid(-1, &status, WNOHANG) checks if any zombie children exists
  47. //If yes, it will be reaped and return either 0 if another unterminated child exists
  48. //Or -1 when no more are left
  49.  
  50. /* Aus Dokumentation pubs.opengroup.org:
  51. *WNOHANG
  52. *The waitpid() function shall not suspend execution of the calling thread
  53. *if status is not immediately available for one of the child processes specified by pid.
  54. */
  55. while((pid = waitpid(-1, &status, WNOHANG)) > 0);
  56.  
  57. }
  58.  
  59. void exithandler(int){
  60. time_t end = time(NULL); //nehme Zeit, wenn STRG+C gedrückt wird
  61. int seconds = difftime(end,start); //Differenz zwischen Start und Ende
  62. int minutes = seconds / 60;
  63. int hours = minutes / 60;
  64. seconds = seconds % 60;
  65. minutes = minutes % 60;
  66. fprintf(stdout,"\nTerminated\nTime elapsed:%dh %dm %ds\n",hours,minutes,seconds);
  67. exit(0);
  68. }
  69.  
  70.  
  71. int main(int argc, char *argv[]) {
  72. start = time(NULL); //Zeit bei Start des Programms
  73.  
  74. signal(SIGINT, exithandler);
  75.  
  76.  
  77. int childPid;
  78. int status;
  79. char command[MAXLINE];
  80. char *parameters[MAXLINE];
  81. int noParams;
  82.  
  83. while (1) {
  84. noParams = read_command(command, parameters); // read user input
  85. if(strncmp(command,"cd",2)==0){
  86. chdir(parameters[1]);
  87. continue;
  88. }
  89.  
  90. if(strcmp(parameters[noParams-1],"&") == 0) {
  91. parameters[noParams-1] = NULL;
  92. noParams--;
  93. //SIGCHLD is the Signal when a Childprocess terminates
  94. signal(SIGCHLD,childhandler);
  95.  
  96. if (noParams == 0) {
  97. fprintf(stderr, "no command ?!\n");
  98. exit(1);
  99. }
  100. if ((childPid = fork()) == -1) { // create process
  101. fprintf(stderr, "can't fork!\n");
  102. exit(2);
  103. } else if (childPid == 0) { // child process
  104. execvp(command, parameters); // executes command
  105. exit(3);
  106. }
  107. else { // father
  108. printf("[%d]\n",childPid);
  109. }
  110.  
  111. } else {
  112.  
  113. if (noParams == 0) {
  114. fprintf(stderr, "no command ?!\n");
  115. exit(1);
  116. }
  117. if ((childPid = fork()) == -1) { // create process
  118. fprintf(stderr, "can't fork!\n");
  119. exit(2);
  120. } else if (childPid == 0) { // child process
  121. execvp(command, parameters); // executes command
  122. exit(3);
  123. }
  124. else { // father
  125. waitpid(childPid, &status, WUNTRACED | WCONTINUED);
  126. }
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement