作者:曹忠明,華清遠見嵌入式學院講師。
一、線程式控制制
上一節我們講了使用互斥量實現線程的同步,這裡我們介紹一下另外一種常用的方法,POSIX提供的無名訊號量sem,PV原語是對整數計數器訊號量sem的操作,P操作判斷sem資源數是否為0,不為0則進行P操作,一次P操作可使sem減一,而一次V操作可使sem加一。下面是POSIX提供的一些介面函數:
1、訊號量初始化
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
函數參數:
sem:訊號量
pshared:一個參數一般為0,表示同一個進程中的線程共用一個訊號量。
value:訊號量資源個數
2、其餘函數
int sem_wait (sem_t* sem);
int sem_trywait (sem_t* sem);
int sem_post (sem_t* sem);
int sem_getvalue (sem_t* sem);
int sem_destroy (sem_t* sem);
sem_wait和sem_trywait相當於P操作,它們都能將訊號量的值減一,兩者的區別在於若訊號量的值小於零時,sem_wait將會阻塞進程,而sem_trywait則會立即返回。
sem_post相當於V操作,它將訊號量的值加一,同時發出喚醒的訊號給等待的進程(或線程)。
sem_getvalue 得到訊號量的值。
sem_destroy 摧毀訊號量。
下面用一個常式說明訊號量的使用:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <semaphore.h>
sem_t sem;
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
void *thread_a(void *arg)
{
printf("thread a enter/n");
sem_wait(&sem); //sem - 1
printf("thread a P operation/n");
sleep(10);
sem_post(&sem); //sem + 1
printf("thread a V operation/n");
}
void *thread_b(void *arg)
{
printf("thread b enter/n");
sem_wait(&sem); //sem - 1
printf("thread b P operation/n");
sleep(10);
sem_post(&sem); //sem + 1
printf("thread b V operation/n");
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
sem_init(&sem, 0, 1);
err = pthread_create(&tid_a,NULL,thread_a,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
err = pthread_create(&tid_b,NULL,thread_b,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
sleep(30);
sem_destroy(&sem);
printf("the main close/n");
return 0;
}
二、POSIX tid和linux tid
前面我們說建立線程的時候提到一個函數pthread_self,這個函數使POSIX線程庫中的一個函數,通過這個函數可以獲得線程的ID,可是我們列印出來這個ID會發現這個ID是一個很大的數字。沒有得到我們想象的一個數字,其實這個ID是POSIX線程庫提供的一個數字,而linux核心中也為這個線程提供了一個ID,這個ID可以通過gettid獲得,gettid是linux核心提供的一個系統調用,Glibc沒有封裝函數,只能通過系統調用實現。
POSIX:
#include <pthread>
pthread_t pthread_self(void);
linux系統調用:
#include <sys/types.h>
#include <sys/syscall.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
下面我們通過一個常式看下這兩個函數的區別。
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
void *thread_a(void *arg)
{
printf("thread a enter/n");
pthread_t posix_tid;
pid_t linux_tid;
pid_t pid;
posix_tid = pthread_self();
linux_tid = gettid();
pid = getpid();
printf("pid = %x, posix_tid = %x, linux_tid = %x/n", pid, posix_tid, linux_tid);
}
void *thread_b(void *arg)
{
printf("thread b enter/n");
pthread_t posix_tid;
pid_t linux_tid;
pid_t pid;
posix_tid = pthread_self();
linux_tid = gettid();
pid = getpid();
printf("pid = %x, posix_tid = %x, linux_tid = %x/n", pid, posix_tid, linux_tid);
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
err = pthread_create(&tid_a,NULL,thread_a,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
err = pthread_create(&tid_b,NULL,thread_b,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
sleep(5);
printf("the main close/n");
return 0;
}
程式運行結果:
thread a enter
pid = 3b89, posix_tid = b7fd4b90, linux_tid = 3b8a
thread b enter
pid = 3b89, posix_tid = b75d3b90, linux_tid = 3b8b
the main close
通過這個函數我們可以發現posix提供的這個ID不是很有規律,而linux核心為線程提供的ID是經跟在主進程進程號的數字,如上面程式中主進程ID為3b89而兩個線程的ID分別為3b8a,3b8b。