report5
report5
LAB REPORT-5
EXERCISE - 5
VIMALATHITHAN D
03.04.2024 22I272
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int fd[2];
pid_t pid;
char messageIn[100];
if (pipe(fd) < 0) {
perror("pipe error");
exit(EXIT_FAILURE);
pid = fork();
if (pid < 0) {
perror("fork error");
exit(EXIT_FAILURE);
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.
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;
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;
message.msg_type = 1;
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:
- Prompts the user to enter a message, reads it from the standard input, and sends it to the server
using `msgsnd`.
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;
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`.