我在嘗試將fork()和exit()函數用於課程作業時遇到了一個問題。問題是我的子進程在錯誤結束時結束。這裏是我的代碼:子進程在出錯時終止
int main(int argc, char** argv)
{
int shmid;
int *shmptr;
int N = 10;
// Create shared memory segment
if ((shmid = shmget(IPC_PRIVATE, N * N * sizeof(int), 0600)) < 0)
{
puts("parent: shmget error");
return -1;
}
// Attach shared memory segment
if ((shmptr = shmat(shmid, 0, 0)) == (void *) -1)
{
puts("parent: shmat error");
return -1;
}
int ret = fork();
if (ret == 0)
{
int status = 0;
if (wait(&status) == -1)
{
puts("parent: Child terminated on error.");
}
else
{
printf("parent: status of child: %d\n", status);
}
puts("parent: End parent.");
}
else
{
puts("child: End child.");
_exit(0);
}
exit(0);
}
我總是得到兒童終止對錯誤警告。有什麼建議麼?