what is causing my bus error: #include <stdio.h> #include <stdlib.h> #include <unistd.h>…
- User
what is causing my bus error: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/types.h> #include <semaphore.h> #include <errno.h> #include #include #include "myShm.h" using namespace std;
int main(int argc, char* argv[]) { cout << "Slave begins execution\n";
int child_number = stoi(argv[1]); const char* shmName = argv[2]; const char* semName = argv[3]; //const int SIZE = 4096; int shm_fd; CLASS* shm_ptr;
// Open the shared memory segment shm_fd = shm_open(shmName, O_CREAT|O_RDWR, 0666); if (shm_fd == -1) { cerr << "Slave: Shared memory failed: " << std::strerror(errno) << "\n"; exit(1); }
// Configue size of shared memory segment ftruncate(shm_fd, sizeof(CLASS));// Map shared memory segment in the address space of the process shm_ptr = (CLASS*)mmap(0, sizeof(CLASS), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (shm_ptr == MAP_FAILED) { cerr << "Slave: Map failed: " << std::strerror(errno) << "\n"; exit(1); }
// Open the semaphore sem_t* mutex_sem = sem_open(semName, 0); if (mutex_sem == SEM_FAILED) { cerr << "Slave: sem_open failed: " << std::strerror(errno) << "\n"; exit(1); }
// Lock mutex_sem for critical section if (sem_wait(mutex_sem) == -1) { cerr << "Slave: sem_wait failed: " << std::strerror(errno) << "\n"; exit(1); }
// Access and modify the shared memory structure int index = shm_ptr->index; cout << "I am child number " << child_number << ", received shared memory name " << shmName << " and " << semName << "\n"; cout << "Child number " << child_number << ": What is my lucky number?\n"; int lucky_number; cin >> lucky_number; shm_ptr->response[index] = child_number; // Write child number to response array shm_ptr->response[index+1] = lucky_number; // Write user's lucky number to response array shm_ptr->index = index + 2; // Update index
// Exit critical section, release mutex_sem if (sem_post(mutex_sem) == -1) { cerr << "Slave: sem_post failed: " << std::strerror(errno) << "\n"; exit(1); }
cout << "I have written my child number to slot " << index << " and my lucky number to slot " << index+1 << ", and updated index to " << index+2 << ".\n"; cout << "Child " << child_number << " closed access to shared memory and terminates.\n";
// Close the semaphore if (sem_close(mutex_sem) == -1) { cerr << "Slave: Failed to close semaphore: " << std::strerror(errno) << "\n"; exit(1); }
return 0; }
- Assistant
There are a few potential causes for a "bus error" in this code:
-
The shared memory segment is not being properly initialized or configured. Ensure that the shared memory segment is created and truncated correctly using the appropriate size.
-
The shared memory segment is not being mapped into the address space of the process correctly. Check if the return value of
mmapis equal toMAP_FAILED, which indicates an error. -
The semaphore is not being opened correctly. Confirm that the
sem_openfunction call succeeds and that the return value is notSEM_FAILED. -
The semaphore is not being locked or unlocked properly. Check that the calls to
sem_waitandsem_postreturn successfully. -
There may be an issue with accessing and modifying the shared memory structure. Make sure that the
shm_ptrpointer is properly initialized and valid. Additionally, ensure that theindexvariable is within the bounds of the shared memory structure.
You should check these potential issues and use debugging techniques to narrow down the exact cause of the bus error.
-