0% found this document useful (0 votes)
22 views8 pages

OS 9 New

Uploaded by

aamod1916
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

OS 9 New

Uploaded by

aamod1916
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Name Aamod Sardeshpande

UID no. 2022300097

Experiment 1

AIM : Create a Chat Bot using Shared Memory in Linux

In this assignment, you will implement a chat bot using shared memory in Linux. The chat
bot should communicate with the user using the shared memory segment and respond to the
user's messages based on the pre-defined rules.

Here are the steps to follow:


1. Define the message format: Decide on the message format that will be used to
communicate between the chat bot and the user. For example, the message format could be
a string with a pre-defined prefix for the chat bot's response.
2. Create a shared memory segment: Use System V IPC mechanisms to create a shared
memory segment. Use the shmget() function to create the shared memory segment and get a
shared memory identifier.
3. Attach to the shared memory segment: Use the shmat() function to attach to the shared
memory segment and get a pointer to the shared memory segment.
4. Implement the chat bot logic: Write code that listens for messages from the user and
responds appropriately based on pre-defined rules. For example, if the user sends a message
starting with "Hi", the chat bot could respond with "Hello, how can I help you today?".
5. Send the chat bot's response: After the chat bot has generated a response, write the
response to the shared memory segment.
6. Wait for the user's next message: After writing the response to the shared memory
segment, wait for the user to send the next message.
7. Detach from the shared memory segment: Use the shmdt() function to detach from the
shared memory segment.
8. Clean up the shared memory segment: When the chat bot is done, use the shmctl()
function to delete the shared memory segment.

Discussion & Sharedmemory.cpp:-(im using c libraries only i just renamed to cpp incase i
Output: would have to do file operations)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_KEY 1234
#define SHM_SIZE 1024

struct Msg {
int isUserMsg;
char text[256];
};

int main() {
int shmid;
void *shm = (void *)0;
struct Msg *msg;
int msgFlag = 0;

shmid = shmget((key_t)SHM_KEY, SHM_SIZE, 0666 | IPC_CREAT);


if (shmid == -1) {
perror("shmget failed");
exit(EXIT_FAILURE);
}

shm = shmat(shmid, (void *)0, 0);


if (shm == (void *)-1) {
perror("shmat failed");
exit(EXIT_FAILURE);
}

printf("Memory attached at %X\n", (void*)shm);

msg = (struct Msg *)shm;

while(1) {
if (msg->isUserMsg == 1) {
if (strcmp(msg->text, "Hi") == 0) {
strcpy(msg->text, "Hello, how can I help you today?");
} else {
strcpy(msg->text, "Sorry, I didn't understand that.");
}
msg->isUserMsg = 0;
msgFlag = 1;
}

if (msgFlag == 1) {
printf("Bot: %s\n", msg->text);
msgFlag = 0;
sleep(1);
}
}

if (shmdt(shm) == -1) {
perror("shmdt failed");
exit(EXIT_FAILURE);
}

if (shmctl(shmid, IPC_RMID, 0) == -1) {


perror("shmctl(IPC_RMID) failed");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);
}

user .cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHARED_MEMORY_KEY 1234


#define SHARED_MEMORY_SIZE 1024

// message structure
struct Message {
int isUserMessage;
char text[256]; //Max length of the message
};

int main() {
int shmid;
void *shared_memory = (void *)0;
struct Message *message;

// shared memory segment


shmid = shmget((key_t)SHARED_MEMORY_KEY,
SHARED_MEMORY_SIZE, 0666);
if (shmid == -1) {
perror("shmget failed");
exit(EXIT_FAILURE);
}

// Attach to the shared memory segment


shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1) {
perror("shmat failed");
exit(EXIT_FAILURE);
}
printf("Shared memory attached at address: %X\n", (void*)shared_memory);

message = (struct Message *)shared_memory;

while(1) {
// user give input
printf("User: ");
fgets(message->text, sizeof(message->text), stdin);

// Mark the message as from the user


message->isUserMessage = 1;

while (message->isUserMessage == 1) {
sleep(1); // Wait
}

printf("Bot: %s\n", message->text);


}

if (shmdt(shared_memory) == -1) {
perror("shmdt failed");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHARED_MEMORY_KEY 1234


#define SHARED_MEMORY_SIZE 1024

struct Message {
int isUserMessage;
char text[256];
};

int main() {
int shmid;
void *shared_memory = (void *)0;
struct Message *message;

shmid = shmget((key_t)SHARED_MEMORY_KEY,
SHARED_MEMORY_SIZE, 0666);
if (shmid == -1) {
perror("shmget failed");
exit(EXIT_FAILURE);
}

shared_memory = shmat(shmid, (void *)0, 0);


if (shared_memory == (void *)-1) {
perror("shmat failed");
exit(EXIT_FAILURE);
}

printf("Shared memory attached at address: %X\n", (void*)shared_memory);


message = (struct Message *)shared_memory;

while(1) {
printf("User: ");
fgets(message->text, sizeof(message->text), stdin);

message->isUserMessage = 1;

while (message->isUserMessage == 1) {
sleep(1);
}

printf("Bot: %s\n", message->text);


}

if (shmdt(shared_memory) == -1) {
perror("shmdt failed");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);
}
CONCLUSION: Shared Memory implemented in linux .

You might also like