如何通过return语句实现子进程向父进程返回值?
Hey there! I totally get why you're stuck—most tech posts jump into different IPC (Inter-Process Communication) methods without breaking down the return/exit() approach specifically for simple math assignments like yours. Let's walk through this step by step, with concrete examples.
First, the Key Limitation to Know
A child process can only return an 8-bit integer (0-255) via return (or exit()). This is a system-level restriction—any value outside this range gets truncated (e.g., returning 300 will give the parent 44, since 300 % 256 = 44). If your math operations stay within 0-255, the return method works perfectly. For larger results, we'll cover a backup IPC method too.
Example: Child Reads Parent Data & Returns Addition Result
Let's use C (a common language for this type of assignment) to demonstrate:
- Parent creates a child with
fork()—the child inherits a copy of the parent's variables. - Child uses those inherited values to do the math.
- Child returns the result via
return. - Parent waits for the child to finish, then extracts the return value.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { // Parent's data: two numbers to add int num1 = 12, num2 = 42; pid_t child_pid = fork(); if (child_pid == -1) { perror("Failed to create child process"); exit(EXIT_FAILURE); } else if (child_pid == 0) { // ---------------------- // Child process code // ---------------------- // Read parent's data (inherited via fork) int sum = num1 + num2; printf("Child: Calculated sum = %d\n", sum); // Return the result to parent return sum; // Equivalent to: exit(sum); } else { // ---------------------- // Parent process code // ---------------------- int child_status; // Wait for child to finish and capture its exit status waitpid(child_pid, &child_status, 0); if (WIFEXITED(child_status)) { // Extract the return value from the status code int child_result = WEXITSTATUS(child_status); printf("Parent: Received result from child = %d\n", child_result); } } return 0; }
What If Your Result Is Outside 0-255?
If you're adding larger numbers (e.g., 200 + 200 = 400), the return method won't work. Instead, use a pipe to send the result back—this works for any integer size or even complex data.
Here's a quick pipe example:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { int parent_to_child[2]; // Pipe for parent to send data to child int child_to_parent[2]; // Pipe for child to send result back int num1 = 200, num2 = 200; int result; // Create both pipes if (pipe(parent_to_child) == -1 || pipe(child_to_parent) == -1) { perror("Pipe creation failed"); exit(EXIT_FAILURE); } pid_t child_pid = fork(); if (child_pid == -1) { perror("Fork failed"); exit(EXIT_FAILURE); } else if (child_pid == 0) { // Child: Close unused pipe ends close(parent_to_child[1]); close(child_to_parent[0]); // Read numbers from parent int nums[2]; read(parent_to_child[0], nums, sizeof(nums)); close(parent_to_child[0]); // Calculate sum result = nums[0] + nums[1]; printf("Child: Calculated sum = %d\n", result); // Send result back to parent write(child_to_parent[1], &result, sizeof(result)); close(child_to_parent[1]); exit(EXIT_SUCCESS); } else { // Parent: Close unused pipe ends close(parent_to_child[0]); close(child_to_parent[1]); // Send numbers to child int nums[] = {num1, num2}; write(parent_to_child[1], nums, sizeof(nums)); close(parent_to_child[1]); // Wait for child and read result wait(NULL); read(child_to_parent[0], &result, sizeof(result)); close(child_to_parent[0]); printf("Parent: Received result from child = %d\n", result); } return 0; }
Quick Notes for Your Assignment
- When you
fork()a child, it gets a copy of the parent's entire memory space—so any variables set beforefork()are accessible to the child (that's how it "reads parent process information"). - If you need the parent to update data after
fork()and have the child see it, you'll need shared memory instead of inherited copies (since copies are independent afterfork()).
内容的提问来源于stack exchange,提问作者Jonathan A Vazquez

