Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char *argv[]) {
  9.     int status, i, arg_length, half_arg_length;
  10.     arg_length=strlen(argv[1]);
  11.     char *str = malloc(sizeof(char) * (arg_length));
  12.     char *half = malloc(sizeof(char) * (half_arg_length));
  13.     char *half2 = malloc(sizeof(char) * (half_arg_length));
  14.     printf("%d ", getpid());
  15.         printf("%s \n", argv[1]);
  16.     if(strlen(argv[1])==1) {
  17.             _Exit(0);
  18.     }
  19.     else {
  20.             strcpy(str, argv[1]);
  21.             half_arg_length=arg_length/2;
  22.             for(i = 0; i < half_arg_length; i++) {
  23.                 half[i]=str[i];
  24.                 half2[i]=str[i + half_arg_length];
  25.             }
  26.         if(fork()==0) {
  27.             wait(&status);
  28.             printf("%d ", getpid());
  29.             execl("./1", "1", half, NULL);
  30.         }
  31.         else
  32.             wait(&status);
  33.         if(fork()==0) {
  34.             wait(&status);
  35.             printf("%d ", getpid());
  36.             execl("./1", "1", half2, NULL);
  37.         }
  38.         else
  39.             wait(&status);
  40.     }
  41.     free(half);
  42.     free(str);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement