pthread

了解thread


更新記錄

item note
20161206 第一版

目錄


What is a Thread

thread

  • 即共用process的data,text,headp
  • 獨立的stack及register等
  • 使用pthread library來實現

出處:An introduction to UNIX processes

  • process包含下例訊息

    • Process ID, process group ID, user ID, and group ID (process是依使用者有不同的pid)
    • Environment
    • Program instructions (text:存放程式碼)
    • Registers
    • Stack
    • Heap
    • File descriptors
    • Signal actions
    • Shared libraries
    • Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory).
  • Threads use and exist within these process resources

  • This independent flow of control is accomplished because a thread maintains its own:
    • Stack pointer
    • Registers
    • Scheduling properties (such as policy or priority)
    • Set of pending and blocked signals
    • Thread specific data. (ex.Thread ID) [thread_within_a_unix_process]
  • 三種記憶體區間: global、stack、heap
    global: 全域變數、靜態變數(static)
    stack: 區域變數、函式的參數與函式的位址等,由系統管理,必須在編譯時期為已知
    heap: 這裡的記憶體由使用者負責進行回收,配置則是由malloc或是new來負責

example

  • pthread_create函数的详细讲解
    pthred_create取得的ntid(即thread id)
    也可以使用pthread_self
    main及父process,與建立的thread為同1個pid

  • main.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    $ cat main.c 
    #include <pthread.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>

    pthread_t ntid;

    void printids(const char *s){
    pid_t pid;
    pthread_t tid;
    pid = getpid();
    tid = pthread_self();

    printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
    }

    void *thr_fn(void *arg){
    printids("new thread:");
    return ((void *)0);
    }

    int main(void){
    int err;
    err = pthread_create(&ntid,NULL,thr_fn,NULL);

    if( err != 0){
    printf("can't create thread; %s\n",strerror(err));
    }else{
    printf("ntid %u (0x%x)\n",(unsigned int)ntid,(unsigned int)ntid);
    printids("main thread;");
    sleep(1);
    exit(0);
    }
    }
  • a.out

    1
    2
    3
    4
    $ ./a.out 
    ntid 1990027008 (0x769d6700)
    main thread; pid 11778 tid 1998260032 (0x771b0740)
    new thread: pid 11778 tid 1990027008 (0x769d6700)

其它說明

  • Creating and Terminating Threads

    1
    2
    3
    4
    5
    6
    7
    8
    9
    pthread_create (thread,attr,start_routine,arg)

    pthread_exit (status)

    pthread_cancel (thread)

    pthread_attr_init (attr)

    pthread_attr_destroy (attr)
  • pthread_create - create a new thread

    1
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
  • 父process使用pthread_join來等待thread的結束

  • The new thread terminates in one of the following ways

    • pthread_exit
      terminate calling thread

      1
      void pthread_exit(void *retval);
    • pthread_cancel
      send a cancellation request to a thread
      phtread_join收到的exit stats為PTHREAD_CANCELED

      1
      int pthread_cancel(pthread_t thread);
    • pthread_self
      obtain ID of the calling thread

  • pthread_join和pthread_detach的用法
    一般使用pthread_join來等待結束並且將释放所有资源
    也可以使用pthread_detach

  • pthread_detach
    pthread_detach(thread_id)(非阻塞,可立即返回)
    这将该子线程的状态设置为detached,则该线程运行结束后会自动释放所有资源。


Process

unixprocess說明如下

  • What is Process
    UNIX中Pocess代表一個具有獨立記憶空間、可單獨運作的”工作空間”(Program),每一件系統或User的工作均由各種不同的process完成之
  • Why Process
    NIX中,很多resource的管理,必須藉由process的控制來完成UNIX每一個程序都有一個唯一的識別代號,我們稱之為程序代號(process id or pid)
  • How Process Works
    每一process在必要情況之下由其Parent Process產生,完成工作之後會自動釋放所有佔用的系統資源,結束並系開系統。

  • Process的產生
    fork-and-exec是UNIX中所有process運作的模式

  • Process的結束
    Process結束退出系統前,會放棄所有佔用之系統resource,並分別送出訊號通知parent process及child process。
    如果因某些原因,parent process在其結束前即已不存在,此 process即成為所謂的Zombie process(or defunct process),無法正常結束。

  • top

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    Mem: 30584K used, 215460K free, 0K shrd, 0K buff, 5944K cached
    CPU: 0.0% usr 0.0% sys 0.0% nic 100% idle 0.0% io 0.0% irq 0.0% sirq
    Load average: 7.76 2.40 0.83 1/59 912
    PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
    904 1 root S 1440 0.5 1 0.0 -sh
    912 904 root R 1432 0.5 1 0.0 top
    1 0 root S 1428 0.5 0 0.0 init
    653 1 root S 1428 0.5 1 0.0 /usr/sbin/telnetd
    4 2 root SW 0 0.0 0 0.0 [kworker/0:0]
    12 2 root SW 0 0.0 1 0.0 [kworker/1:0]
    10 2 root SW 0 0.0 1 0.0 [migration/1]
    342 2 root SW 0 0.0 1 0.0 [kworker/1:1]
    3 2 root SW 0 0.0 0 0.0 [ksoftirqd/0]
    7 2 root SW 0 0.0 0 0.0 [migration/0]
    2 0 root SW 0 0.0 3 0.0 [kthreadd]
    11 2 root SW 0 0.0 1 0.0 [ksoftirqd/1]
    9 2 root SW 0 0.0 1 0.0 [rcu_sched]
    14 2 root SW 0 0.0 2 0.0 [migration/2]
    15 2 root SW 0 0.0 2 0.0 [ksoftirqd/2]
    16 2 root SW 0 0.0 2 0.0 [kworker/2:0]
    13 2 root SW< 0 0.0 1 0.0 [kworker/1:0H]
    5 2 root SW< 0 0.0 0 0.0 [kworker/0:0H]
    6 2 root SW 0 0.0 0 0.0 [kworker/u8:0]
    20 2 root SW 0 0.0 3 0.0 [kworker/3:0]
  • USER:
    Username of the process owner

  • PID:
    Process ID
  • PPID:
    That’s the parent’s PID.
    Every (well, almost) process has a parent process, the process that was responsible for its creation.
  • STAT: Process state
    R Running
    S Sleeping
    I Idle
    Z Zombie

  • An introduction to UNIX processes ,Summarizing

    • A process is an instance of a running program;
    • Processes have some properties related to it (pid, ppid, tty, etc.);
    • Processes are created in a two step process: exec and fork;
    • Processes always exit with an exit code;
    • A process is a zombie if it is already dead but its parent still didn’t read its exit code with wait;
    • A process is an orphan if it is still alive but its parent isn’t. The init process becomes the new parent;
    • A daemon is a process that runs in the background, and is not attached to a controlling terminal;
    • Signals are messages sent from one process to another.

參考