Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- int main(int argc, char *argv[])
- {
- int status;
- if(argc!=2)
- {
- fprintf(stderr,"Error1\n");
- exit(1);
- }
- int length=strlen(argv[1]);
- if(length>20)
- {
- fprintf(stderr,"Error2\n");
- exit(1);
- }
- if (length%2==0)
- {
- char tab1 [length/2];
- char tab2 [length/2];
- //printf("Jestem tu 1\n");
- strncpy(tab1,argv[1],length/2);
- strncpy(tab2,argv[1]+length/2,length/2);
- //printf("%s\n",tab1);
- //printf("%s\n",tab2);
- //Pierwszy potomek
- pid_t cpid = fork();
- if (cpid>0)
- {
- //Drugi potomek
- pid_t ccpid = fork();
- if (ccpid>0)
- {
- waitpid(cpid,&status,0);
- int exitstatus1=WEXITSTATUS(status);
- waitpid(ccpid,&status,0);
- int exitstatus2=WEXITSTATUS(status);
- int pid = getpid();
- if (cpid>0)
- printf("%d\t%d\t%s\t%c\n",pid,cpid,tab1,exitstatus1);
- if (ccpid>0)
- printf("%d\t%d\t%s\t%c\n",pid,ccpid,tab2,exitstatus2);
- if (exitstatus1>exitstatus2)
- return exitstatus1;
- if (exitstatus1<exitstatus2)
- return exitstatus2;
- }
- else
- {
- int exec2 = execlp(argv[0],argv[0],tab2,NULL);
- printf("exec2=%d\n",exec2);
- }
- }
- else
- {
- int exec1 = execlp(argv[0],argv[0],tab1,NULL);
- printf("exec1=%d\n",exec1);
- }
- }
- if (length%2==1)
- {
- if (length==1)
- {
- return *argv[1];
- }
- char tab1[length/2];
- char tab2[length/2+1];
- //printf("Jestem tu 2\n");
- strncpy(tab1,argv[1],length/2);
- strncpy(tab2,argv[1]+length/2,length/2+1);
- //printf("%s\n",tab1);
- //printf("%s\n",tab2);
- //Pierwszy potomek
- pid_t cpid = fork();
- if (cpid>0)
- {
- //Drugi potomek
- pid_t ccpid = fork();
- if (ccpid>0)
- {
- waitpid(cpid,&status,0);
- int exitstatus1=WEXITSTATUS(status);
- waitpid(ccpid,&status,0);
- int exitstatus2=WEXITSTATUS(status);
- int pid = getpid();
- if (cpid>0)
- printf("%d\t%d\t%s\t%c\n",pid,cpid,tab1,exitstatus1);
- if (ccpid>0)
- printf("%d\t%d\t%s\t%c\n",pid,ccpid,tab2,exitstatus2);
- if (exitstatus1>exitstatus2)
- return exitstatus1;
- if (exitstatus1<exitstatus2)
- return exitstatus2;
- }
- else
- {
- int exec2 = execlp(argv[0],argv[0],tab2,NULL);
- printf("exec2=%d\n",exec2);
- }
- }
- else
- {
- int exec1 = execlp(argv[0],argv[0],tab1,NULL);
- printf("exec1=%d\n",exec1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement