简体   繁体   中英

Child process hangs when using shared memory?

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM