gpt4 book ai didi

c - 在 yyparse() 调用时执行操作?

转载 作者:行者123 更新时间:2023-11-30 17:26:35 31 4
gpt4 key购买 nike

我正在开发一种脚本语言和 C API,以便 C 程序可以与我的语言交互。它基于 LUA 的堆栈方法。但是,我在两个模块之间的同步方面遇到了一些问题......

在 API 的 init 函数上,我启动了一个线程,该线程对我的解释器进行系统调用,以便它准备好接收我希望它执行的命令。我还创建了 2 个信号量,以便我可以(理论上)将我的 API 与解释器模块同步。我需要按以下顺序完成操作:初始化解释器 -> API 发送命令 -> 解释器执行所述命令 -> 等待新命令。

这是解释器的主要功能:

int main(int argc, char* argv[]) { 

extern FILE *yyin;

char *semRead = "/canRead";
char *semSend = "/canSend";

sem_t *canRead;
sem_t *canSend;

//this part is just so that my code will be coming through a pipe
//created by me, instead of stdin
if(argc == 1)
yyin = stdin;
else if(argc == 2){

suppress = 1;

if(strcmp(argv[1], "-s") == 0){

char *source = "/tmp/colliPipe";
mkfifo(source, 0666);

yyin = fopen(source, "w+");
}
else{
printf("Error: unknown option %s\n", argv[1]);
return -1;
}
}
else if(argc > 2){
printf("Error: too many parameters\n");
return -2;
}
#if YYDEBUG == 1
extern int yydebug;
yydebug = 1;
#endif

initTables(); //Initializing symbol tables

if(!suppress){
printf("Colli 0.0.1 - 2014");
printf(">> ");
}

canRead = sem_open(semRead, O_CREAT, 0644, 0);
canSend = sem_open(semSend, O_CREAT, 0644, 0);

sem_post(canSend);

yyparse();

return 0;
}

事情是:当我创建线程时,我不能保证我的解析器将准备好接收我的输入。 sem_post(canSend)用于表示解析器应该准备好获取所述输入,但它不起作用,因为操作系统可以决定在发布之后但在 yyparse() 之前交换进程。 .

问题是:我能否确保解析器在发布到信号量之前已准备好进行解析?

提前致谢!

最佳答案

如果有人想知道如何解决,我解决了......

刚刚决定创建一个线程来调用yyparse(),允许我在创建后放置 sem_post...它似乎工作得很好。

关于c - 在 yyparse() 调用时执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725108/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com