Untitled

 avatar
unknown
plain_text
12 days ago
2.2 kB
3
Indexable
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring>
using namespace std;

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

    // Create shared memory segment
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);

    // Attach to shared memory
    char *str = (char*) shmat(shmid, nullptr, 0);

    cout << "Enter your Name, Father's Name, CNIC, Registration Number (all in one line):" << endl;
    cin.getline(str, 1024);

    cout << "Data written in shared memory: " << str << endl;

    // Detach from shared memory
    shmdt(str);

    return 0;
}







#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring>
using namespace std;

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

    // Get shared memory segment
    int shmid = shmget(key, 1024, 0666);

    // Attach to shared memory
    char *str = (char*) shmat(shmid, nullptr, 0);

    cout << "Data read from shared memory: " << str << endl;

    // Detach and destroy shared memory
    shmdt(str);
    shmctl(shmid, IPC_RMID, nullptr);

    return 0;
}






#include <iostream>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <cstring>
using namespace std;

struct msg_buffer {
    long msg_type;
    char msg_text[100];
};

int main() {
    msg_buffer message;
    message.msg_type = 1;

    key_t key = ftok("progfile", 65);
    int msgid = msgget(key, 0666 | IPC_CREAT);

    cout << "Enter your Name, Father's Name, CNIC, Registration Number:" << endl;
    cin.getline(message.msg_text, sizeof(message.msg_text));

    msgsnd(msgid, &message, sizeof(message.msg_text), 0);

    cout << "Message sent: " << message.msg_text << endl;

    return 0;
}




#include <iostream>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <cstring>
using namespace std;

struct msg_buffer {
    long msg_type;
    char msg_text[100];
};

int main() {
    msg_buffer message;

    key_t key = ftok("progfile", 65);
    int msgid = msgget(key, 0666 | IPC_CREAT);

    msgrcv(msgid, &message, sizeof(message.msg_text), 1, 0);

    cout << "Message received: " << message.msg_text << endl;

    msgctl(msgid, IPC_RMID, nullptr);

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