当前位置:网站首页>Source code analysis of Tencent libco collaboration open source library (I) -- download libco compilation and installation and try to run the sample code
Source code analysis of Tencent libco collaboration open source library (I) -- download libco compilation and installation and try to run the sample code
2022-06-10 01:10:00 【Love 6】
List of articles
Foreword
ha-ha I didn't expect that before my internship interview And write such a column
I thought it took me more than a day Write Select/Poll/Epoll This kind of source code depth adventure There are enough in the current period of time
little does one think I may spend a day or two looking at the source code of the collaboration library
Of course Here, I'll also explain this in passing Libco The original intention and reason of the source code analysis series
For a month or two Of course, I'm preparing for the interview I have prepared quite a lot Recently, the strategy of writing questions is also very different from the original It turns out that I do it whenever I have a problem And now it becomes to see a problem A problem that can be solved by thinking and thinking about it in your mind I won't spend time typing code
And for My thinking stopped a little Or I feel that there may be some details that I can't grasp in my mind I'll take the time to write it myself
No kidding Thank you very much for more than a year ago I began to form the habit of writing questions very early Otherwise, I now think of algorithm problems in the interview I don't know what to do I think at least before the interview You should brush the title of one year Maybe you can really be more comfortable with the algorithm assessment in the interview If the assessment is too difficult or you have not done the subject before Then you can only listen to fate Look at the status of the scene
I wrote about I/O Multiplexing The source code analysis of the three functions of Tell the truth It really helped me a lot At least on the brush surface Select/Poll/Epoll The difference and principle of And some minor issues It should be said that I can still make do with the answer
Anyway, for the interview It may take another two months or so to prepare carefully Less than 100% prepared The probability is that you won't choose to send your resume
by the way I also forgot to mention why I started this column About Tencent Libco Analysis of the source code of the library
The first is the Tencent is also my favorite internship It is also the company I have been preparing for the interview Of course, for the open source projects written by Tencent itself And it is also a skill point that background development must master You must know something
The second is There are quite a few questions about the cooperation process brushed on the face brush And the questions asked are more detailed During this period of time, I found that The interview is not like what the Internet says A lot of things need to be understood by yourself Some words about this period of time and preparation for the interview will be mentioned in the following chapters
The third is I always thought Before source code No secret This is the concept that I have always advocated For computers Something I always thought I would forget I must have never seen the source code Or just know the surface If you read the source code I believe that during the interview It will be more confident to answer the corresponding questions You can also know clearly by speaking it out Is it right Instead of answering a few words on the Internet
Finally, let's talk about the feelings of preparing for the interview during this period
My first feeling is difficult I also believe Why can't my school produce a few who can enter the top big factories in a year When I am really preparing And after preparing for a period of time, I will go to this stage of delivery I really understand why the interview will brush people
Previous studies It is also aimless Now we have entered the preparatory stage I suddenly found that many of the things I had learned need to be overturned and reread This process has been repeated for a long time
for example Network counting For a lot of things I have learned it all over again But there are still some missing knowledge points I know where the loopholes need to be filled Next time I see such a problem, I'll go back and study carefully Otherwise you won't feel that kind of During the interview, I really asked about the sense of urgency
My second feeling is In the interview Will magnify infinitely what you don't know Or a little bit of knowledge Big factory interview A word I didn't feel It was the original senior student of Tencent who told me A question will be examined during the interview Keep going deep Until you reach the limit of your knowledge I really feel this sentence until now So for all the interview questions I try to figure out everything For the lowest level of knowledge I also try to find out
My third feeling is For a large factory interview Always choose the best from the best Everything I have prepared for the past two years Before I started preparing for the interview I think I have enough confidence to prepare I brushed the buckle 700+ The subject of I have done a small operating system All the subjects I can do done MIT Stanford Jiwang Cameron CSAPP I also learned assembly language in my freshman year Three months ago, I followed muduo Write a highly concurrent network library with all functions
Before preparing for the interview That is, before brushing the surface I think I've done enough As a result, when you brush your face I find Ten questions I can't even answer nine of them But after more than a month of pain This situation is much better At the very beginning Knowledge points of two face classics It seems to me that it took me six days to prepare … What happened later Familiar with similar problems I can handle it
I found that preparing for an interview Just like preparing for the college entrance examination It turned out that senior one and senior two It can also be regarded as laying the foundation The first time in senior three, I took a comprehensive examination of science The effect is not ideal But the farther back The former senior one and senior two students who have been in the lead When you come across a problem that won't happen later Review up The faster it takes to get it back And the students with poor foundation Is about equal to new learning You can imagine the effect
interview Now I feel like Project preparation + Algorithm topic preparation + Basic knowledge preparation + Domain related knowledge reserve + Breadth of knowledge Depth of knowledge
Be short of one cannot For different large factories Of course, the emphasis is different For bytes The algorithm problem seems to be very big Tencent, on the other hand, pays more attention to projects They also pay more attention to basic knowledge But the difficulty of the algorithm is not low
My fourth feeling is Maybe my school It is also true for companies that want to enter in the future It was really bad I saw a lot of things To invest in Tencent Electronics 、 fudan 、 Zhejiang University 、 Peking University 、 tsinghua Harbin Institute of technology is the top school in China I also saw a screenshot of Tianmei internship group I found that there were a lot of Tsinghua University …
I also understand It also needs more efforts No school aura Only by their own hard power can they be recognized
Wrote so much In fact, it means sharing your recent feelings and experiences in preparing for the interview
In the future, we still need to prepare carefully You need to take a deep breath to settle down
Of course I write these blogs For some students You don't have to read my blog every day to study my current situation You can take care of yourself You can't understand the blogs I wrote You don't know There is no need to infer my behavior from my words with the other's heart mind yourself that is ok
Have leisure to pay attention to the author's life It's better to learn your major first Think about how to improve yourself Think about how to clean up your own business Instead of staring at other people's bowls See if there is any meat in it
Don't just talk about others behind your back
After all, the days after that are your own It has nothing to do with me
I'm just a passer-by in your life You are welcome to ask me any questions I try my best to answer But there is no need to pry into other people's lives in this way Pry into other people's situations
There are almost three months left I will be blogging for two years
time flies I have been saying During this period of time, the number of blog posts has become less and less It seems that the number of blog posts has been fixed in 1200 Many articles
If you find an internship later Maybe I will write some interesting projects later For example, learn from Libco The idea of I also write a collaboration library
Finally, I will send a few more College guidance for younger students and younger students How to learn about computer science Take the right path and write a few more Should be my university blog I will say goodbye to myself
Now while I still want to write Just write more
If I can help you It can also be regarded as the meaning of what I have done and written
tencent Libco Collaborative open source library Source code analysis ( One )---- download Libco Compilation and installation Try running the sample code
1、 get Libco Source code
It's a cliche Here's a post Libco Source code link
Tencent/Libco Github link

