pthread_create、readlink、getpid等函数的学习与总结

本文详细介绍了pthread_create函数用于创建线程的使用方法,包括参数解释和实例演示。同时,文章还探讨了readlink函数在获取Linux系统路径方面的应用,提供了具体示例和错误代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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函数获取运行路径            盛世游戏:http://www.shengshiyouxi.com
  相关函数: 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()函数
   
  定义和用法            盛世游戏:http://www.shengshiyouxi.com
  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

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

 

                                       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值