繁体   English   中英

C-POSIX:共享内存

[英]C - POSIX: 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.

我不知道出了什么问题。 有谁知道,为什么代码要这么做?

scanf格式字符串的末尾留下\\n几乎总是一个问题(对于fscanf ,这是没有问题的,因为它不是交互式的),因为格式字符串中的任何空格字符实际上都表示“ 0或更大(可能无限) (很多)空白字符”-并且它们可以是任何空白字符(空格,制表符或回车符)。

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

自从%d (以及几乎比其他所有的格式说明%c%[ )忽略前导空格无论如何 ,你经常会发现明确包括它们是没有必要的。

暂无
暂无

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

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