Old order Let's go to the Linux Input instructions above obtain Libco Folder This part is over git clone [email protected]:Tencent/libco.git

2、 compile Libco Get the sample file
1、cmake compile
Here we follow the cmake Compile the But I use cmake Compilation error Let's introduce it
Get into libco Folder Enter the following instructions
mkdir build
cd build
cmake ..
make
If compiled successfully That's fine But I had a problem compiling cmake Display cannot find a function
So we still use They wrote it by themselves makefile Let's compile

2、makefile compile
We still use others' own writing makefile Compile it
It's simple direct make Just go So there's no mistake

3、 Try running the sample file Take a quick look at the sample code
Because I haven't eaten yet Let's make an introduction here
Take a look at the sample code How does it work libco Of After dinner In the afternoon, I have to take a test on Mao probability …
Second articles When I put some functions and Roughly what functions are used Make it clear Send it again
1、example_echosvr.cpp
The code is below For the parsing of the following code We're in the second When introducing the function, I will talk about it in detail
/* * Tencent is pleased to support the open source community by making Libco available. * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include "co_routine.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <stack>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#ifdef __FreeBSD__
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#endif
using namespace std;
struct task_t
{
stCoRoutine_t *co;
int fd;
};
static stack<task_t*> g_readwrite;
static int g_listen_fd = -1;
static int SetNonBlock(int iSock)
{
int iFlags;
iFlags = fcntl(iSock, F_GETFL, 0);
iFlags |= O_NONBLOCK;
iFlags |= O_NDELAY;
int ret = fcntl(iSock, F_SETFL, iFlags);
return ret;
}
static void *readwrite_routine( void *arg )
{
co_enable_hook_sys();
task_t *co = (task_t*)arg;
char buf[ 1024 * 16 ];
for(;;)
{
if( -1 == co->fd )
{
g_readwrite.push( co );
co_yield_ct();
continue;
}
int fd = co->fd;
co->fd = -1;
for(;;)
{
struct pollfd pf = {
0 };
pf.fd = fd;
pf.events = (POLLIN|POLLERR|POLLHUP);
co_poll( co_get_epoll_ct(),&pf,1,1000);
int ret = read( fd,buf,sizeof(buf) );
if( ret > 0 )
{
ret = write( fd,buf,ret );
}
if( ret > 0 || ( -1 == ret && EAGAIN == errno ) )
{
continue;
}
close( fd );
break;
}
}
return 0;
}
int co_accept(int fd, struct sockaddr *addr, socklen_t *len );
static void *accept_routine( void * )
{
co_enable_hook_sys();
printf("accept_routine\n");
fflush(stdout);
for(;;)
{
//printf("pid %ld g_readwrite.size %ld\n",getpid(),g_readwrite.size());
if( g_readwrite.empty() )
{
printf("empty\n"); //sleep
struct pollfd pf = {
0 };
pf.fd = -1;
poll( &pf,1,1000);
continue;
}
struct sockaddr_in addr; //maybe sockaddr_un;
memset( &addr,0,sizeof(addr) );
socklen_t len = sizeof(addr);
int fd = co_accept(g_listen_fd, (struct sockaddr *)&addr, &len);
if( fd < 0 )
{
struct pollfd pf = {
0 };
pf.fd = g_listen_fd;
pf.events = (POLLIN|POLLERR|POLLHUP);
co_poll( co_get_epoll_ct(),&pf,1,1000 );
continue;
}
if( g_readwrite.empty() )
{
close( fd );
continue;
}
SetNonBlock( fd );
task_t *co = g_readwrite.top();
co->fd = fd;
g_readwrite.pop();
co_resume( co->co );
}
return 0;
}
static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr)
{
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(shPort);
int nIP = 0;
if( !pszIP || '\0' == *pszIP
|| 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0")
|| 0 == strcmp(pszIP,"*")
)
{
nIP = htonl(INADDR_ANY);
}
else
{
nIP = inet_addr(pszIP);
}
addr.sin_addr.s_addr = nIP;
}
static int CreateTcpSocket(const unsigned short shPort /* = 0 */,const char *pszIP /* = "*" */,bool bReuse /* = false */)
{
int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
if( fd >= 0 )
{
if(shPort != 0)
{
if(bReuse)
{
int nReuseAddr = 1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr));
}
struct sockaddr_in addr ;
SetAddr(pszIP,shPort,addr);
int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr));
if( ret != 0)
{
close(fd);
return -1;
}
}
}
return fd;
}
int main(int argc,char *argv[])
{
if(argc<5){
printf("Usage:\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT]\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT] -d # daemonize mode\n");
return -1;
}
const char *ip = argv[1];
int port = atoi( argv[2] );
int cnt = atoi( argv[3] );
int proccnt = atoi( argv[4] );
bool deamonize = argc >= 6 && strcmp(argv[5], "-d") == 0;
g_listen_fd = CreateTcpSocket( port,ip,true );
listen( g_listen_fd,1024 );
if(g_listen_fd==-1){
printf("Port %d is in use\n", port);
return -1;
}
printf("listen %d %s:%d\n",g_listen_fd,ip,port);
SetNonBlock( g_listen_fd );
for(int k=0;k<proccnt;k++)
{
pid_t pid = fork();
if( pid > 0 )
{
continue;
}
else if( pid < 0 )
{
break;
}
for(int i=0;i<cnt;i++)
{
task_t * task = (task_t*)calloc( 1,sizeof(task_t) );
task->fd = -1;
co_create( &(task->co),NULL,readwrite_routine,task );
co_resume( task->co );
}
stCoRoutine_t *accept_co = NULL;
co_create( &accept_co,NULL,accept_routine,0 );
co_resume( accept_co );
co_eventloop( co_get_epoll_ct(),0,0 );
exit(0);
}
if(!deamonize) wait(NULL);
return 0;
}
2、 Try to run example_echosvr
The following runs twice The first time there is no parameter
The second time, the parameter is added
Let's simply say here Parameter meaning IP PORT No introduction TASK_COUNT That is, the number of coroutines in a single process PROCESS_COUNT Is the number of processes

