两个信号量没有通信

时间:2016-05-31 08:48:36

标签: c linux locking semaphore

我有一个使用信号量的客户端服务器项目。 我从同一个文件夹运行,他们使用相同的密钥。 现在,我希望服务器锁定信号量,因此客户端无法运行命令直到服务器释放它,但客户端忽略了服务器的锁定。我不明白我的错误在哪里。服务器代码:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/stat.h>
#define FLAGS IPC_CREAT | 0644
union semun {
    int val;
    struct semid_ds *buf;
    ushort *array; };
union semun semarg;
struct sembuf sops[1];
int main() {
    semarg.val=1;
    int resultsCreator=open("results.txt",O_CREAT|O_RDWR);
    key_t key;
    key = ftok("results.txt", 'k');
    int shmid = shmget(key, 12, FLAGS);
    int semfor = semget(key, 1, IPC_CREAT | IPC_EXCL | 0666);
    semctl ( semfor , 0 , SETVAL , semarg );
    sops->sem_num = 0;
    sops->sem_flg = 0;
    sops->sem_op = -1;
    int k = semop ( semfor , sops , 1 ); //lock the semaphore
    char* shmaddr;
    int numWaiting =0;
    while(1){   
        sleep(2); //CHECK EVERY 2 SECONDS IF SOMEONE IS WAITING
        numWaiting = semctl(semfor, 0, GETNCNT, semarg); 
        if(numWaiting==0){  
            printf("none waiting\n");
            continue; }
        printf("more than one waiter\n");  //NEVER GETS HERE
    } //END WHILE

客户代码:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/stat.h>
#define FLAGS IPC_CREAT | 0644
union semun {
    int val;
    struct semid_ds *buf;
    ushort *array;
};   
union semun semarg;
struct sembuf sops[1];
int main()
{
    key_t key;
    key = ftok("results.txt", 'k');
    int shmid = shmget(key, 12, FLAGS);
    semarg.val=1;
    int semfor = semget(key, 0, 0666);
    semctl ( semfor , 0 , SETVAL , semarg );
    sops->sem_num = 0;
    sops->sem_flg = 0;
    sops->sem_op = -1;
    semop ( semfor , sops , 1 );
    printf("skipped lock\n"); //PRINTS IT, EVEN WHEN IT'S STILL LOCKED
    sops->sem_op = 1;
    semop ( semfor , sops , 1 );
    return 0;
}

为什么客户端会忽略服务器的信号量锁?

1 个答案:

答案 0 :(得分:0)

有一个类似的问题我回答了,所以我只是将它复制到这里

如果你是技术人员,如果你在线程之间同步任务,你应该使用信号量。在解析输入之前读取输入的示例。 Here's关于信号量的答案。

但是如果您正在使用共享资源,并且需要同时避免竞争条件/两个线程访问,则应使用互斥锁。 Here's关于什么是互斥锁的问题。

另请参阅迈克尔巴尔的disambiguation,这是非常好的。

我会彻底阅读这个问题和消除歧义,你实际上最终可能不会使用信号量和互斥量,因为根据你的解释,你只是控制共享资源。

常见的信号量函数

int sem_init(sem_t *sem, int pshared, unsigned int value); //Use pshared with 0, starts the semaphore with a given value

int sem_wait(sem_t *sem);//decreases the value of a semaphore, if it's in 0 it waits until it's increased

int sem_post(sem_t *sem);//increases the semaphore by 1

int sem_getvalue(sem_t *sem, int *valp);// returns in valp the value of the semaphore the returned int is error control

int sem_destroy(sem_t *sem);//destroys a semaphore created with sim_init

常用互斥锁功能(用于unix)

int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *attr); //starts mutex pointed by p_mutex, use attr NULL for simple use

int pthread_mutex_lock(pthread_mutex_t *p_mutex); //locks the mutex

int pthread_mutex_unlock(pthread_mutex_t *p_mutex); //unlocks the mutex

int pthread_mutex_destroy(pthread_mutex_t *p_mutex);//destroys the mutex

我不确定为什么你的代码不起作用,但是根据你所解释的你不需要使用信号量,因为客户端和服务器之间没有同步只使用共享资源。尝试用互斥锁重写信号量部分,你会发现它看起来更自然,最终可能会解决这个问题。

相关问题