Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <ctype.h>
- int MyExit(int ret)
- {
- printf("\n");
- return ret;
- }
- int main(int argc, char** argv)
- {
- if(argc != 2)
- {
- printf("Za dużo/mało argumentów (%d)", argc - 1);
- return MyExit(1);
- }
- char* arg = argv[1];
- int length = strlen(arg);
- if(length > 20)
- {
- printf("Za dużo znaków (%d)", length);
- return MyExit(1);
- }
- if (length == 1)
- {
- //printf("%c", arg[0]);
- return arg[0];
- }
- //printf("%s \n", arg);
- char* firstHalf = (char*)malloc(sizeof(char) * (length/2 + 1));
- char* secondHalf = (char*)malloc(sizeof(char) * (length - length/2 + 1));
- int i;
- memcpy(firstHalf, arg, length/2);
- memcpy(secondHalf, (arg + length/2), length - length/2);
- firstHalf[length/2] = '\0';
- secondHalf[length - length/2] = '\0';
- //printf("%s \n", firstHalf);
- //printf("%s \n", secondHalf);
- char** childArgs1 = malloc(sizeof(char*)*3);
- char** childArgs2 = malloc(sizeof(char*)*3);
- int wait1, wait2, ret1, ret2;
- pid_t pid_t1, pid_t2;
- pid_t1 = fork();
- if (pid_t1 == 0)
- {
- childArgs1[0] = "a.out";
- childArgs1[1] = firstHalf;
- childArgs1[2] = NULL;
- execv("a.out", childArgs1);
- }
- pid_t2 = fork();
- if(pid_t2 == 0)
- {
- childArgs2[0] = "a.out";
- childArgs2[1] = secondHalf;
- childArgs2[2] = NULL;
- execv("a.out", childArgs2);
- }
- pid_t waitresult1 = waitpid(pid_t1, &wait1, 0);
- ret1 = WEXITSTATUS(wait1);
- printf("%d %d %s %d \n", getpid(), waitresult1, firstHalf, ret1);
- pid_t waitresult2 = waitpid(pid_t2, &wait2, 0);
- ret2 = WEXITSTATUS(wait2);
- printf("%d %d %s %d \n", getpid(), waitresult2, secondHalf, ret2);
- free(firstHalf);
- free(secondHalf);
- free(childArgs1);
- free(childArgs2);
- if(ret1 > ret2)
- return ret1;
- else
- return ret2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement