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

时间:2014-11-03 23:22:36

标签: c linux compiler-construction ipc bison

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

在我的API上的init函数中,我启动了一个线程,它对我的​​解释器进行系统调用,以便它可以接收我想要它执行的命令。我还创建了2个信号量,以便我(理论上)可以将我的API与解释器模块同步。我需要按以下顺序完成操作:Initialise Interpreter - > 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()之前交换进程。

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

刚决定创建一个调用yyparse()的线程,允许我在创建后放置sem_post ......看起来效果很好。