Advertisement
Guest User

zaras sie zdenerwuje

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     int status;
  10.     if(argc!=2)
  11.     {
  12.         fprintf(stderr,"Error1\n");
  13.         exit(1);
  14.     }
  15.     int length=strlen(argv[1]);
  16.     if(length>20)
  17.     {
  18.         fprintf(stderr,"Error2\n");
  19.         exit(1);
  20.     }
  21.     if (length%2==0)
  22.     {
  23.         char *tab1 [length/2];
  24.         char *tab2 [length/2];
  25.         printf("Jestem tu 1\n");
  26.         strncpy(tab1,argv[1],length/2);
  27.         strncpy(tab2,argv[1]+length/2,length/2);
  28.         //printf("%s\n",tab1);
  29.         //printf("%s\n",tab2);
  30.  
  31.         //Pierwszy potomek
  32.         pid_t cpid = fork();
  33.         if (cpid>0)
  34.         {
  35.             //Drugi potomek
  36.             pid_t ccpid = fork();
  37.             if (ccpid>0)
  38.             {
  39.                 waitpid(cpid,&status,0);
  40.                 waitpid(ccpid,&status,0);
  41.                 printf("%d\t%d\t%s\n",getpid(),cpid,tab1);
  42.                 printf("%d\t%d\t%s\n",getpid(),ccpid,tab2);
  43.             }
  44.             else
  45.             {
  46.                 execlp(argv[0],argv[0],tab2);
  47.             }
  48.         }
  49.         else
  50.         {
  51.             execlp(argv[0],argv[0],tab1);
  52.         }
  53.  
  54.     }
  55.     if (length%2==1)
  56.     {
  57.         char tab1[length/2];
  58.         char tab2[length/2+1];
  59.         printf("Jestem tu 2\n");
  60.         strncpy(tab1,argv[1],length/2);
  61.         strncpy(tab2,argv[1]+length/2,length/2+1);
  62.         //printf("%s\n",tab1);
  63.         //printf("%s\n",tab2);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement