Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中并行测量循环sigwait的时间_C_Time_Parallel Processing_Signals_Posix - Fatal编程技术网

在C中并行测量循环sigwait的时间

在C中并行测量循环sigwait的时间,c,time,parallel-processing,signals,posix,C,Time,Parallel Processing,Signals,Posix,我有一个向孩子们发送信号的主程序,如下所示: void handleInputs(query_data *q_data) { char input[DEFAULT_INPUT_SIZE]; while(1) { if(fgets(input,DEFAULT_INPUT_SIZE,stdin)==NULL) ERR("FGETS"); input[strlen(input)-1] = '\0'; if(!

我有一个向孩子们发送信号的主程序,如下所示:

void handleInputs(query_data *q_data)
{
    char input[DEFAULT_INPUT_SIZE];
    while(1)
    {

        if(fgets(input,DEFAULT_INPUT_SIZE,stdin)==NULL) ERR("FGETS");
        input[strlen(input)-1] = '\0';
        if(!strcmp(input,"exit"))
        {
            kill(0,SIGTERM);
            exit(EXIT_SUCCESS);
        }
        else if(!strcmp(input,"index"))
            kill(0,SIGUSR2);

        else if(!strcmp(input,"status"))
            kill(0,SIGUSR1);    
        else
        {
            char* token = strtok(input, " ");
            if(!token) printf("Wrong input\n");
            else
            {
                if(!strcmp(token,"query"))
                {
                    initializeQueryNumbers(q_data,token);
                    createQueryThread(q_data);
                }
                else printf("Wrong input\n");
            }
            
        }   
    }
}
void waitForSignals(data* ch_data)
{
   
    char* pid_directory = strConcat(ch_data->dir, "/.numf_pid");
    int signo;
    for(;;)
    {
        //how to make this block independent to the sigwait
        printf("time elapse: %ld  interval: %d\n",time(NULL)-ch_data->end, ch_data->i);
        if((time(NULL)- ch_data->end)>ch_data->i && ch_data->status ==0)
        {
            printf("Time elapsed from the last indexing > %d\n",ch_data->i);
            createIndexingThread(ch_data);
        }
       //end here

        if(sigwait(&(ch_data->mask), &signo)) ERR("SIGWAIT");
        switch(signo)
        {
            case SIGUSR1:
                if(ch_data->status == 1)
                {
                    printf("Indexing in progress for directory: %s\n",ch_data->dir);
                    printf("Time elapsed: %ld\n", time(NULL)- ch_data->start);
                }
                else printf("No indexing in progress for directory: %s\n",ch_data->dir);
                break;
            case SIGUSR2:
                if(ch_data->status ==0) 
                    createIndexingThread(ch_data);
                else printf("Indexing already in progess for directory: %s\n",ch_data->dir);
                break;
            case SIGTERM:
                if(remove(pid_directory)<0) ERR("REMOVE");
                free(pid_directory);
                if(pthread_cancel(ch_data->tid)==0)
                    printf("Stopped the indexing of directory %s\n",ch_data->dir);
                exit(EXIT_SUCCESS);
        }

        
    }
    if(pthread_join(ch_data->tid,NULL)) ERR("PTHREAD_JOIN");
}
然后我有一个子进程,它在循环中使用sigwait等待信号,如下所示:

void handleInputs(query_data *q_data)
{
    char input[DEFAULT_INPUT_SIZE];
    while(1)
    {

        if(fgets(input,DEFAULT_INPUT_SIZE,stdin)==NULL) ERR("FGETS");
        input[strlen(input)-1] = '\0';
        if(!strcmp(input,"exit"))
        {
            kill(0,SIGTERM);
            exit(EXIT_SUCCESS);
        }
        else if(!strcmp(input,"index"))
            kill(0,SIGUSR2);

        else if(!strcmp(input,"status"))
            kill(0,SIGUSR1);    
        else
        {
            char* token = strtok(input, " ");
            if(!token) printf("Wrong input\n");
            else
            {
                if(!strcmp(token,"query"))
                {
                    initializeQueryNumbers(q_data,token);
                    createQueryThread(q_data);
                }
                else printf("Wrong input\n");
            }
            
        }   
    }
}
void waitForSignals(data* ch_data)
{
   
    char* pid_directory = strConcat(ch_data->dir, "/.numf_pid");
    int signo;
    for(;;)
    {
        //how to make this block independent to the sigwait
        printf("time elapse: %ld  interval: %d\n",time(NULL)-ch_data->end, ch_data->i);
        if((time(NULL)- ch_data->end)>ch_data->i && ch_data->status ==0)
        {
            printf("Time elapsed from the last indexing > %d\n",ch_data->i);
            createIndexingThread(ch_data);
        }
       //end here

        if(sigwait(&(ch_data->mask), &signo)) ERR("SIGWAIT");
        switch(signo)
        {
            case SIGUSR1:
                if(ch_data->status == 1)
                {
                    printf("Indexing in progress for directory: %s\n",ch_data->dir);
                    printf("Time elapsed: %ld\n", time(NULL)- ch_data->start);
                }
                else printf("No indexing in progress for directory: %s\n",ch_data->dir);
                break;
            case SIGUSR2:
                if(ch_data->status ==0) 
                    createIndexingThread(ch_data);
                else printf("Indexing already in progess for directory: %s\n",ch_data->dir);
                break;
            case SIGTERM:
                if(remove(pid_directory)<0) ERR("REMOVE");
                free(pid_directory);
                if(pthread_cancel(ch_data->tid)==0)
                    printf("Stopped the indexing of directory %s\n",ch_data->dir);
                exit(EXIT_SUCCESS);
        }

        
    }
    if(pthread_join(ch_data->tid,NULL)) ERR("PTHREAD_JOIN");
}
void waitForSignals(数据*Chu数据)
{
char*pid_directory=strConcat(ch_data->dir,“/.numf_-pid”);
国际签名;
对于(;;)
{
//如何使此块独立于sigwait
printf(“时间流逝:%ld间隔:%d\n”,时间(空)-通道数据->结束,通道数据->i);
如果((时间(空)-通道数据->结束)>通道数据->i&&C通道数据->状态==0)
{
printf(“从上次索引开始经过的时间>%d\n”,CHU数据->i);
createIndexingThread(CHU数据);
}
//到此为止
如果(sigwait(&(CHU数据->掩码),&signo))错误(“sigwait”);
开关(信号)
{
案例SIGUSR1:
如果(通道数据->状态==1)
{
printf(“正在为目录%s\n编制索引”,目录数据->目录);
printf(“经过的时间:%ld\n”,时间(NULL)-CHU数据->开始);
}
else printf(“目录:%s\n,目录数据->目录无索引进行中”);
打破
案例信号2:
如果(通道数据->状态==0)
createIndexingThread(CHU数据);
else printf(“目录%s\n的索引已在进行中”,目录数据->目录);
打破
案例术语:
如果(删除(pid_目录)tid)==0)
printf(“已停止对目录%s\n的索引”,目录数据->目录);
退出(退出成功);
}
}
if(pthread_join(ch_data->tid,NULL))ERR(“pthread_join”);
}

我的子进程创建了执行一些索引的线程。我想在主进程发出SIGUSR2信号(工作正常)或上次索引所用的时间大于用户给定的时间(存储在Chu data->I中)时创建这样的线程。然而,现在注释之间的块只有在我从主进程发送了一些信号之后才激活。使其并行工作的最佳方法是什么?

如果我理解正确,您会问如何根据信号或最近一次操作后的时间安排执行线程操作,以先到者为准

您可以使用而不是
sigwait
,根据需要重新计算超时

您可能需要启动该操作(或者,可能是给自己发信号)。您需要检查操作是否最近执行,并且您需要适当地重新配置(重新安排)下一个计时器,可能是在每次操作结束时

您可能只是有一个空闲的循环后台线程,它执行计时器所执行的操作。有时,这些比
sigevent
计时器更容易推理


您可以合并这样一个事件驱动的框架,它知道如何处理超时和信号,而不必担心实现。但是,您可能仍然需要重新计算到期日。

input[strlen(input)-1]='\0'strlen()
为零或字符串未以换行符结尾,则code>将失败。fgets不总是在末尾添加“\n”吗?我尝试了空输入和“0”,效果很好。关于输入和
fgets
,请尝试输入长度等于或大于
DEFAULT\u input\u SIZE
的内容。对于大输入,效果也很好。这不是我的问题。我们知道这不是问题,但这是个问题。你遇到的无关问题越少,我们就越容易帮助你。至于
fgets
问题,让我们假设
DEFAULT\u INPUT\u SIZE
2
。您将
ab\n
作为输入
fgets
将只读取
a
,而
输入的内容将是
a\0
。使用
strlen(输入)-1
将覆盖
a
使字符串
\0\0
。改为使用,就像在
input[strcspn(input,“\n”)]='\0'
中一样,无论字符串中是否有换行符,它都会做正确的事情。我通过结合使用sigpending和sigwait成功地做到了这一点。谢谢anyway@MichałFilipiak,如何?这两个函数都没有引入计时机制。你想发布你自己问题的答案吗?