【发布时间】:2011-08-05 23:07:06
【问题描述】:
我从一些 c 代码中遇到了一些非常奇怪的输出。当然,我是 c 和 Linux 开发的新手,因为我的背景以 .NET 和 C# 为中心。
无论如何,我应该用 c 编写一个 FAT12 实现和一个命令 shell。每当子进程试图访问共享内存时,我的 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 为父进程......所以你应该有两个逻辑,每个逻辑一个案例...就目前而言,在客户端调用该方法之后,它也将开始循环 while 循环,对吗?还有..“什么都没有”发生是什么意思...您是否尝试过使用 printfs 来提高知名度?
-
@forsvarir 请写下这个作为答案。你说的对。将其更改为 != 0 就是答案...从网络上复制我真是太愚蠢了...我永远不会发现这个错误。
-
hmmm...我原以为可能会比这更多...但是自从大学以来我就没有在 unix 上使用共享内存...
标签: c linux fork shared-memory