In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article to share with you is about how to linuxthreads source code analysis ptfork.c, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after harvest, not much to say, follow Xiaobian to see it.
ptfork.c implements the pthread_atfork function, which is described in pthread_atfork(3) - Linux manual page and in the pthread_atfork.man file in the source code.
/* The "atfork" stuff */
#include
#include
#include
#include "pthread.h"
#include "internals.h"
struct handler_list {
void (*handler)(void);
struct handler_list * next;
};
//Mutex variables used for mutex access lists
static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
//three linked lists
static struct handler_list * pthread_atfork_prepare = NULL;
static struct handler_list * pthread_atfork_parent = NULL;
static struct handler_list * pthread_atfork_child = NULL;
//Create a new handler_list node and insert it into the list
static void pthread_insert_list(struct handler_list ** list,
void (*handler)(void),
struct handler_list * newlist,
int at_end)
{
if (handler == NULL) return;
//insert to the end, then point directly to the tail node first
if (at_end) {
while(*list != NULL) list = &((*list)->next);
}
//Save data to new node
newlist->handler = handler;
// *list is the address of the first node
newlist->next = *list;
// * Change the contents of the list to a new node
*list = newlist;
}
struct handler_list_block {
struct handler_list prepare, parent, child;
};
int pthread_atfork(void (*prepare)(void),
void (*parent)(void),
void (*child)(void))
{
struct handler_list_block * block =
(struct handler_list_block *) malloc(sizeof(struct handler_list_block));
if (block == NULL) return ENOMEM;
pthread_mutex_lock(&pthread_atfork_lock);
/* "prepare" handlers are called in LIFO */
//Save three functions into a node, if this node is inserted into three handle_list queues respectively
pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
/* "parent" handlers are called in FIFO */
pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
/* "child" handlers are called in FIFO */
pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
pthread_mutex_unlock(&pthread_atfork_lock);
return 0;
}
// handle_list Functions for each node in the linked list
static inline void pthread_call_handlers(struct handler_list * list)
{
for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
}
extern int __fork(void);
// http://man7.org/linux/man-pages/man3/pthread_atfork.3.html
/*
Glibc defines the relationship between fork and__fork.
weak_alias (__fork, fork)
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)))
fork is a weak symbol and is an alias for__fork. That is, if fork is defined, it overrides fork in glibc.
This is to override glibc's fork and then perform some extra operations before calling glibc's__fork. This way, when the user executes fork,
The following fork function is executed, thus executing glibc's__fork
*/
int fork(void)
{
int pid;
struct handler_list * prepare, * child, * parent;
pthread_mutex_lock(&pthread_atfork_lock);
prepare = pthread_atfork_prepare;
child = pthread_atfork_child;
parent = pthread_atfork_parent;
pthread_mutex_unlock(&pthread_atfork_lock);
//list of functions called before fork
pthread_call_handlers(prepare);
pid = __fork();
//child process
if (pid == 0) {
__pthread_reset_main_thread();
__fresetlockfiles();
pthread_call_handlers(child);
} else {
//parent process
pthread_call_handlers(parent);
}
return pid;
}
The above is how to carry out linuxthreads source code analysis ptfork.c, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.