Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问hustoj支持交互题吗?如果支持的话应该如何设置?(样例如图) #928

Open
JasoxHsueh opened this issue Mar 31, 2023 · 4 comments

Comments

@JasoxHsueh
Copy link

WechatIMG38

@zhblue
Copy link
Owner

zhblue commented Apr 1, 2023

不支持,你可以自己写个spj来跟目标程序交互试试

@JasoxHsueh
Copy link
Author

好吧,我们尝试一下,多谢

@zhblue
Copy link
Owner

zhblue commented Apr 21, 2023

写了个参考给你

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int test(){
                int c=0;
                printf("1 2\n");
                fflush(stdout);
                scanf("%d",&c);
                if(c==3) {
                        return 100;
                }
                if(c==917)
                        return 50;
                return 0;
}
int main(){
        int mark=0;
        const char * spj="./spj";
        const char * main="./Main";
        int pipe_main2spj[2];
        int pipe_spj2main[2];
        pid_t pid_spj,pid_main;
        int type=0;
        if(pipe2(pipe_main2spj,0) == -1){
                perror("pipe a init fail \n");
                exit(EXIT_FAILURE);
        }
        if(pipe2(pipe_spj2main,0) == -1){
                perror("pipe b init fail \n");
                exit(EXIT_FAILURE);
        }

        pid_main=fork();
        if(pid_main == 0){ // target process
                printf("i'm main\n");
                close(pipe_main2spj[0]);
                close(pipe_spj2main[1]);
                dup2(pipe_main2spj[1],1);
                dup2(pipe_spj2main[0],0);
                execl(main,main,NULL);
                perror("bye from main\n");
                exit(0);
        }else{ // parent
                printf("i'm spj\n");
                close(pipe_main2spj[1]);  // pipe a main -> spj ,close the outpu                                         t of pipe
                close(pipe_spj2main[0]);  // pipe b spj -> main .close the input                                          of pipe
                dup2(pipe_main2spj[0],0);
                dup2(pipe_spj2main[1],1);

                mark=test();
        }
        //waitpid(-1,0,0);
        waitpid(-1,0,0);
        return 100-mark;
}

@JasoxHsueh
Copy link
Author

收到,我们参考一下,respect🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants