Learning and summarizing functions such as pthread_create, readlink, and getpid

Source: Internet
Author: User

Pthread_create is a thread function created in a Unix environment.

 

Specific format:

# Include <pthread. h>

Int pthread_create (pthread_t * restrict TIDP, const pthread_attr_t * restrict ATTR, void * (* start_rtn) (void *), void * restrict Arg );

Return Value: If successful, 0 is returned; otherwise, an error number is returned.

When a success is returned, the memory unit pointed to by TIDP is set as the thread ID of the newly created thread. The ATTR parameter is used to define various thread attributes. The newly created thread starts running from the address of the start_rtn function. This function has only one no-pointer parameter Arg. If more than one parameter needs to be passed to the start_rtn function, you need to put these parameters in a structure, and then pass the address of this structure as the ARG parameter.

Multi-threaded programs are developed in C in Linux. multithreading in Linux follows the POSIX thread interface, which is called pthread.

 

# Include <pthread. h>

Int pthread_create (pthread_t * restrict TIDP, const pthread_attr_t * restrict ATTR, void * (* start_rtn) (void), void * restrict Arg );

 

Returns: 0 if OK, error number on Failure


The Pointer Modified by restrict is the first and only method to access the object pointed to by the pointer. Only when the second pointer is based on the first one can the object be accessed. Access to objects is limited to pointer expressions modified by restrict. Pointers modified by restrict are mainly used for function parameters or memory space allocated by malloc. The restrict data type does not change the semantics of the program. The compiler can better optimize some types of routines by making the restrict modifier pointer as the assumption that the only method to access the object is used.

The first parameter is the pointer to the thread identifier.

The second parameter is used to set the thread attributes.

The third parameter is the starting address of the thread-running function.

The last parameter is the parameter used to run the function.

In addition, add the-lpthread parameter during compilation to call the static Link Library. Because pthread is not the default library in Linux

 

========================================================== ========================================================

Obtain the running path of the readlink function in Linux

Related functions: stat, lstat, symlink
Header file: # include <unistd. h>
Define the function: int readlink (const char * path, char * Buf, size_t bufsiz );
Function Description: readlink () connects the symbolic content of the path parameter to the memory space referred to by the Buf parameter. The returned content does not end with a null string, but returns the number of characters of the string. If the bufsiz parameter is smaller than the content length of the symbolic connection, the content that is too long will be truncated.

Returned value: if the execution is successful, the path string of the file indicated by the symbolic connection is passed. If the execution fails,-1 is returned. The error code is stored in errno.
Error code:
The eaccess is denied when obtaining the file, and the permission is insufficient.
The value of the einval parameter bufsiz is negative.
EIO o Access Error
The file to be opened by eloop has too many symbolic connections.
The path name of the enametoolong parameter is too long.
The file specified by the enoent parameter path does not exist.
Insufficient enomem core memory
The directory in the path of the enotdir parameter exists but is not a real directory.

Example 1:
# Include <stdio. h>
# Include <unistd. h>
# Define path_max 1024
Char * get_exe_path ()
{
Static char Buf [path_max];
Int I;
Int rslt = readlink ("/proc/self/EXE", Buf, path_max );
If (rslt <0 | rslt> = path_max)
{
Return NULL;
}
Buf [rslt] = '/0 ';
For (I = rslt; I> = 0; I --)
{
Printf ("Buf [% d] % C/N", I, Buf [I]);
If (BUF [I] = '/')
{
Buf [I + 1] = '/0 ';
Break;
}
}
Return Buf;
}

Int main (INT argc, char ** argv)
{
Printf ("% s/n", get_exe_path ());
Return 0;
}

 

========================================================== ========================================================

 

Getpid obtains the process identifier

 

Related functions: fork, kill, getpid header file: # include <unistd. h>

 

Define the function: pid_t getpid (void );

 

Function Description:  

Getpid () is used to obtain the process identifier of the current process. Many programs use this value to create a temporary file to avoid problems caused by the same temporary file.

 

Return Value:Current process identifier

 

Example:

# Include <unistd. h>

Main ()

{

Printf ("pid = % d/N", getpid ());

}

 

Run:  

PID = 1494/* The results of each execution are not necessarily the same */

 

========================================================== ========================================================

 

Strrchr () function

 


Definition and usage

The strrchr () function is used to find the last occurrence position of a string in another string and return all characters starting from this position in the string until the end of the string. If the specified character cannot be found, the function returns NULL.

 

Syntax

Char * strrchr (char * STR, char C );

 

Example

# Include <string. h>

# Include <stdio. h>

Int main (void)

Char string [16];

Char * PTR, c = 'R ';

 

Strcpy (string, "This is a string ");

PTR = strrchr (string, C );

 

If (PTR)

Printf ("the character % C is at position: % d/N", C, PTR-string );

Else

Printf ("the character was not found/N ");

Return 0;

}

 

The running result is: The character R is at position: 12

 

========================================================== ========================================================

 

Strstr () function usage

 

 

C ++ function prototype:
Const char * strstr (const char * str1, const char * str2 );
Char * strstr (char * str1, const char * str2 ); C function prototype:
Char * strstr (const char *, const char *);

Check whether string B exists in string,
If yes, return string a from string B for the first time.
If no value exists, null is output.

Example:
Char st [] = "abc1234 XYZ ";
Printf ("% s", strstr (St, "34 "));Print:
34 XYZ

 

 

========================================================== ========================================================

 

 

 

 

 

 

 

 

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: info-contact@alibabacloud.com 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.