问题描述
我正在尝试使用 gcc 在运行时编译汇编代码。我从标准输入(汇编代码)获取输入并将结果以二进制格式输出到标准输出。 由于某种原因,我的程序卡住了(我认为是因为它在等待来自 stdin 的输入时阻塞了)。
代码如下:
char* assemble(char* assembly_code){
char *binaryPath = "/usr/bin/gcc";
char *binary = "gcc";
char *arg1 = "-x";
char *arg2 = "assembler";
char *arg3 = "-";
char *arg4 = "-o";
char *arg5 = "/dev/stdout";
char *arg6 = "-nostdlib";
char *arg7 = "-Wl,--oformat=binary";
char *arg8 = "-m64";
char data[1024];
memset(data,1024);
pid_t pid = 0;
int pipe_fd_1[2];
int pipe_fd_2[2];
int status;
pipe(pipe_fd_1);
pipe(pipe_fd_2);
pid = fork();
if (pid == 0){
dup2(pipe_fd_1[0],fileno(stdin));
dup2(pipe_fd_2[1],fileno(stdout));
close(pipe_fd_1[1]);
close(pipe_fd_2[0]);
execl(binaryPath,binary,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,(char*) NULL);
close(pipe_fd_1[0]);
close(pipe_fd_2[1]);
exit(1);
}
close(pipe_fd_1[0]);
close(pipe_fd_2[1]);
write(pipe_fd_1[1],assembly_code,strlen(assembly_code));
read(pipe_fd_2[0],(void*)data,1024);
close(pipe_fd_1[1]);
close(pipe_fd_2[0]);
printf("Received answer: %s\n",(char*)data);
kill(pid,SIGKILL);
waitpid(pid,&status,0);
return data;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)