Here is a simple try use nc Have a try See if it works properly
The same as usual Use nc Communicate Found to be working properly

Conclusion
This part is very simple Is simply get Libco The source file
Then we simply ran the sample file Understand the general meaning Then this article is over
about Libco The collaboration process is created co_resume Start the recovery function And the active weight transfer function Let's move on to the next one With an analysis of the sample code to introduce That's all for this one See you next ヾ( ̄▽ ̄)Bye~Bye~
边栏推荐
- 洛谷P1220 关路灯 题解 区间DP
- MySQL -- how to solve the problem of data read consistency
- 数据库之App.config配置文件错误
- Application scheme of residual pressure monitoring system in a high-rise residential building
- Sword finger offer II 014 Anagrams in strings
- 剑指 Offer II 010. 和为 k 的子数组
- Chapter 6 domain controller security
- Mysql——》varchar
- My creation anniversary
- Summary of learning linear programming and dual programming
猜你喜欢

第5章域内横向移动分析及防御

沪漂大专程序员,一边跟刘畊宏健身,一边拿22k的offer

How to optimize slow queries? (actual combat slow query)

分布式数据库下子查询和 Join 等复杂 SQL 如何实现?

My creation anniversary

Hcip day 1 Summary

Pycharm 2022 permanently activated version download, valid for personal test

第6章域控制器安全

内网渗透第4章

Sword finger offer II 017 The shortest string containing all characters
随机推荐
ThingsBoard教程(十九):规则节点概览介绍
剑指 Offer II 012. 左右两边子数组的和相等
Chapter 5 intra domain lateral movement analysis and defense
Sword finger offer II 015 All modifiers in the string
Can the online security of futures account opening be guaranteed? Which futures company is good?
JVM records a CPU surge
剑指 Offer II 018. 有效的回文
MySQL transaction
TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪
Expérience Rip
騰訊Libco協程開源庫 源碼分析(一)---- 下載Libco 編譯安裝 嘗試運行示例代碼
Summary of learning linear programming and dual programming
Sword finger offer II 013 Sum of two dimensional submatrix
The latest version of Alibaba development manual is released in 2022!!!
Sword finger offer II 012 The sum of left and right subarrays is equal
[GoogleCTF2019 Quals]Bnv -S
余压监控系统在某高层住宅的应用方案
Sword finger offer II 011 0 and 1 subarrays with the same number
孙宇晨等收购Poloniex,公链交易所双轮驱动波场生态
JVM——》类文件