shm
This commit is contained in:
parent
68da160004
commit
4d6c844438
|
@ -82,6 +82,7 @@ extern "C" {
|
||||||
#include "osLz4.h"
|
#include "osLz4.h"
|
||||||
#include "osMath.h"
|
#include "osMath.h"
|
||||||
#include "osMemory.h"
|
#include "osMemory.h"
|
||||||
|
#include "osProc.h"
|
||||||
#include "osRand.h"
|
#include "osRand.h"
|
||||||
#include "osThread.h"
|
#include "osThread.h"
|
||||||
#include "osSemaphore.h"
|
#include "osSemaphore.h"
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can use, redistribute, and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License, version 3
|
||||||
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _TD_OS_PROC_H_
|
||||||
|
#define _TD_OS_PROC_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// start a copy of itself
|
||||||
|
int32_t taosNewProc(const char *args);
|
||||||
|
|
||||||
|
// the length of the new name must be less than the original name to take effect
|
||||||
|
void taosSetProcName(char **argv, const char *name);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /*_TD_OS_PROC_H_*/
|
|
@ -49,6 +49,8 @@ void taosSetSignal(int32_t signum, FSignalHandler sigfp);
|
||||||
void taosIgnSignal(int32_t signum);
|
void taosIgnSignal(int32_t signum);
|
||||||
void taosDflSignal(int32_t signum);
|
void taosDflSignal(int32_t signum);
|
||||||
|
|
||||||
|
void taosKillChildOnSelfStopped();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -56,7 +56,7 @@ static void dndSetSignalHandle() {
|
||||||
taosSetSignal(SIGCHLD, dndHandleChild);
|
taosSetSignal(SIGCHLD, dndHandleChild);
|
||||||
} else {
|
} else {
|
||||||
// When the parent process exits, the child process will receive the SIGKILL signal
|
// When the parent process exits, the child process will receive the SIGKILL signal
|
||||||
prctl(PR_SET_PDEATHSIG, SIGKILL);
|
taosKillChildOnSelfStopped();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,8 +143,7 @@ static int32_t dndInitLog() {
|
||||||
static void dndSetProcName(char **argv) {
|
static void dndSetProcName(char **argv) {
|
||||||
if (global.ntype != DNODE) {
|
if (global.ntype != DNODE) {
|
||||||
const char *name = dndNodeProcStr(global.ntype);
|
const char *name = dndNodeProcStr(global.ntype);
|
||||||
prctl(PR_SET_NAME, name);
|
taosSetProcName(argv, name);
|
||||||
strcpy(argv[0], name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,7 @@ typedef struct SMgmtWrapper {
|
||||||
bool deployed;
|
bool deployed;
|
||||||
bool required;
|
bool required;
|
||||||
EProcType procType;
|
EProcType procType;
|
||||||
|
int32_t procId;
|
||||||
SProcObj *pProc;
|
SProcObj *pProc;
|
||||||
SShm shm;
|
SShm shm;
|
||||||
void *pMgmt;
|
void *pMgmt;
|
||||||
|
|
|
@ -193,9 +193,17 @@ static int32_t dndRunInParentProcess(SDnode *pDnode) {
|
||||||
if (!pWrapper->required) continue;
|
if (!pWrapper->required) continue;
|
||||||
|
|
||||||
if (pDnode->ntype == NODE_MAX) {
|
if (pDnode->ntype == NODE_MAX) {
|
||||||
dInfo("node:%s, will be started manually", pWrapper->name);
|
dInfo("node:%s, should be started manually", pWrapper->name);
|
||||||
} else {
|
} else {
|
||||||
dInfo("node:%s, will pull up the child process through exec", pWrapper->name);
|
char args[PATH_MAX];
|
||||||
|
int32_t pid = taosNewProc(args);
|
||||||
|
if (pid <= 0) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("node:%s, failed to exec in new process since %s", pWrapper->name, terrstr());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
pWrapper->procId = pid;
|
||||||
|
dInfo("node:%s, run in new process, pid:%d", pWrapper->name, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (taosProcRun(pWrapper->pProc) != 0) {
|
if (taosProcRun(pWrapper->pProc) != 0) {
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can use, redistribute, and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License, version 3
|
||||||
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define ALLOW_FORBID_FUNC
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
|
int32_t taosNewProc(const char *args) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void taosSetProcName(char **argv, const char *name) {
|
||||||
|
prctl(PR_SET_NAME, name);
|
||||||
|
strcpy(argv[0], name);
|
||||||
|
}
|
|
@ -71,4 +71,6 @@ void taosIgnSignal(int32_t signum) { signal(signum, SIG_IGN); }
|
||||||
|
|
||||||
void taosDflSignal(int32_t signum) { signal(signum, SIG_DFL); }
|
void taosDflSignal(int32_t signum) { signal(signum, SIG_DFL); }
|
||||||
|
|
||||||
|
void taosKillChildOnSelfStopped() { prctl(PR_SET_PDEATHSIG, SIGKILL); }
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -26,108 +26,3 @@ class UtilTestQueue : public ::testing::Test {
|
||||||
static void SetUpTestSuite() {}
|
static void SetUpTestSuite() {}
|
||||||
static void TearDownTestSuite() {}
|
static void TearDownTestSuite() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#if 0
|
|
||||||
TEST_F(UtilTestQueue, 01_fork) {
|
|
||||||
pid_t pid;
|
|
||||||
int shmid;
|
|
||||||
int* shmptr;
|
|
||||||
int* tmp;
|
|
||||||
|
|
||||||
int err;
|
|
||||||
pthread_mutexattr_t mattr;
|
|
||||||
if ((err = taosThreadMutexAttrInit(&mattr)) < 0) {
|
|
||||||
printf("mutex addr init error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((err = taosThreadMutexAttrSetPshared(&mattr, PTHREAD_PROCESS_SHARED)) < 0) {
|
|
||||||
printf("mutex addr get shared error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_t* m;
|
|
||||||
int mid = shmget(IPC_PRIVATE, sizeof(pthread_mutex_t), 0600);
|
|
||||||
m = (pthread_mutex_t*)shmat(mid, NULL, 0);
|
|
||||||
|
|
||||||
if ((err = taosThreadMutexInit(m, &mattr)) < 0) {
|
|
||||||
printf("mutex mutex init error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((shmid = shmget(IPC_PRIVATE, 1000, IPC_CREAT | 0600)) < 0) {
|
|
||||||
perror("shmget error");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((shmptr = (int*)shmat(shmid, 0, 0)) == (void*)-1) {
|
|
||||||
perror("shmat error");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = shmptr;
|
|
||||||
|
|
||||||
int shmid2;
|
|
||||||
int** shmptr2;
|
|
||||||
if ((shmid2 = shmget(IPC_PRIVATE, 20, IPC_CREAT | 0600)) < 0) {
|
|
||||||
perror("shmget2 error");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((shmptr2 = (int**)shmat(shmid2, 0, 0)) == (void*)-1) {
|
|
||||||
perror("shmat2 error");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
*shmptr2 = shmptr;
|
|
||||||
|
|
||||||
if ((pid = fork()) < 0) {
|
|
||||||
perror("fork error");
|
|
||||||
exit(1);
|
|
||||||
} else if (pid == 0) {
|
|
||||||
if ((err = taosThreadMutexLock(m)) < 0) {
|
|
||||||
printf("lock error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 30; ++i) {
|
|
||||||
**shmptr2 = i;
|
|
||||||
(*shmptr2)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((err = taosThreadMutexUnlock(m)) < 0) {
|
|
||||||
printf("unlock error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if ((err = taosThreadMutexLock(m)) < 0) {
|
|
||||||
printf("lock error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
for (int i = 10; i < 42; ++i) {
|
|
||||||
**shmptr2 = i;
|
|
||||||
(*shmptr2)++;
|
|
||||||
}
|
|
||||||
if ((err = taosThreadMutexUnlock(m)) < 0) {
|
|
||||||
printf("unlock error:%s\n", strerror(err));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wait(NULL);
|
|
||||||
|
|
||||||
for (int i = 0; i < 70; ++i) {
|
|
||||||
printf("%d ", tmp[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
taosThreadAttrDestroy(&mattr);
|
|
||||||
//销毁mutex
|
|
||||||
taosThreadMutexDestroy(m);
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue