Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
IPC:每秒从客户端收集数据_C_Pthreads_Semaphore - Fatal编程技术网

IPC:每秒从客户端收集数据

IPC:每秒从客户端收集数据,c,pthreads,semaphore,C,Pthreads,Semaphore,我想从连接到我的服务器的所有客户端收集数据,我想最多等待1秒。如何使用信号量实现这一点? 我当前的代码: int players=2; while(1){ //request for choice for(int i = 0; i<players; i++) sem_post(&sharedMemory->request_choice); //wait for data for (int i = 0;i<players;

我想从连接到我的服务器的所有客户端收集数据,我想最多等待1秒。如何使用信号量实现这一点? 我当前的代码:

int players=2;
while(1){
    //request for choice
    for(int i = 0; i<players; i++)
        sem_post(&sharedMemory->request_choice);
    //wait for data
    for (int i = 0;i<players; i++)
        //ok..I have data but not in 1 second..
        sem_wait(&sharedMemory->got_choice);


    //updating data..
}
int-players=2;
而(1){
//请求选择
for(int i=0;irequest_选项);
//等待数据
for(int i=0;igot_选择);
//正在更新数据。。
}

简介

#include <semaphore.h>
#include <time.h>

int sem_timedwait(sem_t *restrict sem,
       const struct timespec *restrict abstime);
该链接还提供了以下示例用法:


好的,但是我想同时为所有客户端等待1秒,它会工作吗?这取决于您如何实现同步-您可以使用多个信号量。除了共享内存和信号量之外,还有很多方法可以处理这个问题——消息队列或管道会立即浮现在脑海中。
[ETIMEDOUT]
    The semaphore could not be locked before the specified timeout expired.
/* Calculate relative interval as current time plus
   number of seconds given argv[2] */


if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
    perror("clock_gettime");
    exit(EXIT_FAILURE);
}
ts.tv_sec += atoi(argv[2]);


printf("main() about to call sem_timedwait()\n");
while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR)
    continue;       /* Restart if interrupted by handler */