이 질문을 downvote하지 말아라, 나는 지나치게 구체적이지 않다는 것을 깨닫는다. 그러나 그것은 정확하게 나의 문제 다. 모든 명령이 내 코드 내에서 무엇을하는지 알고 있습니다. 처음에는 그 이유를 모릅니다. 이것은 나의 질문이 내 자신의 프로그램과 대부분 관련이 있기 때문에 답을 찾는 것이 광기로 어렵다. 사과해도 여전히 답할 수 없다면 미래의 질문을 개선하기 위해 노력할 것입니다 :).* randNum이 증가하는 이유는 무엇입니까?
프로세스를 생성하고 삭제하기 위해 공유 메모리를 통해 통신 할 수있는 프로그램을 작성해야합니다. 나는 주어진 코드 조각, 특히 아래의 비트를 이해하려고 노력하고있다. 맨 밑에는 누군가가 내 질문에 대답하는 데 도움이되는 경우 전체 프로듀서 코드가 포함되어 있습니다.
질문 : 왜 * 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(*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;
}
이 프로그램의 동작을 이해하기 위해 (gdb''같은) 디버거를 사용; 예를 들어 그것을 컴파일하십시오. 'gcc -Wall -g' ... –