Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8.  
  9. pid_t childPidL, childPidR;
  10.  
  11. int main(int argc, char* argv[]) {
  12. char *str = argv[1];
  13. char *pStr = "";
  14. if(argv[2] != NULL) asprintf(&pStr, "%s", argv[2]);
  15. int len = strlen(str);
  16. asprintf(&pStr, "%s %s", pStr, str);
  17. if (len > 1) {
  18. int halfLen = (len/2);
  19.  
  20. char *strLeft = (char*)malloc((halfLen+1) * sizeof(char));
  21. strncpy(strLeft, str, halfLen);
  22. childPidL = fork();
  23. if (!childPidL) {
  24. execl("./main", "main", strLeft, pStr, NULL);
  25. }
  26.  
  27. char *strRight = (char*)malloc((halfLen+1) * sizeof(char));
  28. strncpy(strRight, str+halfLen, halfLen);
  29. childPidR = fork();
  30. if (!childPidR) {
  31. execl("./main", "main", strRight, pStr, NULL);
  32. }
  33. wait(NULL);
  34. wait(NULL);
  35. free(strLeft);
  36. free(strRight);
  37. free(pStr);
  38. }
  39. printf("%s\n", pStr);
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement