Guest User

Untitled

a guest
Dec 8th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. // SO IS1 213A LAB02
  2. // Krzysztof Osman
  3.  
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.  
  11. if(argv[2]!=NULL || argv[1]==NULL)
  12.     {
  13.     return 1;
  14.     }
  15. if(strlen(argv[1])>20)
  16.     {
  17.     printf("Za dlugi lancuch");
  18.     return 1;
  19.     }
  20.  
  21. if(strlen(argv[1])==1)
  22.     {
  23.     return argv[1][0];
  24.     }
  25.  
  26. char *wyraz=argv[1];
  27. char nazwa1[11];
  28. char nazwa2[11];
  29. int x=0;
  30. int g=0;
  31.  
  32. if(strlen(argv[1])%2==0)
  33. {
  34.     while(x<strlen(argv[1])/2)
  35.     {
  36.     nazwa1[x]=wyraz[x];
  37.     x++;
  38.     }
  39.     while(x<strlen(argv[1])+1)
  40.     {
  41.     nazwa2[g]=wyraz[x];
  42.    
  43.     x++;
  44.     g++;
  45.     }
  46. nazwa1[x/2]='\0';
  47. nazwa2[x]='\0';
  48.  
  49. }
  50. else
  51. {
  52.     while(x<(strlen(argv[1])/2))
  53.         {
  54.         nazwa1[x]=wyraz[x];
  55.         //printf("Nazwa1c: %c\n",nazwa1[x]);
  56.         x++;
  57.         }
  58.     while(x<strlen(argv[1]))
  59.         {
  60.         nazwa2[g]=wyraz[x];
  61.         //printf("Nazwa2c: %c\n",nazwa2[g]);
  62.         x++;
  63.         g++;
  64.         }
  65.  
  66. nazwa1[x/2]='\0';
  67. nazwa2[x]='\0';
  68.  
  69. }
  70.  
  71. //printf("Nazwa1: %s\n",nazwa1);
  72. //printf("Nazwa2: %s\n",nazwa2);
  73. //printf("Pelna: %s\n",wyraz);
  74.  
  75. STARTUPINFO si;
  76. PROCESS_INFORMATION pi[2];
  77.  
  78.  
  79. memset(&si, 0, sizeof(si));
  80. memset(&pi, 0, sizeof(pi));
  81. si.cb = sizeof(si);
  82.  
  83. char *argument[2];
  84. int doreturn[2];
  85.  
  86. argument[0]=nazwa1;
  87. argument[1]=nazwa2;
  88.  
  89. //printf("argument[0]: %s\n",argument[0]);
  90. //printf("argument[1]: %s\n",argument[1]);
  91. for(int i = 0; i < 2; i++)
  92. {
  93.     sprintf(argv[1], "a.exe %s ", argument[i]);
  94.         if(CreateProcessA(NULL,argv[1],NULL,NULL,0,0,NULL,NULL,&si,pi+i) == 0)
  95.         {
  96.             printf( "CreateProcess #%d failed (%d).\n", i+1, GetLastError() );
  97.             return 2;
  98.         }
  99. printf("Process #%d created: handle=%p, identifier=%d\n", i+1, pi[i].hProcess, pi[i].dwProcessId);
  100.  
  101.  
  102. }
  103.  
  104.  
  105. HANDLE children[2] = {pi[0].hProcess,pi[1].hProcess};
  106. WaitForMultipleObjects(2, children, 1, INFINITE);
  107. for(int j = 1; j >= 0; j--)
  108. {
  109.     GetExitCodeProcess(children[j],&doreturn[j]);
  110.     CloseHandle( pi[j].hProcess );
  111.     CloseHandle( pi[j].hThread );
  112.     printf("Process #%d terminated.\n", j+1);
  113.    
  114. }
  115.  
  116. if(doreturn[0]>doreturn[1])
  117. {
  118.     return doreturn[0];
  119. }
  120. else
  121. {
  122.     return doreturn[1];
  123. }
  124.  
  125.  
  126.  
  127. /*
  128. int wartosci_pid[2];
  129. int wartosci[2];
  130.  
  131. int pid1=fork();
  132.  
  133.  
  134. if(pid1==0)
  135. {
  136.     execlp("./a.out","./a.out",nazwa1,NULL);
  137.     fprintf(stderr,"fork fail %s\n",strerror(errno));
  138.     return 2;
  139. }
  140. else
  141. {
  142.     wartosci_pid[0]=pid1;
  143.     int status, child = waitpid(wartosci_pid[0],&status,0);
  144.     int a = getpid();
  145.     wartosci[0]=WEXITSTATUS(status);
  146.     if(child!=-1)
  147.     {
  148.     printf("%d %d  %s %c\n",a, child, nazwa1, wartosci[0] );
  149.     }
  150.        
  151.         int pid2=fork();
  152.         if(pid2==0)
  153.         {
  154.         execlp("./a.out","./a.out",nazwa2,NULL);
  155.         fprintf(stderr,"fork fail %s\n",strerror(errno));
  156.         return 3;
  157.         }
  158.         else
  159.         {
  160.             wartosci_pid[1]=pid2;
  161.             int status, child = waitpid(wartosci_pid[1],&status,0);
  162.             int a = getpid();
  163.             wartosci[1]=WEXITSTATUS(status);
  164.             if(child!=-1)
  165.             {
  166.             printf("%d %d  %s %c\n",a, child, nazwa2, wartosci[1] );
  167.             }
  168.         }
  169.    
  170. }
  171.  
  172.  
  173. */
  174.  
  175.  
  176.  
  177.  
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment