このページを編集する際は、[[編集に関する方針]]に従ってください。 *概要 [#m06a60fa] -対象:8.1.4 -言語:C -宣言:[[postgresql-8.1.4/src/include/storage/pg_shmem.h]] -定義:[[postgresql-8.1.4/src/backend/port/sysv_shmem.c]] ページ名を [[PGSharedMemoryCreate()/ipc_test/postgresql-8.1.4]] に変更しましたので、こちらを参照ください。 -(概要を書いてください。) *引数 [#z9f72095] - Size size -- - bool makePrivate -- - int port -- *実装 [#id801c02] PGShmemHeader * PGSharedMemoryCreate(Size size, bool makePrivate, int port) { -[[PGShmemHeader/postgresql-8.1.4]] -- 下記メンバを持つ構造体型 --int32(signed intの別名)型の変数magic --pid_t型の変数creatorPID --Size(size_tの別名)型の変数totalsize --Size(size_tの別名)型の変数freeoffset --dev_t型の変数device --ino_t型の変数inode -[[Size/postgresql-8.1.4]] -- size_tの別名 -[[bool/postgresql-8.1.4]] -- charの別名 IpcMemoryKey NextShmemSegID; -[[IpcMemoryKey/postgresql-8.1.4]] -- key_tの別名 void *memAddress; PGShmemHeader *hdr; -[[PGShmemHeader/postgresql-8.1.4]] -- 上記参照 IpcMemoryId shmid; -[[IpcMemoryId/postgresql-8.1.4]] -- intの別名 #ifndef WIN32 struct stat statbuf; -stat -- sys/stat.hで定義 #endif /* Room for a header? */ Assert(size > MAXALIGN(sizeof(PGShmemHeader))); -[[Assert()/postgresql-8.1.4]] -- (USE_ASSERT_CHECKINGを指定して生成したバイナリのみ存在)グローバル変数assert_enabledが0以外で、かつ、評価式conditionの結果が0である時、"FailedAssertion"を出力してプログラムを停止する。 -[[MAXALIGN()/postgresql-8.1.4]] -- configureスクリプトによって指定される定数(または定数4)の倍数のうち、LEN以上である最小値を返す。 -[[PGShmemHeader/postgresql-8.1.4]] -- 上記参照 /* Make sure PGSharedMemoryAttach doesn't fail without need */ UsedShmemSegAddr = NULL; -[[UsedShmemSegAddr/postgresql-8.1.4]] -- void型へのポインタ型のグローバル変数(初期値NULL)。 /* Loop till we find a free IPC key */ NextShmemSegID = port * 1000; for (NextShmemSegID++;; NextShmemSegID++) { /* Try to create new segment */ memAddress = InternalIpcMemoryCreate(NextShmemSegID, size); -[[InternalIpcMemoryCreate()/postgresql-8.1.4]] if (memAddress) break; /* successful create and attach */ /* Check shared memory and possibly remove and recreate */ if (makePrivate) /* a standalone backend shouldn't do this */ continue; if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL) continue; /* can't attach, not one of mine */ /* * If I am not the creator and it belongs to an extant process, * continue. */ hdr = (PGShmemHeader *) memAddress; if (hdr->creatorPID != getpid()) { if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH) { shmdt(memAddress); continue; /* segment belongs to a live process */ } } /* * The segment appears to be from a dead Postgres process, or from a * previous cycle of life in this same process. Zap it, if possible. * This probably shouldn't fail, but if it does, assume the segment * belongs to someone else after all, and continue quietly. */ shmdt(memAddress); if (shmctl(shmid, IPC_RMID, NULL) < 0) continue; /* * Now try again to create the segment. */ memAddress = InternalIpcMemoryCreate(NextShmemSegID, size); if (memAddress) break; /* successful create and attach */ /* * Can only get here if some other process managed to create the same * shmem key before we did. Let him have that one, loop around to try * next key. */ } /* * OK, we created a new segment. Mark it as created by this process. The * order of assignments here is critical so that another Postgres process * can't see the header as valid but belonging to an invalid PID! */ hdr = (PGShmemHeader *) memAddress; hdr->creatorPID = getpid(); hdr->magic = PGShmemMagic; #ifndef WIN32 /* Fill in the data directory ID info, too */ if (stat(DataDir, &statbuf) < 0) ereport(FATAL, (errcode_for_file_access(), errmsg("could not stat data directory \"%s\": %m", DataDir))); hdr->device = statbuf.st_dev; hdr->inode = statbuf.st_ino; #endif /* * Initialize space allocation status for segment. */ hdr->totalsize = size; hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader)); /* Save info for possible future use */ UsedShmemSegAddr = memAddress; UsedShmemSegID = (unsigned long) NextShmemSegID; return hdr; } *呼出元 [#c5ae2f5f] -[[main()/ipc_test/postgresql-8.1.4]] *備考 [#o6a10650] /* * PGSharedMemoryCreate * * Create a shared memory segment of the given size and initialize its * standard header. Also, register an on_shmem_exit callback to release * the storage. * * Dead Postgres segments are recycled if found, but we do not fail upon * collision with non-Postgres shmem segments. The idea here is to detect and * re-use keys that may have been assigned by a crashed postmaster or backend. * * makePrivate means to always create a new segment, rather than attach to * or recycle any existing segment. * * The port number is passed for possible use as a key (for SysV, we use * it to generate the starting shmem key). In a standalone backend, * zero will be passed. */ *履歴 [#u09bc16d] -作者:[[testnoda/ページ作者]] -日付:????/?/? |更新日|更新者|更新内容| |||| *コメント [#d631deb0] #comment