本文整理汇总了C++中pthread_atfork函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_atfork函数的具体用法?C++ pthread_atfork怎么用?C++ pthread_atfork使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_atfork函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: do_test
static int
do_test (void)
{
pid_t pid;
int status = 0;
if (pthread_atfork (prepare1, parent1, child1) != 0)
{
puts ("1st atfork failed");
exit (1);
}
if (pthread_atfork (prepare2, parent2, child2) != 0)
{
puts ("2nd atfork failed");
exit (1);
}
pid = fork ();
if (pid == -1)
{
puts ("fork failed");
exit (1);
}
if (pid != 0)
{
/* Parent. */
if (val != 24)
{
printf ("expected val=%d, got %d\n", 24, val);
exit (1);
}
if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
{
puts ("waitpid failed");
exit (1);
}
}
else
{
/* Child. */
if (val != 80)
{
printf ("expected val=%d, got %d\n", 80, val);
exit (2);
}
}
return status;
}
示例2: main
int main()
{
int err;
pid_t pid;
pthread_t tid;
if((err = pthread_atfork(prepare,parent,child)) != 0)
{
perror("pthread_atfork error");
exit(-1);
}
if((err == pthread_create(&tid, NULL,thr_fn,0))< 0)
{
perror("pthread_create error");
exit(-2);
}
sleep(2);
printf("parent about to fork...\n");
if((pid = fork()) < 0)
{
exit(-3);
}
else if(pid == 0)
{
printf("child return from fork\n");
}
else
{
printf("parent return from fork\n");
}
return 0;
}
示例3: main
int main(int argc, char *argv[])
{
int err;
pid_t pid;
pthread_t tid;
if ((err = pthread_atfork(prepare, parent, child))) {
fprintf(stderr, "pthread_atfork error\n");
exit(2);
}
err = pthread_create(&tid, NULL, thr_fn, NULL);
if (err) {
fprintf(stderr, "pthread_create error\n");
exit(3);
}
sleep(2);
printf("parent about to fork...\n");
if ((pid = fork()) < 0) {
fprintf(stderr, "fork error\n");
exit(4);
} else if (pid == 0) {
printf("child returned from fork\n");
} else {
printf("parent returned from fork\n");
}
exit(0);
}
示例4: slog_open
int slog_open(const char *dir, const char *name, enum slog_level level, uint64_t rotate_size)
{
if (_slog_mkdir_recursive(dir) == -1)
return -1;
char *filename = calloc(1, strlen(dir) + strlen(name) + 2);
sprintf(filename, "%s/%s", dir, name);
int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd < 0) {
free(filename);
return -1;
}
char *lockname = calloc(1, strlen(dir) + strlen(name) + 8);
sprintf(lockname, "%s/.%s.lock", dir, name);
int lockfd = open(lockname, O_RDONLY | O_CREAT, 0644);
if (lockfd < 0) {
free(lockname);
free(filename);
close(fd);
return -1;
}
logger = calloc(1, sizeof(*logger));
logger->fd = fd;
logger->filename = filename;
logger->level = level;
logger->lockfd = lockfd;
logger->lockname = lockname;
logger->rotate_size = rotate_size;
fstat(fd, &logger->fdstat);
pthread_mutex_init(&logger->mutex, NULL);
/* fork是安全的, 子进程会保持mutex无锁, 同时会重新打开lock文件 */
pthread_atfork(_slog_atfork_prepare, _slog_atfork_parent, _slog_atfork_child);
return 0;
}
示例5: uwsgi_python_enable_threads
void uwsgi_python_enable_threads() {
PyEval_InitThreads();
if (pthread_key_create(&up.upt_save_key, NULL)) {
uwsgi_error("pthread_key_create()");
exit(1);
}
if (pthread_key_create(&up.upt_gil_key, NULL)) {
uwsgi_error("pthread_key_create()");
exit(1);
}
pthread_setspecific(up.upt_save_key, (void *) PyThreadState_Get());
pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get());
pthread_mutex_init(&up.lock_pyloaders, NULL);
pthread_atfork(uwsgi_python_pthread_prepare, uwsgi_python_pthread_parent, uwsgi_python_pthread_child);
up.gil_get = gil_real_get;
up.gil_release = gil_real_release;
up.swap_ts = simple_threaded_swap_ts;
up.reset_ts = simple_threaded_reset_ts;
if (uwsgi.threads > 1) {
up.swap_ts = threaded_swap_ts;
up.reset_ts = threaded_reset_ts;
}
uwsgi_log("python threads support enabled\n");
}
示例6: main
int main(void)
{
int err;
pid_t pid;
pthread_t tid;
if((err = pthread_atfork(prepare,parent,child)) != 0){
err_exit(err,"can't install fork handlers");
}
if((err = pthread_create(&tid,NULL,thr_fn,0)) != 0){
err_exit(err,"can't create thread");
}
sleep(2);
printf("parent about to fork...\n");
if((pid = fork()) < 0){
err_quit("fork failed");
}else if(pid == 0){ //child
printf("child return from fork\n");
}else{
printf("parent return from fork\n");
}
exit(0);
}
示例7: _od_xpc_pipe
XPC_RETURNS_RETAINED
static xpc_pipe_t
_od_xpc_pipe(bool resetPipe)
{
static dispatch_once_t once;
xpc_pipe_t result = NULL;
#ifdef __i386__
if (xpc_pipe_create == NULL) {
_si_disable_opendirectory();
return NULL;
}
#endif
dispatch_once(&once, ^(void) {
char *rc_xbs;
/* if this is a build environment we ignore opendirectoryd */
rc_xbs = getenv("RC_XBS");
if ((issetugid() == 0) && (rc_xbs != NULL) && (strcmp(rc_xbs, "YES") == 0)) {
_si_opendirectory_disabled = 1;
return;
}
pthread_atfork(_od_fork_prepare, _od_fork_parent, _od_fork_child);
});
示例8: main
int main( void ) {
int err;
pid_t pid;
pthread_t tid;
#if defined( BSD ) || defined( MACOS )
printf( "pthread atfork is unsupported\n" );
#else
if ( (err = pthread_atfork( prepare, parent, child )) != 0 ) {
err_exit( err, "cannot install fork handlers" );
}
err = pthread_create( &tid, NULL, thread_run, 0 );
if ( err != 0 ) {
err_exit( err, "cannot create thread" );
}
sleep( 2 ); /* TODO: why ? */
printf( "parent about to fork...\n" );
if ( (pid = fork()) < 0 ) {
err_quit( "fork failed" );
} else if ( pid == 0 ) {
/* child is running */
printf( "child returned from fork\n" );
} else {
/* parent is running */
printf( "parent returned from fork\n" );
}
#endif
return 0;
}
示例9: udev_monitor_register
// register a monitor in our list of monitors
// return 0 on success
// return -ENOSPC if we're out of slots
static int udev_monitor_register( struct udev_monitor* monitor ) {
g_monitor_table_spinlock();
// find a free slot
int i = 0;
int rc = -ENOSPC;
for( i = 0; i < UDEV_MAX_MONITORS; i++ ) {
if( g_monitor_table[i] == NULL ) {
g_monitor_table[i] = monitor;
monitor->slot = i;
rc = 0;
break;
}
}
if( g_pid == 0 ) {
// first monitor ever.
// register our fork handler.
g_pid = getpid();
pthread_atfork( NULL, NULL, udev_monitor_atfork );
}
g_monitor_table_unlock();
return rc;
}
示例10: test
/* Test function -- calls pthread_setschedparam() and checks that EINTR is never returned. */
void * test(void * arg)
{
int ret = 0;
/* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */
ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL);
if (ret != 0)
{
UNRESOLVED(ret, "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread");
}
while (do_it)
{
count_ope++;
ret = pthread_atfork(prepare, parent, child);
if (ret == EINTR)
{
FAILED("EINTR was returned");
}
}
return NULL;
}
示例11: InitAtfork
__END_DECLS
// ------------------------------------------------------------------------
static void InitAtfork() {
static pthread_once_t atfork_init = PTHREAD_ONCE_INIT;
pthread_once(&atfork_init, [](){
pthread_atfork(
[](){
if (g_debug != nullptr) {
g_debug->PrepareFork();
}
},
[](){
if (g_debug != nullptr) {
g_debug->PostForkParent();
}
},
[](){
if (g_debug != nullptr) {
g_debug->PostForkChild();
}
}
);
});
}
示例12: s_once_proc
static void
s_once_proc(void) {
int st;
mccp_result_t r;
if ((st = pthread_attr_init(&s_attr)) == 0) {
if ((st = pthread_attr_setdetachstate(&s_attr,
PTHREAD_CREATE_DETACHED)) != 0) {
errno = st;
perror("pthread_attr_setdetachstate");
mccp_exit_fatal("can't initialize detached thread attr.\n");
}
} else {
errno = st;
perror("pthread_attr_init");
mccp_exit_fatal("can't initialize thread attr.\n");
}
if ((r = mccp_hashmap_create(&s_thd_tbl, MCCP_HASHMAP_TYPE_ONE_WORD,
NULL)) != MCCP_RESULT_OK) {
mccp_perror(r, "mccp_hashmap_create()");
mccp_exit_fatal("can't initialize the thread table.\n");
}
if ((r = mccp_hashmap_create(&s_alloc_tbl, MCCP_HASHMAP_TYPE_ONE_WORD,
NULL)) != MCCP_RESULT_OK) {
mccp_perror(r, "mccp_hashmap_create()");
mccp_exit_fatal("can't initialize the thread allocation table.\n");
}
(void)pthread_atfork(NULL, NULL, s_child_at_fork);
}
示例13: init_process
init_process(void)
{
assert(!process_inited);
if (getenv("_RR_CHECK_PRELOAD")) {
/* The tracer parent is just checking that we loaded.
* We did, so return a success code. */
exit(0);
}
real_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock");
buffer_enabled = !!getenv(SYSCALLBUF_ENABLED_ENV_VAR);
if (!buffer_enabled) {
debug("Syscall buffering is disabled");
process_inited = 1;
return;
}
pthread_atfork(NULL, NULL, post_fork_child);
install_syscall_filter();
rrcall_monkeypatch_vdso(&_vsyscall_hook_trampoline);
process_inited = 1;
init_thread();
}
示例14: __attribute__
void __attribute__ ((constructor)) cop_init()
{
if (!cop_process_init_complete) {
cop_init_cops();
/* set default memory pool values */
unsigned long cc_size = 16 * 1024 * 1024;
int cc_bolt = 0;
unsigned long rx_size = 16 * 1024 * 1024;
int rx_bolt = 0;
read_environment_variables(&cc_size, &cc_bolt, &rx_size, &rx_bolt);
mpr_rx_pbic = malloc(sizeof(mapped_memory_pbic_root));
mpr_cc_pbic = malloc(sizeof(mapped_memory_pbic_root));
if (COP_NUMBER_COPS) {
/* check for compatible */
call_cop_get_compatible(COP_SYM_CRYPTO);
cop_init_rx_pbic(mpr_rx_pbic, pool_block_Size64K, rx_size, rx_bolt);
cop_init_cc_pbic(mpr_cc_pbic, pool_block_Size64K, cc_size, cc_bolt);
/* register fork handler */
pthread_atfork(NULL, cop_fork_parent, cop_fork_child);
}
cop_process_init_complete = 1;
}
}
示例15: main
int
main(void)
{
int err;
pid_t pid;
pthread_t tid;
#if defined(BSD) || defined(MACOS)
printf("pthread_atfork is unsupported\n");
#else
if((err = pthread_atfork(prepare,parent,child)) != 0)
err_exit(err,"can't install fork handlers");
err = pthread_create(&tid,NULL,thr_fn,0);
if(err != 0)
err_exit(err,"can't create thread");
sleep(2);
printf("parent about to fork ...\n");
if((pid = fork()) < 0)
err_quit("fork failed");
else if(pid == 0)
printf("child returned from fork\n");
else
printf("parent returned from fork\n");
#endif
exit(0);
}