Child process hangs when using shared memory?

Child process hangs when using shared memory? … here is a solution to the problem.

Child process hangs when using shared memory?

I’ve come across some really weird output from some C code. Admittedly, I’m new to C and Linux development because my background is centered around .NET and C#.

Anyway, I should write the FAT12 implementation and command shell in c. Whenever a child process tries to access shared memory, my shell hangs. The fact that nothing happened, it’s really strange. 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 prints 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;
}

Solution

Not really a big fork … But my understanding is that it returns = 0!= 0 for the child process… So you should have two sets of logic, one for each case… As it stands for, after the client calls the method, it will also start looping through the while loop, right? And also.. What do you mean by “nothing happened” … Have you tried printfs to improve visibility?

Related Problems and Solutions