Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <string.h>
- pid_t childPidL, childPidR;
- int main(int argc, char* argv[]) {
- char *str = argv[1];
- char *pStr = "";
- if(argv[2] != NULL) asprintf(&pStr, "%s", argv[2]);
- int len = strlen(str);
- asprintf(&pStr, "%s %s", pStr, str);
- if (len > 1) {
- int halfLen = (len/2);
- char *strLeft = (char*)malloc((halfLen+1) * sizeof(char));
- strncpy(strLeft, str, halfLen);
- childPidL = fork();
- if (!childPidL) {
- execl("./main", "main", strLeft, pStr, NULL);
- }
- char *strRight = (char*)malloc((halfLen+1) * sizeof(char));
- strncpy(strRight, str+halfLen, halfLen);
- childPidR = fork();
- if (!childPidR) {
- execl("./main", "main", strRight, pStr, NULL);
- }
- wait(NULL);
- wait(NULL);
- free(strLeft);
- free(strRight);
- free(pStr);
- }
- printf("%s\n", pStr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement