The following is a detailed analysis of the use of functions such as pthread_create,readlink,getpid, the need for friends can refer to the next
pthread_create is a UNIX environment to create a thread function
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: Returns 0 if successful, or returns an error number
When
returns successfully, the memory unit pointed to by TIDP is set to the thread ID of the newly created thread. The attr parameter is used to develop various threading properties. The newly created thread runs from the address of the START_RTN function, which has only one pointer arg, and if more than one argument is passed to the START_RTN function, the arguments need to be placed in a struct and the address of the struct is passed as an argument to Arg.
Linux under the use of C to develop multithreaded programs, Linux system multithreading following the POSIX thread interface, known as Pthread.
#include <pthread.h>
int pthread_create (pthread_t *restrict TIDP, const pthread_attr_t *restrict attr, void * (*START_RTN) (void), void *restric t arg);
returns:0 if OK, error number on Failure
A pointer decorated by restrict is the first way to access the object that the pointer points to, only when the second pointer is based on the first. Access to an object is limited to a pointer expression that is based on a restrict modification. Pointers decorated by restrict are primarily used for function parameters or point to the memory space allocated by malloc (). The Restrict data type does not change the semantics of the program. The compiler can better optimize certain types of routines by making restrict-modified pointers the only way to access objects.
The first argument is a pointer to the thread identifier.
The second parameter is used to set the thread properties.
The third parameter is the starting address of the thread-running function.
the last parameter is the parameter of the running function.
Additionally, note that the-lpthread parameter is added at compile time to invoke the static link library. Because Pthread is not the default library for Linux systems
===============================================================================linux about Readlink function Gets the run path
related functions: Stat, lstat, Symlink
header file: #include <unistd.h>
defines functions:int Readlink (const char *path, char *buf, size_t bufsiz);
Function Description: readlink () connects the symbol of the parameter path to the memory space referred to by the parameter buf, and the returned content does not end with NULL as a string, but returns the number of characters in the string. If the parameter bufsiz is less than the content length of the symbolic connection, the longer content will be truncated
return value: execution succeeds the file path string that the symbolic connection refers to, failure returns-1, and the error code is stored in errno
Error code:
eaccess is rejected when fetching files, not enough permissions
einval parameter bufsiz is negative
Eio o Access Error
eloop to open the file has too many symbolic connection problems
The path name for the
enametoolong parameter path is too long
enoent parameter path Specifies a file that does not exist
Enomem core memory is low
enotdir parameter path directory exists but is not a true directory
Example One:
#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 obtain process identification code
related functions: fork,kill,getpid header file: #include <unistd.h>
definition function: pid_t getpid (void);
Function Description:
getpid () is used to get the process identification code for the current process, and many programs use this value to create temporary files to avoid the same problems with temporary files.
return value: process Identification code for the current process
Example:
#include <unistd.h>
Main ()
{
printf ("pid=%d/n", Getpid ());
}
Execution:
pid=1494/* Each execution result is not necessarily the same * *
===============================================================================
strrchr () function
definition and usage
The
strrchr () function looks for the last occurrence of a string in another string and returns all characters from this position in the string to the end of the string. If the specified character cannot be found, the function returns NULL.
Grammar
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 is not found/n");
return 0;
}
Run Result: 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 *);
A string to see if there is a B string,
A string is returned from the first discovery of the B string.
NULL is not output
Example:
Char st[]= "ABC 1234 XYZ";
printf ("%s", Strstr (St, "34"));
Print out:
% XYZ