我目前正在为类做一个项目,其中包括一个基于内核的信号量实现。我没有使用DEFINE_SPINLOCK(sem_lock);作为/内核/sys.c中的一个全局变量,我创建了一个方案,其中信号量结构包含一个spinlock_t变量(在用户空间中伪装为spinlock_t*),以便允许不同的信号量独立地下行()/up()(这应该转换成更高效的代码,减少等待时间)。我创建一个syscall来初始化一个信号量,传递一个指向结构的指针。但是,在初始化信号量的spinlock_t时,一行出现了错误。
我联系了我的教授,他说为了这个任务的目的,只是使用带有全局自旋锁定义的粗实现。然而,很难让这件事过去。虽然我显然不介意走这条路,但我仍然想了解为什么我的实现不起作用。有人能帮我吗?
以下是错误:
CC kernel/sys.o
kernel/sys.c: In function 'sys_cs1550_sem_init':
kernel/sys.c:2491: error: expected '=', ',', ';', 'asm' or '__attribute__' before '->' token
kernel/sys.c:2491: error: expected expression before '->' token
make[1]: *** [kernel/sys.o] Error 1
make: *** [kernel] Error 2我试过以下几种方法:
DEFINE_SPINLOCK(sem->sem_lock);
DEFINE_SPINLOCK(&(sem->sem_lock));
DEFINE_SPINLOCK((&(sem->sem_lock)));
DEFINE_SPINLOCK(*(sem->sem_lock));
spinlock_t *lock = &(sem->sem_lock);
DEFINE_SPINLOCK(lock);我在/ code /sys.c中为信号量结构编写的代码:
/*
* Initialize a semaphore with the creation of it's spinlock.
* The idea is to avoid having spinlocks in user space, by making
* the process as opaque as possible. Our lock is defined in the kernel as a spinlock_t,
* but as a void* in userspace. This allows us to have more than one semaphore as needed.
*/
struct cs1550_sem{
spinlock_t *sem_lock; //The lock for our semaphore.
int available_resources; //Total # of available resources.
struct cs1550_pnode *process_list; //Pointer to the first node of our linked list.
};我在/ code /sys.c中用于信号量初始化syscall的代码:
asmlinkage long sys_cs1550_sem_init(struct cs1550_sem *sem, int resource_cap){
DEFINE_SPINLOCK(sem->sem_lock); //Define our lock.
sem->process_list = NULL; //Ensure we have a 'stopping point' when dequeueing.
sem->available_resources = resource_cap;
return 0;
}
EXPORT_SYMBOL_GPL(sys_cs1550_sem_init);耽误您时间,实在对不起。
发布于 2014-10-03 12:51:42
不能以这种方式使用此宏。
// spinlock_types.h
#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)您的结构可能如下所示:
struct cs1550_sem{
spinlock_t sem_lock; //The lock for our semaphore.
int available_resources; //Total # of available resources.
struct cs1550_pnode *process_list; //Pointer to the first node of our linked list.
};和初始化:
asmlinkage long sys_cs1550_sem_init(struct cs1550_sem *sem, int resource_cap){
spin_lock_init(&sem->sem_lock); //Define our lock.
sem->process_list = NULL; //Ensure we have a 'stopping point' when dequeueing.
sem->available_resources = resource_cap;
return 0;
}看看https://www.kernel.org/doc/Documentation/spinlocks.txt和init(.)的第3课(第295行)
https://stackoverflow.com/questions/26178819
复制相似问题