2
Значение status
не возвращается корректно от дочернего элемента к родительскому процессу.Возвращаемое значение из дочернего процесса в родительский процесс с помощью exit() и wait() в C
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
#define BUF_SIZE 200
int main(void){
pid_t pid;
int status=6;
char buf[BUF_SIZE];
pid=fork();
if(pid){
sprintf(buf,"Value in parent process is %d\n",status);
write(1,buf,strlen(buf));
wait(&status);
sprintf(buf,"Value returned from child process is %d\n",status);
write(1,buf,strlen(buf));
}
else if(pid==0){
status++;
sprintf(buf,"Returning %d..\n",status);
write(1,buf,strlen(buf));
exit(status);
}
return 0;
}
Выходной код:
Value in parent process is 6
Returning 7..
Value returned from child process is 1792
Где 1792
приходит? Почему это значение не 7
?