首页
关于
Search
1
Lua使用调试库hook函数调用
729 阅读
2
傻瓜式快速搭建l2tp
643 阅读
3
游戏邮件系统数据设计因素
634 阅读
4
傻瓜式安装chatgpt-web工具
586 阅读
5
Linux内核数据结构kfifo小结(TODO)
577 阅读
项目技术
项目思考
开发环境
数据库
编程语言
生活与阅读
哲学
登录
Search
标签搜索
nodejs
npm
资深IT牛马
累计撰写
58
篇文章
累计收到
0
条评论
首页
栏目
项目技术
项目思考
开发环境
数据库
编程语言
生活与阅读
哲学
页面
关于
搜索到
58
篇与
的结果
关于线程栈大小设置实验
每个线程的创建都是有消耗的。这里关注线程栈大小和设置。在Redis的IO线程中也是有设置线程栈为4MB大小的。另外验证的ulimit -s设置时,只能设置为比当前更小,不能扩大。//anker<lichengman2006@gmail.com> @2021-06-20T18:12:11 //gcc thread_stack_size.c -o thread_stack_size -D_GNU_SOURCE -lpthread #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> size_t get_current_thread_stacksize(){ pthread_attr_t attr; size_t stacksize; //pthread_attr_init(&attr); pthread_getattr_np(pthread_self(), &attr); int err = pthread_attr_getstacksize(&attr,&stacksize); if(err != 0){ perror("cannot get thread stack size\n"); exit(err); } pthread_attr_destroy(&attr); return stacksize; } void *thread_entry(void *arg) { char * msg = (char *)arg; size_t stacksize = get_current_thread_stacksize(); printf("get %s stack size(bytes):%ld\n", msg, stacksize); } int main(int argc, char *argv[]){ if(argc != 2){ printf("usage:" "thread_stack_size $sizInMB\n" ); exit(1); } size_t main_stacksize = get_current_thread_stacksize(); printf("get main thread stack size(bytes):%ld\n", main_stacksize); pthread_t threadId; if (pthread_create(&threadId, NULL, thread_entry, "first thread") != 0) { perror("cannot create sub thread"); exit(errno); } pthread_join(threadId, NULL); pthread_attr_t attr; pthread_attr_init(&attr); if ( 0 != pthread_attr_setstacksize(&attr, 1024*1024*atoi(argv[1]))){ //if ( 0 != pthread_attr_setstacksize(&attr, 1024*1024*10)){ perror("set stack size failed."); exit(errno); } pthread_t threadId2; if (pthread_create(&threadId2, &attr, thread_entry, "second thread") != 0) { printf("cannot create sub thread"); exit(errno); } pthread_attr_destroy(&attr); pthread_join(threadId2, NULL); return 0; }pthread_attr_setstacksize函数说明提到:The stack size attribute determines the minimum size (in bytes) that will be allocated for threads created using the thread attributes object attr.他设置的是栈大小的最小值,另外系统也是有限制栈的16KB最小值PTHREAD_STACK_MIN (16384) bytes.> ulimit -s 4096 > ./thread_stack_size 5 get main thread stack size(bytes):4194304 get first thread stack size(bytes):4194304 get second thread stack size(bytes):5242880 > ./thread_stack_size 3 get main thread stack size(bytes):4190208 get first thread stack size(bytes):4194304 get second thread stack size(bytes):4194304留意以下事实:子线程和主线程的栈大小是不一致的。pthread_attr_setstacksize是设置比ulimit还小的值,系统会自动采用最大的最小值。更详细的策略可以参考glibc的allocatestack.c中的allocate_stack函数,其中有对齐等考虑因素。
2021年06月26日
33 阅读
0 评论
0 点赞
僵尸进程实验
僵尸进程产生的原因是当前还没有其他进程来回收他的尸体。每个进程死亡后系统都有部分资源被持有没有被释放。保留的资源有PID和退出码以及CPU时间、内存使用量等子进程一生的信息。如果他的父进程也已经死亡,则会由系统的INIT进程接管收尸。因为进程已经死亡,使用Kill是无法杀死他的。可能通过杀死其父进程来结束。每个进程在死亡时都会通过SIGCHILD信号通知父进程。#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> void signal_handler(int signo){ printf("this is in the sig handler:%d\n", signo); } bool register_signal(int signo){ struct sigaction act; struct sigaction oldact; act.sa_handler = signal_handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; if(sigaction(signo, &act, &oldact) < 0) return false; return true; } int main(){ pid_t pid; pid = fork(); if(pid<0) { /* 如果出错 */ printf("error occurred!\n"); } else if(pid==0) { /* 子进程 */ exit(0); } else { /*进程对SIGCHLD默认动作是忽略,对SIGUSR1,SIGUSR2是终止 * 这里选用kill -SIGUSR1 $parentPID 演示。 * 如果不注册会因为终止进程而无法观察到sleep被中断 * */ register_signal(SIGUSR1); /* 父进程 */ perror("going to sleep\n"); sleep(300); /* 休眠300秒 */ perror("sleep end\n"); wait(NULL); /* 获取僵尸进程的退出信息 */ } return 0; }运行结果:[anker@ms lab]$ ./a.out going to sleep : Success this is in the sig handler:10 sleep end : Interrupted system call
2021年06月26日
21 阅读
0 评论
0 点赞
关于近期博客事件反思
本周服务器故障,导致笔记丢失。回顾事件,有多个原因导致损失。服务器应该保持有开发账号和维护账号,他们进行不同的职责。在开发账号出现问题不能登录时,可以由维护账号来挽救。事故后不应该草率决定重建。应该先确认备份的有效。在重建后再决定是否清理。类似云机器可以先回收机器但保留磁盘,在使用新服务器后挂载旧磁盘。切换环境要慎重。从Centos8切换到Ubuntu20导致环境不熟悉,导致重建各种低效。没有持续有效的备份机制。一直是有想起就备份,但是没想到最近的一次备份还是两年前。针对以上问题:服务器创建两个权限账号。对笔记进行每天定期备份。使用bypy工具,每天凌晨自动打包自动上传百度云盘备份。
2021年06月26日
41 阅读
0 评论
0 点赞
1
...
11
12