Chương trình tính tổng các số từ 1 đến N .. Quy trình con tính tổng số EVEN. Quy trình gốc tính toán tổng số lẻ. Tôi muốn nhận được giá trị trả lại của quá trình con trong quá trình cha mẹ. Làm thế nào để tôi làm điều đóLàm thế nào để nhận được giá trị trả lại từ TRÌNH TRẺ EM?
#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int N;
int id;
int fd_result;;
printf("Enter N till which you want the sum: \n");
scanf("%d",&N);
if ((fd_result=creat("result", 600))== -1)
{
perror("Error creating file");
exit(1);
}
if ((fd_result=open("result",O_TRUNC | O_RDWR)) == -1)
{
perror("Error Opening file");
exit(1);
}
if ((id=fork())<0)
{
perror("Error occurred forking....!");
exit(errno);
}
if (id == 0)
{
int i;
int sum=0;
for (i=0;i<=N;i=i+2)
sum+=i;
printf("Child sum: %d",sum);
if (write(fd_result,&sum,sizeof(int))==-1) perror("Error writing to file");
_exit(0);
}
if (id > 0)
{
int i;
int sum=0;
int sum_odd;
for (i=1;i<=N;i=i+2)
sum+=i;
lseek(fd_result,0,SEEK_SET);
read(fd_result,&sum_odd,sizeof(int));
printf("\nThe sum is: %d",sum+(int)sum_odd);
}
close(fd_result);
return 0;
}
Xin vui lòng cho tôi biết làm thế nào để có được giá trị trở lại của quá trình con?
Tôi thực sự sử dụng nó như thế nào? Tôi không thể làm được. Trên thực tế, đây là lần đầu tiên tôi viết một chương trình nĩa() ... xin giúp –
Trong cuộc gọi phụ huynh waitpid (-1, & status, 0); –
sau đó, printf ("trạng thái được trả về:% d", WEXITSTATUS (trạng thái)); ... –