Untitled
unknown
verilog
16 days ago
1.3 kB
3
Indexable
#include <iostream> #include <sys/ipc.h> #include <sys/shm.h> #include <cstring> struct PersonalData { char name[50]; char fatherName[50]; char cnic[20]; char registration[20]; }; int main() { // Use a hardcoded key instead of ftok() const key_t key = 12345; // Any unique integer // Create shared memory segment int shmid = shmget(key, sizeof(PersonalData), 0666 | IPC_CREAT); if (shmid == -1) { perror("shmget failed"); return 1; } // Attach to shared memory PersonalData* data = (PersonalData*)shmat(shmid, nullptr, 0); if (data == (void*)-1) { perror("shmat failed"); return 1; } // Write data strcpy(data->name, "abc"); strcpy(data->fatherName, "xyz"); strcpy(data->cnic, "42201-1234567-8"); strcpy(data->registration, "2022-CS-123"); std::cout << "Data written to shared memory:\n" << "Name: " << data->name << "\n" << "Father's Name: " << data->fatherName << "\n" << "CNIC: " << data->cnic << "\n" << "Registration: " << data->registration << std::endl; std::cout << "Waiting for receiver (press Ctrl+C to exit)...\n"; while (true) { sleep(1); } return 0; }
Editor is loading...
Leave a Comment