Learning and summarizing functions based on 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

========================================================== ========================================================== Linux aboutReadlink function to obtain the running path
Related functions:Stat, lstat, symlink
Header file:# Include <unistd. h>
Define functions: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 NULL as a 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.

Return Value:
If the execution is successful, the path string of the file indicated by the symbolic connection is passed.-1 is returned if 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 );
If (buf = '/')
{
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, getpidHeader file:# Include <unistd. h>

Define functions: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.