Based on Linux C development in several technical experience summary

  • 2020-04-02 01:04:27
  • OfStack

Recently, I have been working on C development under Linux, because the boss is a message. Therefore, the use of the main technology is a message of the basic background architecture ideas.
In this period of time, learned a lot, and then admire the technology of a message is really great.
Therefore, I feel that I have developed our project from the beginning, and now I have got a lot of things from my great old university.
At present in the game's guild system, task system, mail system, map, mall, and many other large and small systems, are responsible for by me.
Here are a few things I've summarized recently, and more to come
1, time,
Linux systems have a lot of things in terms of time. In the game, time is a very important variable, which involves a series of function points such as time synchronization of front and back end, countdown of the game business, heartbeat, etc
Wait, if you can use the time variable flexibly, at least you should know the following functions or variables

    time_t  

This variable is actually a long, representing the number of seconds from a point in time (typically 0 minutes 0 seconds on January 1, 1970) to that time. Yes, the index is the number of seconds.
There are other functions that take it as an argument in the time.h file

 in time.h In the header file, we can also see some functions that are time_t Function of parameter type or return value type: 

double difftime(time_t time1, time_t time0);
time_t mktime(struct tm * timeptr);
time_t time(time_t * timer);
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);


Let me give you a small example


#include <stdio.h>      
#include <time.h>       
int main ()
{
  time_t timer;
  struct tm y2k;
  double seconds;
  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
  time(&timer);  
  seconds = difftime(timer,mktime(&y2k));
  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
  return 0;
}

Now, just to be more precise
The following two types are also used frequently

struct timeval *a_pstTv, struct timezone *a_pstTz

When we call

gettimeofday(&pstCtx->stCurr, NULL);

Gets the current time
2. Random Numbers
Random Numbers are used a lot in programs, too, to make things seem random.
C currently provides the rand function. So how do we use it

#define RAND1(range)  ((int)((double)(range)*rand()/(RAND_MAX+1.0)))

This macro randomly selects a number between 0 and rang-1. But when we want to use one of them to mess up the order of one of our arrays, we can pick out its index at random, and then use it with some particular one
Elements such as array[0] to swap.
3. Arrays and Pointers
This topic is the permanent topic of C language. With the first and second level Pointers that I use so far, it's not very difficult. To manipulate data by taking the address of specific data memory.
Conversion between arrays and Pointers, etc
In particular, there are three functions to notice right now

void * memcpy ( void * destination, const void * source, size_t num );
void * memmove ( void * destination, const void * source, size_t num );
void * memset ( void * ptr, int value, size_t num );

When using the third three-parameter sizeof, be sure to pay attention to the sizeof the data block and the starting position of the data
warning
Make sure the pointer is not null before using it.
Be aware of array overbounds before using them.
4. Use callbacks wisely
When our system needs to call the same type of function, it is best to use callback function, register, and then according to the command associated function, specific call, similar to c++ polymorphic behavior. Well worth using.
5. Algorithm, algorithm or algorithm.
Usually write code, to think more, more brain feel that this is good to write, use a specific algorithm will be better, will improve efficiency, save space and so on.
6, background procedures remember, the database is the bottleneck, so and the database when the database to use asynchronous ideas, the data to the database, the program should do,
To know if it's going to work, you have to make sure that it's going to come back with a data tag that tells you about the program.
So much for today, so much for today. Keep up the good work.

Related articles: