首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:共享内存

:共享内存
EN

Stack Overflow用户
提问于 2013-12-27 19:59:15
回答 1查看 1.3K关注 0票数 0

下面的代码有问题:

代码语言:javascript
复制
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信号量。服务器必须等到客户端输入两个值后才处理输入,客户端必须等到服务器处理完输入并打印输出后才提示输入值。

问题是,代码并没有表现出应有的效果。

它应该是:

代码语言:javascript
复制
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

但它所做的是:

代码语言:javascript
复制
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.

我不知道哪里出问题了。有人知道为什么密码会这么做吗?

EN

回答 1

Stack Overflow用户

发布于 2013-12-28 00:20:54

\n保留在scanf格式字符串的末尾几乎总是一个问题(因为它不是交互式的,所以对fscanf来说不是问题),因为格式字符串中的任何空格字符实际上都代表"0或更多(可能无限多)空格字符“--它们可以是任何空白字符(空格、制表符或回车返回)。

因此,当您将\n (或<space>\t)放在格式字符串的末尾时,scanf将不会停止并返回输入,直到它看到一个非空白字符(或遇到某种匹配错误)。从你的角度看,它看起来好像没有打印提示符,或者处理输入,因为它不是--它仍然在同一个位置。如果您只在行上键入一个数字,也会发生同样的情况--程序将暂停,因为scanf仍在等待您键入第二个数字(与以前一样,您键入的enter键“适合”格式中的空格,因此还没有出现匹配错误)。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20807033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档