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:
- Create Pipe: Use
pipe()
to create the pipe. - Create Child Processes: Create two child processes P1 and P2.
- Send Messages: Each child process writes a message to the pipe: “Child1 is Sending a message!” and “Child2 is Sending a message!”.
- Receive Messages: The parent process reads the messages from the pipe and displays them.
Steps
- Initialize Variables: Define an integer array
fd[2]
to store pipe file descriptors.fd[0]
is the read end andfd[1]
is the write end.
int fd[2]; // Array for pipe file descriptors
- Create Pipe: Use the
pipe()
function to create the pipe.
pipe(fd); // Create the pipe
- 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
}
- 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
- 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
- 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);
- Close Pipe: After the communication, close both ends of the pipe.
close(fd[0]); // Close read end
close(fd[1]); // Close write end
- 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
- Locking Mechanism: Although not used in this experiment, for scenarios involving multiple processes accessing shared resources, locking is crucial to prevent data corruption.
- Error Handling: Adding proper error handling can improve the robustness of the program.
- Efficiency: Consider optimizing the program for faster message transmission.
- Complex Scenarios: Experiment with more processes to explore more complex interprocess communication.
57.53KB
文件大小:
评论区