Simple introduction to common functions of Linux

Source: Internet
Author: User
Tags function prototype posix strtok

Introduction to the MMAP function:

The MMAP function is a system call under Unix/linux, to see the description of mmap in "Unix Netword Programming" Volume II 12.2:
The MMAP function maps either a file or a Posix shared memory object into the address space of a process. We use the This function for three purposes:
1. With a regular file to provide memory-mapped I/O
2. With special files to provide anonymous memory mappings
3. With Shm_open to provide Posix shared memory between unrelated processes

Mmap system calls are not designed entirely for shared memory. It itself provides a different way of accessing ordinary files than normal, and processes can operate on ordinary files like read-write memory. The shared memory IPC for POSIX or System V is purely for sharing purposes, and of course mmap () realizes shared memory is also one of its main applications.
Mmap system calls enable shared memory between processes by mapping the same common file. After the normal file is mapped to the process address space, the process can access the file like normal memory without having to call read (), write (), and so on.

Mmap is used extensively in our programs, and it is the function of mmap to "Access files like normal memory". The practice proves that when a file is accessed frequently and the pointer moves back and forth, calling Mmap is much faster than using the usual method.
Let's look at the definition of mmap:
void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset);

The parameter fd is the file descriptor that will be mapped to the process space, usually returned by open (), and the FD can be specified as-1, at which point the Map_anon in the flags parameter must be specified, indicating that the anonymous mapping is done (not involving the specific file name, avoiding the creation and opening of the file, Obviously, it can only be used for inter-process communication with affinity.

Len is the number of bytes mapped to the calling process address space, starting at offset bytes at the beginning of the mapped file.

The prot parameter specifies the access rights for shared memory. The following values may be desirable or: prot_read (readable), prot_write (writable), prot_exec (executable), Prot_none (inaccessible).

Flags are specified by the following constant values: map_shared, Map_private, map_fixed. Among them, map_shared,map_private must choose one, and map_fixed is not recommended.
If specified as map_shared, modifications made to the mapped memory also affect the file. If it is map_private, modifications made to the mapped memory are only visible to the process and have no effect on the file.

The offset parameter is typically set to 0, which indicates that the mapping starts from the file header.

The addr parameter specifies that the file should be mapped to the start address of the process space and is typically assigned a null pointer, at which point the task of selecting the start address is left to the kernel. The return value of the function is the address that the last file maps to the process space, and the process can manipulate the starting address directly to the valid address of the value.

STRSTR Introduction:

Include file: string.h function Name: strstr function prototype: extern char * strstr ( char *str1,  const char *str2);Grammar: strstr (str1,str2)STR1: The target string expression to search.str2 is searched for: To find the object the string expression to find. Return value: If the str2 is a substring of str1, first determine the first occurrence of str2 in str1 and returns the address of this str1 in the first position of the str2. If STR2 is not a str1 substring, NULL is returned. strcat Introduction:Prototypes:extern Char *strcat (char *dest,char *src);Usage:#include <string.h>; in C + +, it exists in the <cstring> header file.Features:Add the string src refers to at the end of the dest (overwrite ' + ' at the end of the dest) and add '% '.Description:The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string.

Strtok Introduction:

function Prototypes:char *strtok (char *s, const char *delim);
Function: decomposes A string into a set of strings. S is the string to be decomposed, Delim is the delimiter string.
Description: Strtok () is used to split a string into fragments. The parameter S points to the string to be split, the parameter Delim is the split string, and when Strtok () discovers the split character of the parameter Delim in the string of the parameter s, the character is changed to the \ = character. At the first call, Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. Each successful call returns a pointer to the fragment being split.

/* The first Call of the function requires two parameters to be set. The result of the first split, returns the string before the first ', ', which is the first output ABC of the above program.
* The second time the function is called strtok (null, ","), the first parameter is set to NULL. The result is a string followed by the split, that is, the second output d.
* Strtok is a thread unsafe function because it uses statically allocated space to store the segmented string position
* Thread-safe function called Strtok_r,ca
* When using strtok to determine IP or Mac, be sure to use other methods to determine the '. ' or ': ' of the number,
* Because with strtok truncation, for example: "192..168.0...8 ..." This string, Strtok will only intercept four times, the middle of the ... No matter how many will be treated as a key

Simple introduction to common functions of Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.