C extends from shared memory shm to memcache external memory, shmmemcache
Introduction-ipc-shm shared memory
This article will explain how C uses the external memory service memcached through a case study.
We recommend that you refer to the following content to review the linux api of shared memory.
Linux inter-process communication (C): Shared Memory http://blog.chinaunix.net/uid-26000296-id-3421346.html
In the above article, you can take a brief look at the concept. The following article is better. You can take a closer look at the usage of shared memory APIs.
Linux shared memory http://www.cnblogs.com/hicjiajia/archive/2012/05/17/2506632.html (I)
Let's get started. first look at the initial compilation file.Makefile
CC = gcc DEBUG =-ggdb3-WallRUN = $ (CC) $ (DEBUG)-o $ ^ all: shmsrv. out shmclt. outshmsrv. out: shmsrv. c $ (RUN) shmclt. out: shmclt. c $ (RUN) # Delete the make clean operation. PHONY: cleanclean: rm-rf *. I *. s *. o *. out *~ Core _ *; ls-al
First look at the shared memory server, mainly to write content to the shared memory.Shmsrv. c
# Include <stdio. h> # include <errno. h> # include <stdlib. h> # include <string. h> # include <sys/ipc. h> # include <sys/shm. h> // the error message is printed on the console. fmt must be a macro enclosed in double quotation marks # define CERR (fmt ,...) \ fprintf (stderr, "[% s: % d] [error % d: % s]" fmt "\ r \ n", \ _ FILE __, _ func __, _ LINE __, errno, strerror (errno), ##__ VA_ARGS _) // print the error message on the console and exit, t. Similarly, fmt must be a String constant enclosed by "" # define CERR_EXIT (fmt ,...) \ CERR (fmt, ##__ VA_ARGS _), exit (EXIT_FAILURE) // simple detection, exit Unexpectedly # define IF_CHECK (code) \ if (code) <0) \ CERR_EXIT (# code) // shared memory key # define _ INT_SHMKEY (0x12321)/** set a shared memory to write data for others to read. * The written data comes from user input. * start */int main (int argc, char * argv []) {int shmid, I, j; char * shm; // check parameter input if (argc <2) CERR_EXIT ("uage: % s argv [1] [argv [.]. ", argv [0]);/** 0764 indicates the number of octal segments starting with 0, * 7 indicates that the current process has read and write permissions, and * 6 indicates that the session Group has read and write permissions, in the same group, the groupid is the same. * 4 indicates that the other group has the read permission */IF_CHECK (shmid = shmget (_ INT_SHMKEY, BUFSIZ + 1, 0764 | IPC_CREAT )); // Add a simple test printf ("test stdio. h BUFSIZ = % d \ n ", BUFSIZ); // start to share the memory Association shm = shmat (shmid, NULL, 0 ); // write data here for (I = j = 0; I <argc; ++ I) {const char * ts = argv [I]; while (* ts) {shm [j ++] = * ts ++; if (j> = BUFSIZ) break;} if (j> = BUFSIZ) break; shm [j ++] = '';} shm [j] = '\ 0'; // check the shared memory information system (" ipcs-m "); // cancel Association IF_CHECK (shmdt (shm); // Delete shared memory // IF_CHECK (shmctl (shmid, IPC_RMID, NULL); return 0 ;}
It is recommended that you refer to the code above to learn how to use the shared memory api. First, you can create or enable it, bind it later, and then unbind it. This is equivalent to reducing the kernel reference count by one.
It may be worth noting that for the 0764 detailed explanation, This is the Convention, using the eight-digit number, the first number 7 = 4 + 2 + 1.
4 indicates the read permission, 2 indicates the write permission, and 1 indicates the executable permission. You can search for the file permission.
For example File Permission http://blog.chinaunix.net/uid-20864319-id-448817.html
Back
ipcs -m
View the status of all shared memory. You can continue searching for specific operation commands.
For example, ipc command http://www.cnblogs.com/cocowool/archive/2012/05/22/2513027.html
The running result is as follows:
Indicates that the current number of connections is 1. The 8193 permission is 0764, And the name is 0x00012321.
Check that the client only readsShmclt. c
# Include <stdio. h> # include <errno. h> # include <stdlib. h> # include <string. h> # include <sys/ipc. h> # include <sys/shm. h> // the error message is printed on the console. fmt must be a macro enclosed in double quotation marks # define CERR (fmt ,...) \ fprintf (stderr, "[% s: % d] [error % d: % s]" fmt "\ r \ n", \ _ FILE __, _ func __, _ LINE __, errno, strerror (errno), ##__ VA_ARGS _) // print the error message on the console and exit, t. Similarly, fmt must be a String constant enclosed by "" # define CERR_EXIT (fmt ,...) \ CERR (fmt, ##__ VA_ARGS _), exit (EXIT_FAILURE) // simple detection, exit Unexpectedly # define IF_CHECK (code) \ if (code) <0) \ CERR_EXIT (# code) // shared memory key # define _ INT_SHMKEY (0x12321)/** set a shared memory to write data for others to read. * The written data comes from user input. * start */int main (int argc, char * argv []) {int shmid; char * shm; IF_CHECK (shmid = shmget (_ INT_SHMKEY, BUFSIZ + 1, IPC_CREAT); // starts to share the memory Association shm = shmat (shmid, NULL, 0); // outputs the content puts (shm ); // check the shared memory information system ("ipcs-m"); // cancel the association IF_CHECK (shmdt (shm); // Delete the shared memory IF_CHECK (shmctl (shmid, IPC_RMID, NULL); return 0 ;}
The running result is
After the result is printed, ipcs-m cannot find the result.
The command to delete a specific shared memory isIpcrm-m shmid
Others will try more. Shared Memory essentially multiple processes map virtual memory addresses to the same physical memory address.
Preface-install and use memcache
Here we have learned about the basic usage of shared memory, and later we will look at the memcache cache mechanism (external memory). We have the opportunity to study and analyze its source code,
Let's talk about this. memcache is the name of this high-speed cache project. memcached indicates the name of the service that was last started.
The preface mainly describes how to install memcache and the basic protocol commands. The environment is ubuntu 15. 10.
Installation command
sudo apt-get install memcached
Installed and used
ps -ef | grep memcached
After successful installation, the memcached service is started.
You can refer to the memcache command Chinese manual or memcached-h later. For more information about the translation, see the following.
Memcached Chinese manual http://www.jinbuguo.com/man/memcached.html
Later, we began to use memcache. We mainly focused on setting data, updating data, and deleting data.
One operation is as follows:
The following example shows how to add set get.
The first set id 0 0 5 indicates that the key is set to id, the first 0 indicates that the id is unsigned short. The second 0 indicates that there is no expiration time, and 5 indicates that the length of the inserted characters is 5.
The input nihao is the set data.
STORED is returned successfully, and NOT_STORED is returned if the request fails.
Similar to set, add can only succeed if the key is not to be inserted. Otherwise, all operations fail.
For details, see
Memcache telnet http://blog.csdn.net/love__coder/article/details/7828253 Maintenance
For more details, see the following:
Memcache protocol http://www.cnblogs.com/warriorlee/archive/2011/09/18/2180661.html
Memcache brief introduction notes http://blog.sina.com.cn/s/blog_53f716d40100hls0.html
Memcached communication protocol (Chinese) http://www.cnblogs.com/kevintian/articles/1197681.html
There are still a lot of specific setting commands. Here we only list the most common usage. More experience requires trial and error.
Memcache is still very useful. Here the basic protocol of memcache can be used. The following will be officially developed through its driver.
Body-C calls the memcached Service
Memcahce is really good and useful. First of all, let's take a look at the example below. It is still very important here. Please be careful, it is not easy to fully run.
Let's start with it. We have installed the memcached server, but there are only a few client drivers. Otherwise, we cannot call its service for processing.
In Linux, we use the libmemcachde library.
Introducing the C Client Library for memcached http://docs.libmemcached.org/libmemcached.html
Source code installation https://github.com/memcached/memcached/wiki/ReleaseNotes1425
Download and run
tar –xvf libmemcached-1.0.18.tarcd libmemcached-1.0.18
Execution result
The result is as follows:
Here, perform the following steps:
./configuremakesudo make install
After the preceding steps are executed, the installation is successful. You may need to install libevent-dev, sudo apt-get install libevent-dev. install the network library.
After installation, You need to configure the lib environment variable for it (it is very important to understand it as the path on the window)View
The command is as follows:
Cdvi. bashrcShift + Gi # library directory export LD_LIBRARY_PATH =/usr/local/lib: $ LD_LIBRARY_PATHwq added for the memcached client libmemcahced! Source. bashrc
The intermediate command is used to add new environment variables to the last line of the. bashrc file./Usr/local/libIs the library directory installed by the user.
Now everything is ready. First, write a simple demo.Memheoo. cTest
# Include <stdio. h> # include <stdlib. h> # include <libmemcached/memcached. h>/** test memcached usage */int main (int argc, char * argv []) {// connect server memcached_st * memc; memcached_return rc; memcached_server_st * mems; time_t expir = 0; uint32_t flag = 0; const char * res; const char * key = "hello"; size_t klen = strlen (key), vlen; // start the test, you do not need to add the detection code. Here we only want to know that this api memc = memcached_create (NULL); mems = Memcached_server_list_append (NULL, "127.0.0.1", 11211, & rc); if (! Memcached_success (rc) {fprintf (stderr, "An error occurred while adding the server list! \ N "); exit (EXIT_FAILURE);} // currently, the only information about this is libmemcached source code rc = memcached_server_push (memc, mems); if (! Memcached_success (rc) {fprintf (stderr, "An error occurred while adding the server list to the client! "); Exit (EXIT_FAILURE);} memcached_server_list_free (mems); // start to set data rc = memcached_set (memc, key, klen," world ", 5, expir, flag ); if (rc = MEMCACHED_SUCCESS) printf ("Set data
Run the following command to compile the command:
gcc -g -Wall -o memheoo.out memheoo.c -lmemcachedls./memheoo.out
The execution result is as follows:
Okay, here we have all the basic demos completed. All of them are running. It instantly feels a little smoother.
Finally, we construct an interesting time lock through libmemcached. The details are as follows:Memlock. c
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <libmemcached/memcached. h>/** test memcached usage */int main (int argc, char * argv []) {// connect server memcached_st * memc; memcached_return rc; memcached_server_st * mems; time_t expir = 10; // The expiration time is 10 s const char * key = "_ mem_key_lock"; size_t klen = strlen (key ); // create a server address and add it to the client. memc = memcached_create (NULL); mems = memcached_serve R_list_append (NULL, "127.0.0.1", 11211, & rc); rc = memcached_server_push (memc, mems); if (rc! = MEMCACHED_SUCCESS) {fprintf (stderr, "An error occurred while adding the server address! => % S \ n ", memcached_error (memc); exit (EXIT_FAILURE);} memcached_server_list_free (mems); // start to lock rc = memcached_add (memc, key, key, klen, "0", 1, expir, 0); if (rc! = MEMCACHED_SUCCESS) {printf ("the competition lock failed! MEMCACHED_NOTSTORED = % d \ n ", rc = MEMCACHED_NOTSTORED); memcached_free (memc); exit (EXIT_FAILURE);} printf (" Wait here to get the lock Resource: % ld s and end \ n ", expir); // wait for 10 s. You can use another process to test sleep (expir); // free memcached_free (memc); return 0 ;}
Check that the first session process starts the test.
The second session is tested during this period
It is interesting to use the memcached service to build a time-sensitive lock. Here, we can basically understand the memcahed or memory usage.
For advanced Part Scaling, It is optimized and expanded according to the business needs. Every technology is bottomless and best for business needs.
Postscript
It's basically done here. If you have any questions, please contact us and we will quickly correct them ~~