当我从同一个共享内存段读取和写入时,为什么无法获得 sigsegv 或其他东西?正常吗?为什么我没有收到任何错误?
(我知道我不叫 shmclt 或 shmdt)(:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int parent(char*);
int child(char*);
int main ()
{
int shmid = shmget (0xACA0E5,30*sizeof(char),
IPC_CREAT | S_IRUSR | S_IWUSR);
char *shared = (char*) shmat(shmid,0,0);
if (fork())
return parent(shared);
else
return child(shared);
}
int parent (char* shared)
{
while(1)
{
printf("F: %s",shared);
sprintf(shared,"FATHER \t%p\n",shared);
}
}
int child(char* shared)
{
while(1)
{
printf("C: %s",shared);
sprintf(shared,"CHILD \t%p\n",shared);
}
}