日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 工業(yè)控制 > 電子設(shè)計(jì)自動(dòng)化
[導(dǎo)讀]作者:曹忠明,華清遠(yuǎn)見(jiàn)嵌入式學(xué)院講師。 一、線(xiàn)程控制 上一節(jié)我們講了使用互斥量實(shí)現(xiàn)線(xiàn)程的同步,這里我們介紹一下另外一種常用的方法,POSIX提供的無(wú)名信號(hào)量sem,PV原語(yǔ)是對(duì)整數(shù)計(jì)數(shù)器信號(hào)量sem的操作,P操作判斷s

作者:曹忠明,華清遠(yuǎn)見(jiàn)嵌入式學(xué)院講師。

一、線(xiàn)程控制

上一節(jié)我們講了使用互斥量實(shí)現(xiàn)線(xiàn)程的同步,這里我們介紹一下另外一種常用的方法,POSIX提供的無(wú)名信號(hào)量sem,PV原語(yǔ)是對(duì)整數(shù)計(jì)數(shù)器信號(hào)量sem的操作,P操作判斷sem資源數(shù)是否為0,不為0則進(jìn)行P操作,一次P操作可使sem減一,而一次V操作可使sem加一。下面是POSIX提供的一些接口函數(shù):

1、信號(hào)量初始化

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

函數(shù)參數(shù):

sem:信號(hào)量

pshared:一個(gè)參數(shù)一般為0,表示同一個(gè)進(jìn)程中的線(xiàn)程共享一個(gè)信號(hào)量。

value:信號(hào)量資源個(gè)數(shù)

2、其余函數(shù)

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相當(dāng)于P操作,它們都能將信號(hào)量的值減一,兩者的區(qū)別在于若信號(hào)量的值小于零時(shí),sem_wait將會(huì)阻塞進(jìn)程,而sem_trywait則會(huì)立即返回。

sem_post相當(dāng)于V操作,它將信號(hào)量的值加一,同時(shí)發(fā)出喚醒的信號(hào)給等待的進(jìn)程(或線(xiàn)程)。

sem_getvalue 得到信號(hào)量的值。

sem_destroy 摧毀信號(hào)量。

下面用一個(gè)例程說(shuō)明信號(hào)量的使用:

#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 entern");

sem_wait(&sem); //sem - 1

printf("thread a P operationn");

sleep(10);

sem_post(&sem); //sem + 1

printf("thread a V operationn");

}

void *thread_b(void *arg)

{

printf("thread b entern");

sem_wait(&sem); //sem - 1

printf("thread b P operationn");

sleep(10);

sem_post(&sem); //sem + 1

printf("thread b V operationn");

}

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 closen");

return 0;

}

二、POSIX tid和linux tid

前面我們說(shuō)創(chuàng)建線(xiàn)程的時(shí)候提到一個(gè)函數(shù)pthread_self,這個(gè)函數(shù)使POSIX線(xiàn)程庫(kù)中的一個(gè)函數(shù),通過(guò)這個(gè)函數(shù)可以獲得線(xiàn)程的ID,可是我們打印出來(lái)這個(gè)ID會(huì)發(fā)現(xiàn)這個(gè)ID是一個(gè)很大的數(shù)字。沒(méi)有得到我們想象的一個(gè)數(shù)字,其實(shí)這個(gè)ID是POSIX線(xiàn)程庫(kù)提供的一個(gè)數(shù)字,而linux內(nèi)核中也為這個(gè)線(xiàn)程提供了一個(gè)ID,這個(gè)ID可以通過(guò)gettid獲得,gettid是linux內(nèi)核提供的一個(gè)系統(tǒng)調(diào)用,Glibc沒(méi)有封裝函數(shù),只能通過(guò)系統(tǒng)調(diào)用實(shí)現(xiàn)。

POSIX:

#include <pthread>

pthread_t pthread_self(void);

linux系統(tǒng)調(diào)用:

#include <sys/types.h>

#include <sys/syscall.h>

pid_t gettid(void)

{

return syscall(SYS_gettid);

}

下面我們通過(guò)一個(gè)例程看下這兩個(gè)函數(shù)的區(qū)別。

#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 entern");

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 = %xn", pid, posix_tid, linux_tid);

}

void *thread_b(void *arg)

{

printf("thread b entern");

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 = %xn", 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 closen");

return 0;

}

程序運(yùn)行結(jié)果:

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

通過(guò)這個(gè)函數(shù)我們可以發(fā)現(xiàn)posix提供的這個(gè)ID不是很有規(guī)律,而linux內(nèi)核為線(xiàn)程提供的ID是經(jīng)跟在主進(jìn)程進(jìn)程號(hào)的數(shù)字,如上面程序中主進(jìn)程ID為3b89而兩個(gè)線(xiàn)程的ID分別為3b8a,3b8b。

“本文由華清遠(yuǎn)見(jiàn)http://www.embedu.org/index.htm提供”



來(lái)源:華清遠(yuǎn)見(jiàn)0次

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