Advertisement
Guest User

Zółwie są spoko

a guest
Dec 11th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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. #include <sys/wait.h>
  7. #include <sys/types.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. int status;
  12. if(argc!=2)
  13. {
  14. fprintf(stderr,"Error1\n");
  15. exit(1);
  16. }
  17. int length=strlen(argv[1]);
  18. if(length>20)
  19. {
  20. fprintf(stderr,"Error2\n");
  21. exit(1);
  22. }
  23. if (length%2==0)
  24. {
  25. char *tab1 [length/2];
  26. char *tab2 [length/2];
  27. //printf("Jestem tu 1\n");
  28. strncpy(tab1,argv[1],length/2);
  29. strncpy(tab2,argv[1]+length/2,length/2);
  30. //printf("%s\n",tab1);
  31. //printf("%s\n",tab2);
  32.  
  33. //Pierwszy potomek
  34. pid_t cpid = fork();
  35. if (cpid>0)
  36. {
  37. //Drugi potomek
  38. pid_t ccpid = fork();
  39. if (ccpid>0)
  40. {
  41. waitpid(cpid,&status,0);
  42. int exitstatus1=WEXITSTATUS(status);
  43. waitpid(ccpid,&status,0);
  44. int exitstatus2=WEXITSTATUS(status);
  45. int pid = getpid();
  46. if (cpid>0)
  47. printf("%d\t%d\t%s\t%c\n",pid,cpid,tab1,exitstatus1);
  48. if (ccpid>0)
  49. printf("%d\t%d\t%s\t%c\n",pid,ccpid,tab2,exitstatus2);
  50. if (exitstatus1>exitstatus2)
  51. return exitstatus1;
  52. if (exitstatus1<exitstatus2)
  53. return exitstatus2;
  54. }
  55. else
  56. {
  57. int exec2 = execlp(argv[0],argv[0],tab2,NULL);
  58. printf("exec2=%d\n",exec2);
  59. }
  60. }
  61. else
  62. {
  63. int exec1 = execlp(argv[0],argv[0],tab1,NULL);
  64. printf("exec1=%d\n",exec1);
  65. }
  66.  
  67. }
  68. if (length%2==1)
  69. {
  70. if (length==1)
  71. {
  72. return *argv[1];
  73. }
  74. char tab1[length/2];
  75. char tab2[length/2+1];
  76. //printf("Jestem tu 2\n");
  77. strncpy(tab1,argv[1],length/2);
  78. strncpy(tab2,argv[1]+length/2,length/2+1);
  79. //printf("%s\n",tab1);
  80. //printf("%s\n",tab2);
  81.  
  82. //Pierwszy potomek
  83. pid_t cpid = fork();
  84. if (cpid>0)
  85. {
  86. //Drugi potomek
  87. pid_t ccpid = fork();
  88. if (ccpid>0)
  89. {
  90. waitpid(cpid,&status,0);
  91. int exitstatus1=WEXITSTATUS(status);
  92. waitpid(ccpid,&status,0);
  93. int exitstatus2=WEXITSTATUS(status);
  94. int pid = getpid();
  95. if (cpid>0)
  96. printf("%d\t%d\t%s\t%c\n",pid,cpid,tab1,exitstatus1);
  97. if (ccpid>0)
  98. printf("%d\t%d\t%s\t%c\n",pid,ccpid,tab2,exitstatus2);
  99. if (exitstatus1>exitstatus2)
  100. return exitstatus1;
  101. if (exitstatus1<exitstatus2)
  102. return exitstatus2;
  103. }
  104. else
  105. {
  106. int exec2 = execlp(argv[0],argv[0],tab2,NULL);
  107. printf("exec2=%d\n",exec2);
  108. }
  109. }
  110. else
  111. {
  112. int exec1 = execlp(argv[0],argv[0],tab1,NULL);
  113. printf("exec1=%d\n",exec1);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement