请不要反对这个问题,我知道这不是太具体,但这正是我的问题。我知道代码中每个命令的作用,我只是不知道它们最初出现在其中的原因。由于我的问题主要与我自己的程序有关,因此很难找到答案。抱歉,如果仍然无法解决,我将尽力改善我以后的问题:)。

我需要编写一个程序,该程序可以在共享内存之间进行通信,轮流创建和删除进程。我试图理解我得到的那段代码,尤其是下面的内容。在最底部,我包括了整个生产者代码,以防它帮助任何人回答我的问题。

问题:为什么* randNum后来增加到101以上,它打印输出的条件是等于101吗?

这是否暗示消费者必须更改位置* randNum中包含的值才能满足条件?

for(A = 0; A < size; A++)    // for loop to reset all priority values so that they are clear to be used in the next set
    {
        *randNum = 101;
        *randNum++;
    }


稍后的if命令:

if(*randNum == 101)
                    {
                        *randNum = rand() % (100 - 1) + 1;
                        *pidNum = getpid();

                        printf("priority: %d Process ID: %d \n", *randNum, *pidNum);

                        x = 1;
                    }


如所承诺的,以下完整程序用于完成目的(试图使您更轻松并防止出现问题;还提供上下文)

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>


void shm2sz(int size);

int main(int argc, char *argv[])


{
    int shmid, A, B, count, *shm, *randNum, *pidNum, pid, *memSig;

    key_t key;

    int size = atoi(argv[1]);

    int shmsz = (size * 2) + 1;  // declaring size of shared mem to be twice the size of user input, + 1 for owner ID

    int x = 0;

    int noToCreate = atoi(argv[2]);

    shm2sz(shmsz);
    key = 2060;        // Identifier key for SharedMem

    shmid = shmget(key, shmsz, IPC_CREAT | 0666);  //creating Sharedmem

    if(shmid < 0)   // variable if sharedmem is less than 0, print error.

            {
                    perror("shmget");    // eror mesage print
                    exit(1);
            }

    shm = shmat(shmid, NULL, 0);    //Attach to shared mem, if fails.. proceed with error message

    if(shm == (int *) -1)   // eror message

        {
                perror("shmat");
                exit(1);
        }


    randNum = shm;         // declare randNum equal to shm

    pidNum = shm + size;   // set pid to the first bit of the second part of the shared mem

    memSig = shm + shmsz;  // set memsig as final value in shared mem

    *memSig = 0;

    for(A = 0; A < size; A++)    // for loop to reset all priority values so that they are clear to be used in the next set
    {
        *randNum = 101;
        *randNum++;
    }

    count = 0;      // set count back to 0

    randNum = shm;              //check randNum equal to shm
    pidNum = shm + size;

    while(*memSig != 2)
    {
        while(*memSig == 1)   // set memsignature to sleep while..
        {
            sleep(1);
        }

        for(B = 0; B < noToCreate; B++)
        {
            pid = fork();

            if(pid == -1)
            {
                perror("Error forking");
                exit(1);
            }
            else if(pid > 0)
            {
                wait(0);
            }
            else
            {
                srand(getpid());

                while(x == 0)
                {
                    if(*randNum == 101)
                    {
                        *randNum = rand() % (100 - 1) + 1;
                        *pidNum = getpid();

                        printf("priority: %d Process ID: %d \n", *randNum, *pidNum);

                        x = 1;
                    }
                    else
                    {
                        *randNum++;
                        *pidNum++;
                    }
                }
                exit(0);
            }
        } /* Closes main for loop */

        if(*memSig == 0)
        {
            *memSig = 1;
        }
    } /* Closes main while loop */
}

void shm2sz(int size)
{
    int shmid, *shm2;
    key_t key;

    key = 9876;

    shmid = shmget(key, 2, IPC_CREAT | 0666);

    if(shmid < 0)
    {
        perror("shmget2");
        exit(1);
    }

    shm2 = shmat(shmid, NULL, 0);

    if(shm2 == (int *) -1)
    {
        perror("shmat2");
        exit(1);
    }

    *shm2 = size;
}

最佳答案

后缀增量运算符的operator precedence高于指针解引用运算符。这意味着*randNum++实际上增加了指针randNum

如果要增加randNum指向的值,则必须使用括号:

(*randNum)++;

10-05 22:35