Untitled

 avatar
unknown
c_cpp
15 days ago
3.3 kB
4
Indexable
tousef najaiz ulad code :

#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

using namespace std;

int main() {
   
    key_t key = ftok("shmfile", 65);


    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);

    char *str = (char*) shmat(shmid, (void*)0, 0);

    printf("Data read from memory: %s\n", str);

    shmdt(str);


    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}






zohaib chutya code:

#include <iostream>
#include <cstring>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_TEXT 100

struct PersonalData {
    long msg_type;          
    char name[50];
    char fatherName[50];
    char cnic[20];
    char registration[20];
};

int main() {
    key_t key = 12345;    
    int msgid;

    
    msgid = msgget(key, 0666 | IPC_CREAT);
    if (msgid == -1) {
        perror("msgget failed");
        return 1;
    }

    PersonalData data;
    data.msg_type = 1;     

   
    std::cout << "Enter your name: ";
    std::cin.getline(data.name, sizeof(data.name));

    std::cout << "Enter father's name: ";
    std::cin.getline(data.fatherName, sizeof(data.fatherName));

    std::cout << "Enter CNIC: ";
    std::cin.getline(data.cnic, sizeof(data.cnic));

    std::cout << "Enter registration number: ";
    std::cin.getline(data.registration, sizeof(data.registration));

   
    if (msgsnd(msgid, &data, sizeof(data) - sizeof(long), 0) == -1) {
        perror("msgsnd failed");
        return 1;
    }

    std::cout << "\nData sent to message queue:\n"
              << "Name: " << data.name << "\n"
              << "Father: " << data.fatherName << "\n"
              << "CNIC: " << data.cnic << "\n"
              << "Reg No: " << data.registration << std::endl;

    return 0;
}




receiver.cpp:
#include <iostream>
#include <cstring>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_TEXT 100

struct PersonalData {
    long msg_type;        
    char name[50];
    char fatherName[50];
    char cnic[20];
    char registration[20];
};

int main() {
    key_t key = 12345;    
    int msgid;

    
    msgid = msgget(key, 0666);
    if (msgid == -1) {
        perror("msgget failed (is sender running?)");
        return 1;
    }

    PersonalData received_data;

    
    if (msgrcv(msgid, &received_data, sizeof(received_data) - sizeof(long), 1, 0) == -1) {
        perror("msgrcv failed");
        return 1;
    }

    
    std::cout << "\nData received from message queue:\n"
              << "Name: " << received_data.name << "\n"
              << "Father: " << received_data.fatherName << "\n"
              << "CNIC: " << received_data.cnic << "\n"
              << "Reg No: " << received_data.registration << "\n";

    
    bool isVerified = true;
    if (strlen(received_data.name) == 0) isVerified = false;
    if (strlen(received_data.fatherName) == 0) isVerified = false;
    if (strlen(received_data.cnic) < 5) isVerified = false;
    if (strlen(received_data.registration) < 3) isVerified = false;

    std::cout << "\nVerification: " << (isVerified ? "SUCCESS" : "FAILED - Missing/Invalid data") << std::endl;

    
    msgctl(msgid, IPC_RMID, nullptr);  

    return 0;
}
Editor is loading...
Leave a Comment