我正在学习如何在C (linux)中使用信号量。
我想问你:
如何确定订单是否会立即执行,还是会等待?
我喜欢睡着的理发师:
在函数中:
void *customer(void *number) {
int num = *(int *)number;
// Leave for the shop and take some random amount of
// time to arrive.
printf("Customer %d leaving for barber shop.\n", num);
randwait(5);
在我的应用程序中,我在父级和子级之间(在Linux和Windows上)为IPC使用共享内存。Linux的完整代码在上。
我在Linux上有下面的代码可以从共享内存中读取:
char buf[BUF_SIZE/4];
//pBuf is the shared memory location
sem_wait(semn);
//Wait for the parent process to write on the shared memory.
memcpy(buf, pBuf, sizeof(buf));
//Signal the parent
sem_post(sem0);
编写以下代码:
/
如何确定类unix操作系统(例如Mac /Linux2.6)是否实现了POSIX信号量?我可以使用sysconf来获取它吗?
#include <unistd.h>
long sysconf(int name);
我只想在Linux/Mac OS X中使用POSIX信号量api(sem_init/sem_wait/sem_post)。我看到Linux 2.6和MAC OS X 10.6.6都实现了POSIX信号量。但是,在编写跨平台应用程序时,如何确定类unix操作系统是否实现了POSIX信号量。
如何测试信号量是否阻塞?
我尝试了函数sem_trywait(),但它没有唤醒,我现在不知道为什么,你能帮我吗?
示例在这里(使用信号量解决睡眠理发师问题的示例程序):
我希望使用sem_trywait()来检测,是信号量blocket:
void *customer(void *number) {
int num = *(int *)number;
//there is my problem...
//you must waiting to free semaphore...
while(sem_trywait(&waitingRoom)){
有时,这个程序有一个分段错误。什么是分割错误?为什么会这样?我该怎么解决呢?
我期望产出如下:
I am consumer
I am producer
I am consumer
I am producer
(etc.)
然而,情况并非如此。有人能给我解释一下吗?
#include<stdio.h>
#include<semaphore.h>
#include<sys/stat.h>
#include<fcntl.h>
sem_t* mutex;
sem_t* null;
main()
{
int temp;
int pid;
pid
我试图实现一种简单的解决餐饮业问题的方法(与五位哲学家一起),我的解决方案是基于这个逻辑的:
sem_t S[philosophers_number]
for each philosopher
{
while(TRUE)
{
if(current philosopher number != last philosopher)
{
thinking()
//i is number of current philosopher
sem_wait(take_chopstick(S