0% found this document useful (0 votes)
18 views

report5

Uploaded by

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

report5

Uploaded by

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

OPERATING SYSTEM

LAB REPORT-5
EXERCISE - 5

VIMALATHITHAN D

03.04.2024 22I272

Aim: To implement interprocess Communication using Pipes, Shared Memory


and Message Queues

1. Demonstrate how two way communications is achieved using pipes implementation.

Code:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main() {

int fd[2];

pid_t pid;

char messageOut[] = "Hello, child process!";

char messageIn[100];

if (pipe(fd) < 0) {

perror("pipe error");

exit(EXIT_FAILURE);

pid = fork();
if (pid < 0) {

perror("fork error");

exit(EXIT_FAILURE);

if (pid > 0) { // Parent process

close(fd[0]); // Close reading end in parent

write(fd[1], messageOut, sizeof(messageOut));

close(fd[1]); // Close writing end in parent

wait(NULL); // Wait for child to finish

} else { // Child process

close(fd[1]); // Close writing end in child

read(fd[0], messageIn, sizeof(messageIn));

printf("Child received: %s\n", messageIn);

close(fd[0]); // Close reading end in child

return 0;

Output:
This program demonstrates inter-process communication using pipes in Unix-like systems. It creates a
pipe, forks a child process, and sends a message from the parent process to the child process through
the pipe. The child process receives the message and prints it. Finally, the program waits for the child
process to finish before exiting.

2. Implement client server communication using Message Queues.

Code:

// Server code

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

struct msg_buffer {

long msg_type;

char msg_text[100];

} message;

int main() {

key_t key;

int msgid;

// Generate unique key

key = ftok("progfile", 65);

// Create message queue

msgid = msgget(key, 0666 | IPC_CREAT);


// Receive message from client

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

// Display the message

printf("Data Received is : %s \n", message.msg_text);

// Remove the message queue

msgctl(msgid, IPC_RMID, NULL);

return 0;

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

struct msg_buffer {

long msg_type;

char msg_text[100];

} message;

int main() {

key_t key;

int msgid;

// Generate unique key

key = ftok("progfile", 65);


// Create message queue

msgid = msgget(key, 0666 | IPC_CREAT);

// Prompt user to enter the message

printf("Enter a message: ");

fgets(message.msg_text, sizeof(message.msg_text), stdin);

message.msg_type = 1;

// Send message to server

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

printf("Data sent is : %s \n", message.msg_text);

return 0;

Output:

1. Server Code:

- Sets up a message queue using `msgget` and waits for a message from a client using `msgrcv`.

- Upon receiving a message, it displays the message content and then removes the message queue
using `msgctl`.
2. Client Code:

- Sets up a message queue using `msgget`.

- Prompts the user to enter a message, reads it from the standard input, and sends it to the server
using `msgsnd`.

- Displays the message sent.

3. Implement IPC using Shared Memory.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h> // To handle errors

int main()
{
void *shared_memory;
char buff[100];
int shmid;

// Create a shared memory segment or attach to an existing one


shmid = shmget((key_t)2345, 1024, 0666|IPC_CREAT);
if (shmid == -1) { // Check if shared memory creation/attachment failed
perror("shmget");
exit(EXIT_FAILURE);
}

printf("Key of shared memory is %d\n", shmid);

// Attach the process to the shared memory segment


shared_memory = shmat(shmid, NULL, 0);
if (shared_memory == (void *)-1) { // Check if attachment failed
perror("shmat");
exit(EXIT_FAILURE);
}

printf("Process attached at %p\n", shared_memory);


printf("Enter some data to write to shared memory\n");
fgets(buff, sizeof(buff), stdin); // Read input from user
strncpy(shared_memory, buff, sizeof(buff)); // Copy data to shared memory

printf("You wrote: %s\n", (char *)shared_memory);

// Detach from the shared memory


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

return 0;
}

Output:

This C program creates or attaches to a shared memory segment identified by the key `2345`. It prompts
the user to input data, copies it into the shared memory, and then prints the input from the shared
memory. Finally, it detaches from the shared memory segment. Error handling is implemented for
shared memory creation, attachment, and detachment using `perror`.

You might also like