C-POSIX : Shared Memory

标签 c linux posix semaphore shared-memory

我在使用以下代码时遇到问题:

struct {
    int a, b;
    sem_t client;
    sem_t server;
} *shm_acc;

void server() {
    while(1) {
        sem_wait(&shm_acc->server);

        if(shm_acc->a == -1 && shm_acc->b == -1)
           return;

        printf("The product of the two entered numbers is %d\n", shm_acc->a * shm_acc->b)
        sem_post(&shm_acc->client);
    }
}

void client() {
    while(1) {
        printf("Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.\n");
        if(2 != scanf("%d %d\n", &shm_acc->a, &shm_acc->b) {
            perror("An error occured. Please try again\n");

            while(getchar() != '\n');

            continue;
        }

         sem_post(&shm_acc->server);

         if(shm_acc->a == -1 && shm_acc->b == -1)
            return;

         sem_wait(&shm_acc->client);

     }

     exit(EXIT_SUCCES);
}

int main() {
    setbuf(stdout, NULL);

    int shm = 0;
    shm = shmget(IPC_PRIVATE, SHAREDMEM_SIZE, S_IRUSR | S_IWUSR);
    if(shm == -1)
        perror("Failed to create shared memory.\n");

    shm_acc = shmat(shm, NULL, 0);

    sem_init(&shm_acc->client, 1, 0);
    sem_init(&shm_acc->server, 1, 0);

    pid_t pid = fork();

    if(pid == 0) {

        client();
        shmdt(shm_acc);
    } else if(pid >0) {

        server();
        waitpid(pid, NULL, 0);
        sem_destroy(&shm_acc->server);
        sem_destroy(&shm_acc->client);
    } else if(pid == -1) {

        perror("fork");
        sem_destroy(&shm_acc->server);
        sem_destroy(&shm_acc->client);
    }

    return 0;
}

在上面的代码中,客户端和服务器使用 POSIX 共享内存相互通信。父进程代表服务器,子进程代表客户端。另外,我正在使用 POSIX 信号量。这 服务器必须等到客户端输入两个值后才处理输入, 客户端必须等到服务器处理完输入并打印输出后才提示输入新的输入值

问题是,代码没有按应有的方式运行。

应该是:

Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
2 2
The product of the two entered values is 4.
Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
3 3
The product of the two entered values is 9.
Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
-1 -1
//exit

但它的作用是:

Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
2 2 // after entering it doesn't calculate the input
2 3 // after entering again it calculates 2x2
The product of the two entered values is 4.
Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
3 3
The product of the two entered values is 6. // This is the result of input 2x3, see above
Please enter two unsigned integer with whitespace in between. For exit enter -1 -1.
-1 -1
The product of the two entered values is 9
2 4
// Now it terminates.

我不知道出了什么问题。有谁知道为什么代码会那样做?

最佳答案

离开 \nscanf 的末尾格式字符串几乎总是会成为一个问题(对于 fscanf 问题不大,因为它不是交互式的),因为格式字符串中的任何空白字符实际上代表“0 个或更多(可能无限多)空白字符”——它们可以是任何空白字符(空格、制表符或回车符)。

因此当你输入 \n (或 <space>\t )在格式字符串的末尾,scanf在看到非空白字符(或遇到某种匹配错误)之前不会停止并返回输入。从您的角度来看,它似乎没有打印提示或处理输入;那是因为它不是——它仍然在同一个地方。如果您只在一行中键入一个数字,也会发生同样的事情——程序会暂停,因为 scanf仍会等待您键入第二个数字(和以前一样,您键入的回车键“适合”格式中的空格,因此还没有匹配错误)。

%d (以及除 %c%[ 之外的几乎所有格式说明符)忽略前导空格无论如何,您通常会发现没有必要显式包含它们。

关于C-POSIX : Shared Memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20807033/

相关文章:

c - 这两个 C 声明之间有什么区别?

c - 如何将一系列整数变量乘以系数的总和分解回变量?

python - 从 Django 应用程序触发分布式异步处理的良好架构是什么?

c++ - Linux下如何压缩图片?

c++ - 如何在 C++03 中用 sprintf 正确替换 sprintf_s?

c++ - 在信号处理程序中设置标志

c - 是否可以找出变量名,指向的指针?

c - XV6-循环调度程序

linux - ansible playbook/shell 命令的电子邮件结果?

regex - 无法使用 nginx logwarn 正确解析包含特定关键字的日志行