基于pthread_create,readlink,getpid等函数的学习与总结

  pthread_create是UNIX环境创建线程函数

  

  具体格式:

  #include<pthread.h>

  int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void*(*start_rtn)(void*),void *restrict arg);

      返回值:若成功则返回0,否则返回出错编号

  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。

  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为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

  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。

  第一个参数为指向线程标识符的指针。

  第二个参数用来设置线程属性。

  第三个参数是线程运行函数的起始地址。

  最后一个参数是运行函数的参数。

  另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

  ===============================================================================linux关于readlink函数获取运行路径

  相关函数: stat, lstat, symlink

  表头文件: #include <unistd.h>

  定义函数:int  readlink(const  char *path,  char *buf, size_t  bufsiz);

  函数说明:readlink()会将参数path的符号连接内容到参数buf所指的内存空间,返回的内容不是以NULL作字符串结尾,但会将字符串的字符数返回。若参数bufsiz小于符号连接的内容长度,过长的内容会被截断

  

  返回值:执行成功则传符号连接所指的文件路径字符串,失败返回-1, 错误代码存于errno

  错误代码:

  EACCESS                  取文件时被拒绝,权限不够

  EINVAL                    参数bufsiz为负数

  EIO                         O存取错误

  ELOOP                     欲打开的文件有过多符号连接问题

  ENAMETOOLONG       参数path的路径名称太长

  ENOENT                   参数path所指定的文件不存在

  ENOMEM                   核心内存不足

  ENOTDIR                   参数path路径中的目录存在但却非真正的目录

  例一:

  #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 取得进程识别码

  

  相关函数: fork,kill,getpid  表头文件: #include<unistd.h>  

  定义函数: pid_t getpid(void);  

  函数说明:  

  getpid()用来取得目前进程的进程识别码,许多程序利用取到的  此值来建立临时文件,以避免临时文件相同带来的问题。  

  返回值: 目前进程的进程识别码  

  范例:  

  #include<unistd.h>  

  main()  

  {  

  printf(“pid=%d/n”,getpid());  

  }  

  执行:  

  pid=1494 /*每次执行结果都不一定相同*/

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

  

  strrchr()函数

  

  定义和用法

  strrchr()函数的作用是:查找一个字符串在另一个字符串中末次出现的位置,并返回从字符串中的这个位置起, 一直到字符串结束的所有字符。  如果未能找到指定字符,那么函数将返回NULL。

  语法

    char *strrchr(char *str, char c);

  例子

    #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 character r is at position:12

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

  strstr()函数用法

  

  c++函数原型:

  const char * strstr ( const char * str1, const char * str2 );

  char * strstr ( char * str1, const char * str2 );

  C函数原型:

  char * strstr ( const char *, const char * );

  a字符串里 查看是否有b字符串,

  有则 从首次发现b字符串处 返回 a字符串。

  没有则输出 null

  例子:

  char st[]="abc 1234 xyz";

  printf("%s",strstr(st,"34") );

  打印出:

  34 xyz