Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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
Solaris 5.10——不能等待C++命名信号量 我尝试使用Solaris 5.10机器上的C++ POSIX命名为SimaPosies,SimyOpCon,SmithAuto,SigiPoST。目标是创建一个银行服务器程序,它可以同时接受多个客户端连接,并执行请求的事务。我接受每个请求,然后派生一个子进程来维护该连接。分配的要点是用信号量和共享内存同步这些进程。我的课程结构如下: #define BALANCE_SEM "/balance_sem" // listen on a port and other stuff static int64_t *balance = (int64_t *) mmap(NULL, sizeof(*balance), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); sem_t *balance_lock; if ((balance_lock = sem_open(BALANCE_SEM, O_CREAT, 0644, 1)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to initialize semaphore: %s\n", strerror(errno)); } while (1) { // accept request from client int pid = fork(); // child process if (pid == 0) { sem_t *balance_sem; if ((balance_sem = sem_open(BALANCE_SEM, 0)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to open existing named semaphore: %s\n", strerror(errno)); } transaction t = getTransactionFromClient(); switch (t.type) { case 0: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance + t.amount; sem_post(balance_sem); case 1: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance - t.amount; sem_post(balance_sem); } // parent process } else { // do some other stuff } }_C++_Solaris_Semaphore_Fork - Fatal编程技术网

Solaris 5.10——不能等待C++命名信号量 我尝试使用Solaris 5.10机器上的C++ POSIX命名为SimaPosies,SimyOpCon,SmithAuto,SigiPoST。目标是创建一个银行服务器程序,它可以同时接受多个客户端连接,并执行请求的事务。我接受每个请求,然后派生一个子进程来维护该连接。分配的要点是用信号量和共享内存同步这些进程。我的课程结构如下: #define BALANCE_SEM "/balance_sem" // listen on a port and other stuff static int64_t *balance = (int64_t *) mmap(NULL, sizeof(*balance), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); sem_t *balance_lock; if ((balance_lock = sem_open(BALANCE_SEM, O_CREAT, 0644, 1)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to initialize semaphore: %s\n", strerror(errno)); } while (1) { // accept request from client int pid = fork(); // child process if (pid == 0) { sem_t *balance_sem; if ((balance_sem = sem_open(BALANCE_SEM, 0)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to open existing named semaphore: %s\n", strerror(errno)); } transaction t = getTransactionFromClient(); switch (t.type) { case 0: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance + t.amount; sem_post(balance_sem); case 1: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance - t.amount; sem_post(balance_sem); } // parent process } else { // do some other stuff } }

Solaris 5.10——不能等待C++命名信号量 我尝试使用Solaris 5.10机器上的C++ POSIX命名为SimaPosies,SimyOpCon,SmithAuto,SigiPoST。目标是创建一个银行服务器程序,它可以同时接受多个客户端连接,并执行请求的事务。我接受每个请求,然后派生一个子进程来维护该连接。分配的要点是用信号量和共享内存同步这些进程。我的课程结构如下: #define BALANCE_SEM "/balance_sem" // listen on a port and other stuff static int64_t *balance = (int64_t *) mmap(NULL, sizeof(*balance), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); sem_t *balance_lock; if ((balance_lock = sem_open(BALANCE_SEM, O_CREAT, 0644, 1)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to initialize semaphore: %s\n", strerror(errno)); } while (1) { // accept request from client int pid = fork(); // child process if (pid == 0) { sem_t *balance_sem; if ((balance_sem = sem_open(BALANCE_SEM, 0)) == SEM_FAILED) { fprintf(stderr, "ERROR: unable to open existing named semaphore: %s\n", strerror(errno)); } transaction t = getTransactionFromClient(); switch (t.type) { case 0: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance + t.amount; sem_post(balance_sem); case 1: if (sem_wait(balance_sem) < 0) { fprintf(stderr, "ERROR: failed to wait on semaphore: %s\n", strerror(errno)); } *balance = *balance - t.amount; sem_post(balance_sem); } // parent process } else { // do some other stuff } },c++,solaris,semaphore,fork,C++,Solaris,Semaphore,Fork,有趣的是,当我在Linux机器上运行服务器程序时,我已经尝试了好几次。但是,当我在Solaris计算机上运行服务器程序时,我的错误消息会打印在sem_wait调用上,而不会打印在sem_open调用上,strerrorno返回无效参数。有什么不对劲吗 编辑: 事实上,以下极其简化的代码也会出现相同的错误,根本不需要分叉: sem_t *balance_lock; if ((balance_lock = sem_open(BALANCE_SEM, O_CREAT, 0644, 1)) == SEM

有趣的是,当我在Linux机器上运行服务器程序时,我已经尝试了好几次。但是,当我在Solaris计算机上运行服务器程序时,我的错误消息会打印在sem_wait调用上,而不会打印在sem_open调用上,strerrorno返回无效参数。有什么不对劲吗

编辑:

事实上,以下极其简化的代码也会出现相同的错误,根本不需要分叉:

sem_t *balance_lock;
if ((balance_lock = sem_open(BALANCE_SEM, O_CREAT, 0644, 1)) == SEM_FAILED) {
    fprintf(stderr, "ERROR, unable to initialize semaphore: %s\n", strerror(errno));
}

if (sem_wait(balance_lock) < 0) {
    fprintf(stderr, "ERROR, failed to wait on semaphore: %s\n", strerror(errno));
}    

您是否打算告诉我们在等待时发生了什么错误?失败的sem_wait调用应该将errno设置为指示问题性质的代码。看看这种性质是什么是有益的。在你的失败案例中调用Pelor来输出那个信息怎么样?对不起,我忘了加上,但是我编辑了它。如果你编译成C++,你就正确地标记了这个问题,但是在口语中说得很简单,这里的C++非常少。事实上,除了t的定义中缺少的结构前缀,我看不到任何!为什么?这只是计划的一部分。我有一些类和一些继承正在那里进行,以封装一些套接字代码,以及其他东西。这实际上只是服务器程序主功能的精简版本,因此您不会看到太多。老实说,我本来可以用C来写整个东西的。