zoukankan      html  css  js  c++  java
  • 跟我一起学Linux-线程创建,类似FreeRTOS创建任务

    1、参考学习大神网址:http://blog.csdn.net/ithomer/article/details/6063067

    #include<stdio.h>  
    #include<pthread.h>  
    #include<string.h>  
    #include<sys/types.h>  
    #include<unistd.h>
    
    pthread_t main_tid;
    
    void *func(void *arg)
    {
        while(1)
        {
             printf("子线程:Hello,world!
    ");
             
             sleep(2); 
        }    
    }
    
    int main()  
    {
        int err;
        
        err = pthread_create(&main_tid, NULL, func, NULL); //创建线程  
        
        
        if(err != 0)
        {  
            printf("create thread error: %s/n",strerror(err));  
            return 1;  
        }
        
        printf("main thread: pid: %u tid: %u (0x%x)/n",   
                    (unsigned int)getpid(),  
                    (unsigned int)pthread_self(),  
                    (unsigned int)pthread_self());
    /*     while(1)
        {
             printf("主线程:Hello,world!
    ");
             
             sleep(2); 
        } */    
     
        return 0;  
    }  

    函数一:pthread_create

    函数声明

    int pthread_create(pthread_t *restrict_tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

    返回值

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

    参数

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

    const pthread_attr_t *restrict_attr:    第二个参数用来设置线程属性。

    void*(*start_rtn)(void*):     第三个参数是线程运行函数的起始地址。

    void *restrict arg:        最后一个参数是运行函数的参数。

    另外

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

     

    函数二:strerror(int errnum

    errnum:错误标号,通常用errno(标准错误号,定义在errno.h中)

    通过标准错误的标号,获得错误的描述字符串 ,将单纯的错误标号转为字符串描述,方便用户查找错误。

     

    函数三:getpid()

        函数说明:getpid函数用来取得目前进程的进程ID,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。
        返回值:目前进程的进程ID
     
       函数四:pthread_self函数作用:获得线程自身的ID。
     
         函数五: sleep,类似FreeRTOS的vTaskDelay
      1. sleep的精度是秒  
      2. usleep的精度是微妙,不精确 

    Makefile:编写如下:

      
        #  参考大神博客地址:http://blog.csdn.net/haoel/article/details/2887
        #                       http://www.cnblogs.com/sld666666/archive/2010/04/08/1707789.html
        #  为每一个 *.c文件生成 *o文件。
      #  连接每一个*.o文件,生成可执行文件。
        #  make 过程 如下
        # 1、make会在当前目录下找名字叫“Makefile”或“makefile”的文件。
        # 2、如果找到,它会找文件中的第一个目标文件(target),在上面的例子中,他会找到“edit”这个文件,并把这个文件作为最终的目标文件。
        # 3、如果edit文件不存在,或是edit所依赖的后面的 .o 文件的文件修改时间要比edit这个文件新,那么,他就会执行后面所定义的命令来生成edit这个文件。
        # 4、如果edit所依赖的.o文件也存在,那么make会在当前文件中找目标为.o文件的依赖性,如果找到则再根据那一个规则生成.o文件。(这有点像一个堆栈的过程)
        # 5、当然,你的C文件和H文件是存在的啦,于是make会生成 .o 文件,然后再用 .o 文件生命make的终极任务,也就是执行文件edit了。
    
        
    # 根据Makefile 的过程1。应该第一步:在所在的thread.c 文件路径下,新建一个文件名字为 Makefile”或“makefile”的文件。
    # 根据Makefile 的过程2、3。应该第二步:用 vim 打开 Makefile文件,第一行写代码 edit : thread.o 
    
    edit:thread.o  #可执行文件edit 由 thread.o 组成(对Makefile来说 这个edit 就是最终的目标文件,即执行文件)
        gcc -o edit thread.o # 这句代码的意思是:将thread.o连接成可执行的二进制文件edit 参考GCC语法:http://blog.csdn.net/tomatofly/article/details/6035363
    thread.o:thread.c    # 这句代码的意思是:thread.o依赖关系是 thread.c 和 stdio.h 两个文件,也就是说 如果这两个文件有一个改动的,thread.o就会被重新编译
        gcc -c thread.c  # 这句代码的意思是:只编译thread.c,成功时输出目标文件thread.o 参考GCC语法:http://blog.csdn.net/tomatofly/article/details/6035363

     

  • 相关阅读:
    Maximum Depth of Binary Tree
    Single Number
    Merge Two Sorted Lists
    Remove Nth Node From End of List
    Remove Element
    Remove Duplicates from Sorted List
    Add Two Numbers
    编译视频直播点播平台EasyDSS数据排序使用Go 语言 slice 类型排序的实现介绍
    RTMP协议视频直播点播平台EasyDSS在Linux系统中以服务启动报错can’t evaluate field RootPath in type*struct排查
    【解决方案】5G时代RTMP推流服务器/互联网直播点播平台EasyDSS实现360°全景摄像机VR直播
  • 原文地址:https://www.cnblogs.com/suozhang/p/6594605.html
Copyright © 2011-2022 走看看