일부 C 코드에서 꽤 이상한 출력이 발생합니다. 내 배경이 .NET 및 C#을 중심으로하므로 C 및 Linux 개발의 초보자입니다.공유 메모리를 사용할 때 하위 프로세스가 중단됩니까?
어쨌든 필자는 FAT12 구현과 명령 셸을 c에 작성했습니다. 자식 프로세스가 공유 메모리에 액세스하려고 할 때마다 내 셸이 멈 춥니 다. 사실 전혀 이상한 일은 전혀 일어나지 않습니다. 누구든지 코드 디버깅을 도와 줄 수 있습니까?
감사합니다,
이 쉘을 실행하는 메인 루프입니다 :
while(strcmp(input, "EXIT") != 0)
{
scanf("%s", input);
input = String_ToFixedArray(input);
array = StringArray_Create(input, " "); //split the input string into array.
if(array->Items == NULL || array->Size == 0)
{
input = "CONTINUE";
continue;
}
if(strcmp(String_ToUpper(array->Items[0]), "PBS") == 0)
{
pid_t processId;
if((processId = fork()) < 0)
{
printf("%s", "Error executing command.");
}
//child process. Nothing happens???????
if(processId == 0)
{
ExecutePBS();
}
}
else if(strcmp(String_ToUpper(array->Items[0]), "PFE") == 0)
{
printf("Execute Print Fat Entries (PFE) Command\n");
}
else if(strcmp(String_ToUpper(array->Items[0]), "EXIT") == 0)
{
printf("Exiting..");
break;
}
else
{
input = "CONTINUE";
}
}
이 부트 섹터 (PBS)의 내용을 인쇄하는 "드라이버"기능입니다. 문제는이 함수가 실행될 때마다 아무 일도 일어나지 않는다는 것입니다.
void ExecutePBS(void)
{
int shm_file_id;
char* shm_file;
char* shm_file_ptr;
struct shmid_ds shm_file_buffer;
if((shm_file_id = shmget(SHM_FILE_NAME_KEY,SHM_FILE_NAME_SIZE, 0666)) < 0)
{
perror("Error locating shared memory segment.");
exit(1);
}
if((shm_file = shmat(shm_file_id, NULL, 0)) == (char *) -1)
{
perror("Error attaching shared memory segment to process' scope.");
exit(1);
}
if(shmctl(shm_file_id, IPC_STAT, &shm_file_buffer) == -1)
{
perror("Error while attempting to control the shared memory segment used to store the floppy file name for IPC.");
exit(1);
}
sprintf(shm_file_ptr, "%s", shm_file);
if(shmdt(shm_file) == -1)
{
perror("Error releasing shared memory.");
exit(1);
}
FILE* floppyImage = fopen(shm_file_ptr, "r+");
if (floppyImage == NULL)
{
printf("Could not open the floppy drive or image.\n");
exit(1);
}
BootSector* bootSector = BootSector_ReadBootSector(floppyImage);
BootSector_ToString(bootSector);
return;
}
큰 forker는 아니지만 ... 내 이해는 자식 프로세스에 대해 반환 된 값이 0입니다. 부모에 대해 = 0 ... 각 논리에 대해 두 개의 논리가 있어야합니다. 클라이언트가 메소드를 호출 한 후에 while 루프를 돌기 시작합니다. 맞습니까? 또한 .. '아무 것도 안된다'는 것은 무엇을 의미합니까 ... 가시성을 높이기 위해 printfs를 두었습니까? – forsvarir
@forsvarir 답변으로 작성하십시오. 당신 말이 맞아요. 이 값을 0으로 변경하면 해답을 얻을 수 있습니다. 웹에서 복사하는 것이 얼마나 어리석은 지 ... 나는이 오류를 발견하지 못했을 것입니다. – bleepzter
흠 ... 나는 그것보다 조금 더 있을지도 모른다라고 생각했을 것이다. 그러나 대학 때부터 유닉스에서 공유 메모리를 사용하지 않았다. – forsvarir