Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. 267 //Error in Fork
  2. 268 case -1: { perror("Hull Breach!\n"); exit(1); break; }
  3. 269
  4. 270 //Child Process
  5. 271 case 0:
  6. 272 {
  7. 273 if (secondArray[0] != NULL)
  8. 274 {
  9. 275 if(strcmp(secondArray[0], ">") == 0)
  10. 276 {
  11. 277 int fileDesc = open(secondArray[1], O_WRONLY | O_APPEND | O_CREAT, 0644);
  12. 278 if (fileDesc == -1)
  13. 279 {
  14. 280 perror(NULL);
  15. 281 exit(1);
  16. 282 }
  17. 283 dup2(fileDesc, 1);
  18. 284 }
  19. 285 if(strcmp(secondArray[0], "<") == 0)
  20. 286 {
  21. 287 int fileDesc2 = open(secondArray[1], O_RDONLY);
  22. 288 if (fileDesc2 == -1)
  23. 289 {
  24. 290 perror(NULL);
  25. 291 exit(1);
  26. 292 }
  27. 293 dup2(fileDesc2, 0);
  28. 294
  29. 295 }
  30. 296 if (strcmp(secondArray[secondCounter - 2], "&") == 0)
  31. 297 {
  32. 298 printf("This is a background process\n");
  33. 299 background = 1;
  34. 300 }
  35. 301 }
  36. 302 execvp(userInputArray[0], userInputArray);
  37. 303 perror("CHILD: exec failure!\n");
  38. 304 exit(2);
  39. 305 break;
  40. 306 }
  41. 307 //Parent Process
  42. 308 default:
  43. 309 {
  44. 310 //If process is run in the foreground, no reason to add the the array of processes
  45. 311 if(background == 1)
  46. 312 {
  47. 313 PIDArray[counterX] = spawnPid;
  48. 314 pid_t myPID = waitpid(spawnPid, &childExitStatus, WNOHANG);
  49. 315 }
  50. 316 fflush(stdout);
  51. 317 pid_t actualPid = waitpid(spawnPid, &childExitStatus, 0);
  52. 318 fflush(stdout);
  53. 319 break;
  54. 320 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement