gpt4 book ai didi

c - 释放共享内存

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:18 25 4
gpt4 key购买 nike

所以下面的程序访问位于“shmid”位置的一 block 共享内存并将其附加到指针“total”。然后创建一个子进程来访问和修改这 block 内存,一旦完成,就会创建另一个子进程并执行相同的操作,然后是第三个子进程。一旦这3个子进程修改完值并执行完毕,父进程释放共享内存,然后程序退出。我的印象是这个“shmctl” block 会产生预期的效果,但似乎没有。

if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
{
perror ("shmctl");
exit (-1);
}

我不相信它会,因为在这个 block 之后我有以下在执行期间打印出与以前相同的值。这是否表明指针仍在访问相同的内存位,还是我弄错了?

printf("value after memory release:%d\n", total->value);

感谢您的参与!

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>

/* change the key number */
#define SHMKEY ((key_t) 5600) //7890

typedef struct
{
int value;
} shared_mem;

shared_mem *total;

/*----------------------------------------------------------------------*
* This function increases the value of shared variable "total"
* by one all the way to 100000
*----------------------------------------------------------------------*/

void process1 ()
{
int k = 0;

while (k < 100000)
{
k++;
total->value = total->value + 1;
}
printf ("From process1 total = %d\n", total->value); //process 1 prints out total and returns to main() function
}

/*----------------------------------------------------------------------*
* This function increases the vlaue of shared memory variable "total"
* by one all the way to 170000
*----------------------------------------------------------------------*/

void process2 ()
{
int k = 0;

while (k < 170000)
{
k++;
total->value = total->value + 1;
}
printf ("From process2 total = %d\n", total->value); //process 2 prints out total and returns to main() function
}
/*----------------------------------------------------------------------*
* This function increases the vlaue of shared memory variable "total"
* by one all the way to 200000
*----------------------------------------------------------------------*/

void process3 ()
{
int k = 0;

while (k < 200000)
{
k++;
total->value = total->value + 1;
}
printf ("From process3 total = %d\n", total->value); //process 3 prints out total and returns to main() function
}

/*----------------------------------------------------------------------*
* MAIN()
*----------------------------------------------------------------------*/

int main()
{
int shmid;
int pid1;
int pid2;
int pid3;
int ID;
int status;

char *shmadd;
shmadd = (char *) 0;

/* Create and connect to a shared memory segmentt*/

if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0)
{
perror ("shmget");
exit (1);
}


if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1)
{
perror ("shmat");
exit (0);
}

total->value = 0;

if ((pid1 = fork()) == 0) //first child created
{
process1(); //first child process begins
}

while((ID = wait(&status)) != -1) //parent loops until 1st child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished


if ((pid1 != 0) && ((pid2 = fork()) == 0)) //second child created
{
process2(); //second child process begins
}

while((ID = wait(&status)) != -1) //parent loops until 2nd child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished

if ((pid1 != 0) && (pid2 != 0) && ((pid3 = fork()) == 0)) //third child created
{
process3(); //third child process begins
}

while((ID = wait(&status)) != -1) //parent loops until 3rd child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished



if ((pid1 != 0) && (pid2 != 0) && (pid3 != 0))
{
if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
{
perror ("shmctl");
exit (-1);
}

printf ("\t\t End of Program.\n"); //prints after all children have finished

printf("value after memory release:%d\n", total->value);
}
return 0;
}

/***** Note: loop for parent to wait for child processes to finish and print ID of each child*****/

最佳答案

来自 shmctl 手册页:

IPC_RMID

Mark the segment to be destroyed. The segment will only actually be destroyed after the last process detaches it (i.e., when the shm_nattch member of the associated structure shmid_ds is zero). The caller must be the owner or creator, or be privileged. If a segment has been marked for destruction, then the (nonstandard) SHM_DEST flag of the shm_perm.mode field in the associated data structure retrieved by IPC_STAT will be set.

The caller must ensure that a segment is eventually destroyed; otherwise its pages that were faulted in will remain in memory or swap.

即您的代码标记要销毁的段,但在程序退出之前不会将其分离。

如果你想早点分离它,你可以调用shmdt(total);。如果您在最后一个 printf 之前执行此操作,则 printf 可能会导致段错误。

关于c - 释放共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14669662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com