Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6.  
  7.  
  8. int main(int argc, char *argv[]){
  9. int arg1Length = strlen(argv[1]);
  10.  
  11.  
  12. if((argc == 2) && (arg1Length<=20)){
  13. if(arg1Length == 1){
  14. return(*argv[1]);
  15. }
  16.  
  17.  
  18. int even = 0;
  19. if(arg1Length%2!=0){
  20. arg1Length--;
  21. even++;
  22. }
  23. int arraySubSize = arg1Length/2;
  24. char arr1[arraySubSize];
  25. char arr2[arraySubSize+even];
  26.  
  27. int stat1=0;
  28. int stat2=0;
  29.  
  30. char ret;
  31.  
  32. for(int i=0; i<arraySubSize; i++) arr1[i] = argv[1][i];
  33. arr1[arraySubSize] = '\0';
  34. for(int i=0; i<arraySubSize+even; i++) arr2[i] = argv[1][i+arraySubSize];
  35. arr2[arraySubSize+even] = '\0';
  36.  
  37.  
  38. pid_t pid = getpid();
  39.  
  40. pid_t child = fork();
  41. if(child==0){
  42. execlp("./zad8.out", "./zad8.out", arr1, NULL);
  43. }
  44. else if(child>0){
  45. child = fork();
  46. if(child==0){
  47. execlp("./zad8.out", "./zad8.out", arr2, NULL);
  48. }
  49. else if(child>0){
  50. wait(&stat1);
  51. ret = WEXITSTATUS(stat1);
  52. printf("%d, %d, %s, %c\n", pid, child, arr1, ret);
  53. }
  54. wait(&stat2);
  55. ret = WEXITSTATUS(stat2);
  56. printf("%d, %d, %s, %c\n", pid, child, arr2, ret);
  57. }
  58. return ret;
  59. }
  60. else{
  61. printf("%s", "Bledne argumenty\n");
  62. return 1;
  63. }
  64.  
  65.  
  66.  
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement