Lab 5Interprocess Communication with Pipe

Lab 5: Interprocess Communication with Pipe

Objective and Background

This lab aims to help learners understand one of the basic principles of interprocess communication—pipe communication. A pipe is used to connect processes, allowing one process (usually called the producer) to write data and another (the consumer) to read it. Pipes are commonly used for simple data exchange between parent and child processes or between sibling processes.

Lab Requirements and Instructions

This lab requires you to use C to create a program that implements a pipe for communication using the pipe() system call, with the following tasks:

  1. Create Pipe: Use pipe() to create the pipe.
  2. Create Child Processes: Create two child processes P1 and P2.
  3. Send Messages: Each child process writes a message to the pipe: “Child1 is Sending a message!” and “Child2 is Sending a message!”.
  4. Receive Messages: The parent process reads the messages from the pipe and displays them.

Steps

  1. Initialize Variables: Define an integer array fd[2] to store pipe file descriptors. fd[0] is the read end and fd[1] is the write end.
int fd[2]; // Array for pipe file descriptors
  1. Create Pipe: Use the pipe() function to create the pipe.
pipe(fd); // Create the pipe
  1. Create Child Processes: Use fork() to create two child processes, P1 and P2.
pid_t pid1 = fork();
if (pid1 == -1) {
  perror("Fork error");
  exit(1);
} else if (pid1 == 0) {
  // Child Process P1
  // P1 writes to the pipe
}
pid_t pid2 = fork();
if (pid2 == -1) {
  perror("Fork error");
  exit(1);
} else if (pid2 == 0) {
  // Child Process P2
  // P2 writes to the pipe
}
  1. Child Process P1 Sends Message: In P1, write the message to the pipe's write end.
char OutPipe1[100] = "Child1 is Sending a message!";
write(fd[1], OutPipe1, strlen(OutPipe1)); // Write message to pipe
  1. Child Process P2 Sends Message: Similarly, in P2, write the message to the pipe's write end.
char OutPipe2[100] = "Child2 is Sending a message!";
write(fd[1], OutPipe2, strlen(OutPipe2)); // Write message to pipe
  1. Parent Process Receives Messages: The parent reads the messages from the pipe and prints them.
char InPipe[100];
read(fd[0], InPipe, 100); // Read from the pipe
printf("%s
", InPipe);
  1. Close Pipe: After the communication, close both ends of the pipe.
close(fd[0]); // Close read end
close(fd[1]); // Close write end
  1. Resource Cleanup: Free allocated resources and exit.

Analysis and Summary

  • Pipe Creation: The pipe() function creates a two-way communication channel.
  • Parent-Child Communication: The pipe facilitates interprocess communication between parent and child.
  • Message Order: Using time control (e.g., sleep()), you can ensure the messages are received in the correct order by the parent.
  • Resource Management: It’s important to close file descriptors and free resources to avoid leaks.

Experiment Results

After running the program, the following messages should appear:

Child1 is Sending a message!
Child2 is Sending a message!

This confirms that the pipe communication between processes worked correctly.

Further Considerations

  1. Locking Mechanism: Although not used in this experiment, for scenarios involving multiple processes accessing shared resources, locking is crucial to prevent data corruption.
  2. Error Handling: Adding proper error handling can improve the robustness of the program.
  3. Efficiency: Consider optimizing the program for faster message transmission.
  4. Complex Scenarios: Experiment with more processes to explore more complex interprocess communication.
pdf 文件大小:57.53KB