3
我正在經歷一些c代碼的一些非常奇怪的輸出。當然,我是c和Linux開發的新手,因爲我的背景是圍繞着.NET和C#。使用共享內存時子進程掛起?
在任何情況下,我都應該在c中編寫一個FAT12實現和一個命令shell。每當子進程嘗試訪問共享內存時,我的外殼就會掛起。事實上沒有任何事情發生,這真的很奇怪。任何人都可以幫助我調試代碼?
感謝,
這是一個運行shell的主循環:
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;
}
不是一個真正的大代理......但我的理解是,它的子進程返回= 0,對於父進程,返回0 =所以你應該有兩個邏輯,每個case ......它在客戶端調用方法之後站立,它也將開始繞過while循環,這是否正確?也......你什麼意思的'沒有'發生......你有沒有嘗試把printfs增加可視性? – forsvarir 2011-04-14 23:25:50
@forsvarir請將此作爲回答。你是對的。把它改爲!= 0就是答案......我從網上覆制的多麼愚蠢......我從來沒有發現過這個錯誤。 – bleepzter 2011-04-14 23:30:43
嗯......我原以爲這裏可能會有更多的東西......但是自從大學以來我沒有在unix上使用共享內存...... – forsvarir 2011-04-14 23:35:31