0% found this document useful (0 votes)
15 views12 pages

Pipes Dynamically

The document contains C code examples demonstrating inter-process communication using pipes, FIFOs, shared memory, and message queues. Each section includes user input handling, process creation with fork, and communication between a parent and child process. Error handling is implemented throughout to manage potential failures in system calls.

Uploaded by

karunakar
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)
15 views12 pages

Pipes Dynamically

The document contains C code examples demonstrating inter-process communication using pipes, FIFOs, shared memory, and message queues. Each section includes user input handling, process creation with fork, and communication between a parent and child process. Error handling is implemented throughout to manage potential failures in system calls.

Uploaded by

karunakar
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/ 12

Pipes dynamically

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <stdlib.h>

#define MAX_MESSAGE_SIZE 100

int main() {

int pipefd[2];

pid_t pid;

char buffer[MAX_MESSAGE_SIZE];

// Create a pipe

if (pipe(pipefd) == -1) {

perror("pipe");

exit(EXIT_FAILURE);

// Dynamically take input from the user

printf("Enter a message to send to the child process: ");

if (fgets(buffer, MAX_MESSAGE_SIZE, stdin) == NULL) {

perror("fgets");

exit(EXIT_FAILURE);

}
// Remove the trailing newline character from fgets input

buffer[strcspn(buffer, "\n")] = 0;

// Create a child process

pid = fork();

if (pid == -1) {

// If fork fails

perror("fork");

exit(EXIT_FAILURE);

if (pid == 0) {

// Child process: Reader

close(pipefd[1]); // Close the write-end of the pipe

read(pipefd[0], buffer, MAX_MESSAGE_SIZE); // Read the message from the pipe

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

close(pipefd[0]);

} else {

// Parent process: Writer

close(pipefd[0]); // Close the read-end of the pipe

write(pipefd[1], buffer, strlen(buffer) + 1); // Write the message to the pipe

printf("Parent (Writer) sent: %s\n", buffer);

close(pipefd[1]);
}

return 0;

Fifo

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#define FIFO_NAME "/tmp/my_fifo"

#define MAX_MESSAGE_SIZE 100

int main() {

pid_t pid;

char buffer[MAX_MESSAGE_SIZE];

int fifo_fd;

// Create a named FIFO (First In First Out)

if (mkfifo(FIFO_NAME, 0666) == -1) {


perror("mkfifo");

exit(EXIT_FAILURE);

// Dynamically take input from the user

printf("Enter a message to send to the child process: ");

if (fgets(buffer, MAX_MESSAGE_SIZE, stdin) == NULL) {

perror("fgets");

exit(EXIT_FAILURE);

// Remove the trailing newline character from fgets input

buffer[strcspn(buffer, "\n")] = 0;

// Create a child process

pid = fork();

if (pid == -1) {

// If fork fails

perror("fork");

exit(EXIT_FAILURE);

if (pid == 0) {

// Child process: Reader


fifo_fd = open(FIFO_NAME, O_RDONLY); // Open the FIFO for reading

if (fifo_fd == -1) {

perror("open");

exit(EXIT_FAILURE);

read(fifo_fd, buffer, MAX_MESSAGE_SIZE); // Read the message from FIFO

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

close(fifo_fd); // Close the FIFO after reading

} else {

// Parent process: Writer

fifo_fd = open(FIFO_NAME, O_WRONLY); // Open the FIFO for writing

if (fifo_fd == -1) {

perror("open");

exit(EXIT_FAILURE);

write(fifo_fd, buffer, strlen(buffer) + 1); // Write the message to FIFO

printf("Parent (Writer) sent: %s\n", buffer);

close(fifo_fd); // Close the FIFO after writing

// Remove the FIFO after use

unlink(FIFO_NAME);
return 0;

Shared memory:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <unistd.h>

#include <sys/wait.h> // Include this header for wait()

#define SHM_SIZE 1024 // Size of shared memory

#define SHM_KEY 12345 // Key for the shared memory segment

int main() {

pid_t pid;

int shm_id;

char *shm_ptr;

char input_str[SHM_SIZE];

// Create shared memory segment

shm_id = shmget(SHM_KEY, SHM_SIZE, 0666 | IPC_CREAT); // create shared memory

if (shm_id == -1) {

perror("shmget failed");

exit(EXIT_FAILURE);
}

// Attach the shared memory segment to the address space of the process

shm_ptr = (char*) shmat(shm_id, NULL, 0);

if (shm_ptr == (char *)-1) {

perror("shmat failed");

exit(EXIT_FAILURE);

// Dynamically take input from the user

printf("Enter a string to share between parent and child: ");

fgets(input_str, sizeof(input_str), stdin);

input_str[strcspn(input_str, "\n")] = 0; // Remove trailing newline from input

// Fork a child process

pid = fork();

if (pid == -1) {

perror("fork failed");

exit(EXIT_FAILURE);

if (pid == 0) {

// Child Process: Reader

// Read the message from shared memory


sleep(1); // Ensure the parent writes first

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

// Detach the shared memory from the child process

if (shmdt(shm_ptr) == -1) {

perror("shmdt failed in child");

exit(EXIT_FAILURE);

} else {

// Parent Process: Writer

// Write the input string to shared memory

strcpy(shm_ptr, input_str);

printf("Parent wrote: %s\n", shm_ptr);

// Wait for the child to finish reading

wait(NULL);

// Detach the shared memory from the parent process

if (shmdt(shm_ptr) == -1) {

perror("shmdt failed in parent");

exit(EXIT_FAILURE);

// Optionally, remove the shared memory segment


if (shmctl(shm_id, IPC_RMID, NULL) == -1) {

perror("shmctl failed");

exit(EXIT_FAILURE);

return 0;

Message queue

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <unistd.h>

#include <sys/wait.h> // Include this header for wait()

#define MSG_KEY 1234 // Message queue key (should be unique)

#define MSG_SIZE 100 // Size of each message

// Message structure

struct msg_buffer {

long msg_type; // Message type

char msg_text[MSG_SIZE]; // Message content


};

int main() {

pid_t pid;

int msgid;

struct msg_buffer message;

// Create a message queue with the given key

msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);

if (msgid == -1) {

perror("msgget failed");

exit(EXIT_FAILURE);

// Dynamically take input from the user for the message

printf("Enter a message to send to the child process: ");

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

message.msg_text[strcspn(message.msg_text, "\n")] = 0; // Remove the trailing newline

message.msg_type = 1; // Message type (can be any positive integer)

// Fork a child process

pid = fork();

if (pid == -1) {

perror("fork failed");
exit(EXIT_FAILURE);

if (pid == 0) {

// Child process: Reader

// Receive the message from the message queue

if (msgrcv(msgid, &message, sizeof(message), message.msg_type, 0) == -1) {

perror("msgrcv failed");

exit(EXIT_FAILURE);

printf("Child received message: %s\n", message.msg_text);

// Remove the message queue

msgctl(msgid, IPC_RMID, NULL);

} else {

// Parent process: Sender

// Send the message to the message queue

if (msgsnd(msgid, &message, sizeof(message), 0) == -1) {

perror("msgsnd failed");

exit(EXIT_FAILURE);

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

// Wait for the child process to finish

wait(NULL);
}

return 0;

You might also like