Почему я получаю нулевое значение в переменной ptr в конце, когда я компилирую?Я пытаюсь получить время начала после завершения программы.Выходные данные говорят, что «код успешно завершен» или дочерний процесс успешно завершен, но переменная ptr в конце имеет значение null.Кто-нибудь может предположить, почему это так?
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<time.h>
#include<sys/mman.h>
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
int main(){
const char *name = "OS";
long *ptr;
pid_t childPid;
childPid = fork();
int shm;
if(childPid == 0){
char *args[] ={"ls","-l",NULL};
int shmid;
int shsize = 5000000;
key_t key;
char *s;
key = 9876;
if(shmid < 0){
printf("error getting shmid");
exit(1);
}
shm = shm_open(name,O_CREAT| O_RDWR,0666);
if(shm == (char *) -1){
printf("error getting shared memory");
exit(1);}
time_t startTime;
gettimeofday(&startTime,0);
ptr = (long *) mmap(0,sizeof(startTime),PROT_READ | PROT_WRITE,MAP_SHARED,shm,0);
ptr+=startTime;
time_t endTime;
execvp(args[0],args);
printf("successfuly created child proceess");
exit(0);
}
else if (childPid <0){
printf("unsuccessfuly created child proccess");
}
else{
int returnStatus;
waitpid(childPid,&returnStatus,0);
if(returnStatus == 0){
printf("The chiild terminated normally");
printf("%s",ptr);
shm_unlink(name);
}
if(returnStatus == 1){
printf("The child terminated with error");
}
}
}