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

发布于 11-01 15:54 字数 2567 浏览 3 评论 0原文

我遇到一些 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;
}

I am experiencing some pretty weird output from some c code. Granted I am a newbie to c and Linux development, as my background is centered around .NET and C#.

In any case, I was supposed to write a FAT12 implementation and a command shell in c. My shell hangs whenever a child process tries to access shared memory. As a matter of fact nothing happens at all which is really weird. Can anyone help me debug the code?

Thanks,

This is the main loop that runs the 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";
        }

    }

This is a "driver" function that will print the contents of the boot sector (PBS). The problem is that whenever this function executes, nothing happens!

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别2024-11-08 15:54:21

并不是一个真正的大分叉...但我的理解是它返回 = 0 子进程!= 0 父进程...所以你应该有两块逻辑,每种情况一个...就目前情况而言,客户端调用该方法后,它也会开始循环 while 循环,这是正确的吗?另外..“什么也没发生”是什么意思...您是否尝试过使用 printfs 来提高可见性?

not really a big forker... but my understanding was that it returned = 0 for the child process != 0 for the parent... so you should have two lots of logic, one for each case... as it stands, after the client has called the method, it's going to start going round the while loop as well, is that correct? also.. what do you mean by 'nothing' happens... have you tried putting printfs to increase visibility?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文