简体   繁体   English

信号量问题在 C 中无法正常工作

[英]Issue with semaphore that doesn't work as excpected in C

I am currently trying to make a client/server program.我目前正在尝试制作一个客户端/服务器程序。 The server needs to prevent error from receiving multiple message at the exact same time.服务器需要防止错误同时接收多条消息。

Here is the server.c code:这是 server.c 代码:

#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/sem.h>
#include <time.h>
#include <semaphore.h>

typedef struct {
    long id;
    char mes[20];
} message;

double calculate(int num){
    return num*2;
}

struct sembuf semaphore_operations[1];

int main() {
    
    // key
    key_t cle = ftok(".", 0);
    if (cle == -1) {
        perror("ftok");
        return -1;
    }

    // message queue
    int msqId = msgget(cle, IPC_CREAT | 0700);
    if (msqId == -1) {
        perror("msgget");
        return -1;
    }

    // semaphore
    int semId = semget(cle, 1, IPC_CREAT | 0700);
    if (semId == -1) {
        perror("semget");
        return -1;
    }

    if (semctl(semId, 0, SETVAL, 1) == -1) {
        perror("semctl");
        return -1;
    }

    semaphore_operations[0].sem_num = 0;
    semaphore_operations[0].sem_flg = 0;

    message mes;

    while (1) {
        
        semaphore_operations[0].sem_op = -1;
        if (semop(semId, semaphore_operations, 1) == -1) {
            perror("semop");
            return -1;
        }

        // wait for message (blocked)
        int received = msgrcv(msqId, &mes, sizeof(message) - sizeof(long), 0, 0);
        if (received == -1) {
            perror("msgrcv");
            return -1;
        }

        int num_mat = atoi(mes.mes);
        printf("Server: message received :%d.\n", num_mat);

        if(num_mat > 0) {
            double result = calculate(num_mat);
            char result_str[20] = "";
            sprintf(result_str, "%f", result);
            strcpy(mes.mes, result_str);

            int sent = msgsnd(msqId, &mes, sizeof(message) - sizeof(long), 0);
            if (sent == -1) {
                perror("msgsnd");
                return -1;
            }
            printf("Server: message sent.\n");
        }
        else {
            strcpy(mes.mes, "Invalid number");
            int sent = msgsnd(msqId, &mes, sizeof(message)-sizeof(long), 0);
        
            if(sent == -1){
                perror("msgsnd");
                return -1;
            }
        }
        
        // signal the semaphore
        semaphore_operations[0].sem_op = 1;
        if (semop(semId, semaphore_operations, 1) == -1) {
            perror("semop");
            return -1;
        }
    }

    // remove when server shut down
    msgctl(msqId, IPC_RMID, NULL);
    semctl(semId, 0, IPC_RMID, 0);

    return 0;
}

And here is the cli_test.c that send two message at the same time:这是同时发送两条消息的 cli_test.c:

#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/sem.h>
#include <time.h>
#include <semaphore.h>
#include <pthread.h>

typedef struct {
    long id;
    char mes[20];
} message;

void* client_thread(void* arg) {
    
    key_t cle = ftok(".", 0);
    if (cle == -1) {
        perror("ftok");
        pthread_exit(NULL);
    }

    int msqId = msgget(cle, 0);
    if (msqId == -1) {
        perror("msgget");
        pthread_exit(NULL);
    }

    message mes;
    mes.id = 1;
    int num = *((int*) arg);
    sprintf(mes.mes, "%d", num);

    // send message to the server
    int sent = msgsnd(msqId, &mes, sizeof(message) - sizeof(long), 0);
    if (sent == -1) {
        perror("msgsnd");
        pthread_exit(NULL);
    }

    // receive the result
    message result;
    int received = msgrcv(msqId, &result, sizeof(message) - sizeof(long), 0, 0);
    if (received == -1) {
        perror("msgrcv");
        pthread_exit(NULL);
    }
    
    printf("Client: message received :%s.\n", result.mes);

    pthread_exit(NULL);
}

int main() {
    
    pthread_t client1, client2;
    int num1 = 10, num2 = -1;

    if (pthread_create(&client1, NULL, client_thread, &num1) != 0) {
        perror("pthread_create");
        return -1;
    }

    if (pthread_create(&client2, NULL, client_thread, &num2) != 0) {
        perror("pthread_create");
        return -1;
    }

    pthread_join(client1, NULL);
    pthread_join(client2, NULL);

    return 0;
}

The server seems not te work because the output of cli_test is:服务器似乎无法正常工作,因为 cli_test 的输出是:

Client: message received :20.000000.
Client: message received :-1.

The expected output should be:预期的输出应该是:

Client: message received :20.000000.
Client: message received :Invalid number.

Changing num1 to -12 and num2 to -1,将 num1 更改为 -12,将 num2 更改为 -1,

Client: message received :-1.
Client: message received :Invalid number.

The expected output should be:预期的输出应该是:

Client: message received :Invalid number.
Client: message received :Invalid number.

Changing num1 to 12 and num2 to 1,将 num1 更改为 12,将 num2 更改为 1,

Client: message received :24.000000.
Client: message received :1.000000.

The expected output should be:预期的输出应该是:

Client: message received :24.000000.
Client: message received :2.000000.

It seems to work a half, it calculate only one number and the other one isn't calculated and just sent to the client back...它似乎工作了一半,它只计算一个数字而另一个不计算而只是发送给客户......

A semaphore that's used by only one task (process, thread) makes no sense.仅由一个任务(进程、线程)使用的信号量没有任何意义。 It's all wrong.都是错的。 I have no idea what you're even trying to achieve with it.我不知道你甚至想用它实现什么。

If you simply remove the semaphore code, your program works as expected.如果您简单地删除信号量代码,您的程序将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM