gpt4 book ai didi

使用共享内存时子进程挂起?

转载 作者:太空狗 更新时间:2023-10-29 11:05:58 25 4
gpt4 key购买 nike

我从某些 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;
}

最佳答案

不是一个真正的大 fork ......但我的理解是它为子进程返回= 0!= 0为父进程......所以你应该有两套逻辑,每种情况一个......作为它代表,在客户端调用该方法之后,它也将开始循环 while 循环,对吗?还有..你所说的“什么都没发生”是什么意思...你有没有试过用 printfs 来提高可见性?

关于使用共享内存时子进程挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5670827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com