當前位置:首頁 » 編程語言 » c語言wait函數信號量為0
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言wait函數信號量為0

發布時間: 2022-06-20 00:51:07

① 在整型信號量機制中的wait操作,只要是信號量s小於等於0就會不斷測試什麼

•整型信號量
–是一個整型量,通過2個原子操作wait(s)和signal(s)來訪問
Wait(s):
while s<= 0 do no-op
s:=s-1;

Signal(s): s:=s+1;
在wait原子操作中,當s<= 0時,就立刻循環回去繼續判斷S的值是否小於等於0

c語言的sleep,wait,delay函數有什麼區別

wait 是等待子進程的返回 sleep 參數指定暫停時間, 單位是 s delay 參數指定暫停時間, 單位是 ms 所以 sleep(n) == delay(1000*n) 原型:extern void sleep(unsigned int sec); 用法:#include <system.h 功能:短暫延時 說明:延時sec秒舉例:// sleep.c #include <system.h main(){int c;clrscr();printf("\nHello, world!");sleep(1);clrscr();printf("\nHi, guys");getchar();return 0;}原型:extern void delay(unsigned int msec); 用法:#include <system.h 功能:短暫延時 說明:延時msec*4毫秒舉例:// delay.c #include <system.h main(){int c;clrscr();printf("\nHello, world!"); delay(250); // 250*4=1000msec=1secclrscr();printf("\nHi, guys");getchar();return 0;}wait(等待子進程中斷或結束)相關函數waitpid,fork表頭文件#include<sys/types.h #include<sys/wait.h 定義函數pid_t wait (int * status);函數說明 wait()會暫時停止目前進程的執行,直到有信號來到或子進程結束。如果在調用wait()時子進程已經結束,則wait()會立即返回子進程結束狀態 值。子進程的結束狀態值會由參數status 返回,而子進程的進程識別碼也會一快返回。如果不在意結束狀態值,則參數status可以設成NULL。子進程的結束狀態值請參考waitpid()。 返回值如果執行成功則返回子進程識別碼(PID),如果有錯誤發生則返回-1。失敗原因存於errno中。

③ 一種是用於實現進程互斥的信號量,初值一般為1;當為0時表示什麼含義

1,D 2,B 3,C 4,C 5,B
1,p,v操作是信號量的原子操作,是指wait(),signal()操作,具有不可再分性,是信號量的原語操作
.因此選D.
2,信號量的值為1,表示開始系統有兩個可用的資源,現在變成-1,則表示有一個資源正在等待,因此選B。
4,人們把在每個進程中訪問臨界資源的那段代碼稱為臨界區,因此選C。
5,兩個並發進程,mutex的初值為1,表示有一個可用資源,現在為0,即表示有一個資源進入臨界區,因此選B。

④ c語言計算時間總是為0

clock() 獲取毫秒。程序執行太快,時間差為0
這是用clock() 的例子:
void example_dt_ms()
{
clock_t tick1,tick2;
int i,j,k;
double dt;
tick1=clock(); //ms
//for (i=0;i<1000;i++) for (j=0;j<1000;j++) for (k=0;k<100;k++) {};
wait_ms(2000);
tick2=clock();
dt = (double) (tick2 - tick1);
printf("==============\nexample_dt_ms:\n");
printf("dt = %lf ms\n",dt);
}

精確計時間差:
用QueryPerformanceCounter()函數返回當時時鍾讀數,計時開始用它得到開始讀數,計時結束用它得到結束讀數,兩個數相減是時間差讀數。讀數轉換為時間,要用QueryPerformanceFrequency()函數獲得時鍾頻率,讀數除以時鍾頻率,得時間,單位是秒。乘1000得毫秒。據說,這種方法比較精確。

void accurate_dt_ms()
{
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
int executionTime;
double dt,d_freq,d_begin_t,d_end_t;
// Start of time interval
QueryPerformanceFrequency(&nFreq);
d_freq = (double) nFreq.QuadPart;
QueryPerformanceCounter(&nBeginTime);
// Any code
// ...
wait_ms(2000);
// End of time interval
QueryPerformanceCounter(&nEndTime);
// time interval in ms
executionTime = (nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / nFreq.QuadPart;
dt = (double) ((nEndTime.QuadPart - nBeginTime.QuadPart) * 1000) / d_freq;
printf("==============\naccurate_dt_ms:\n");
printf("dt=%d in ms or dt=%lf ms\n", executionTime,dt);
}

還有一種方法是用Pentium晶元匯編指令 0x0F 和 0x31,讀取時鍾計數圖章,類似QueryPerformanceCounter()函數,計時開始和結束分別調用一次,得讀數差,並扣除執行一次調用函數消耗的額外時間。讀數差怎樣轉化成時間是個問題,如果知道CPU速度可以算得時間,如果知道時間,可以估算CPU速度。子程序 get_CPU_speed() 計算CPU的速度,單位 百萬赫。
// Pentium instruction "Read Time Stamp Counter".
__forceinline unsigned _int64 My_clock(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}

unsigned _int64 Start(void) { return My_clock();}
unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)
{return My_clock()-m_start - m_overhead; }

void get_CPU_speed()
{
unsigned _int64 m_start=0, m_overhead=0;
unsigned int CPUSpeedMHz;
m_start = My_clock();
m_overhead = My_clock() - m_start - m_overhead;
printf("overhead for calling My_clock=%I64d\n", m_overhead);
m_start = My_clock();
wait_ms(2000);
CPUSpeedMHz=(unsigned int) ( (My_clock()- m_start - m_overhead) / 2000000);
printf("CPU_Speed_MHz: %u\n",CPUSpeedMHz);
}

⑤ (計算機操作系統)wait操作和signal操作什麼意思

wait操作:

sem_wait是一個函數,也是一個原子操作,它的作用是從信號量的值減去一個「1」,但它永遠會先等待該信號量為一個非零值才開始做減法。也就是說,如果你對一個值為2的信號量調用sem_wait(),線程將會繼續執行,將信號量的值將減到1。

如果對一個值為0的信號量調用sem_wait(),這個函數就會原地等待直到有其它線程增加了這個值使它不再是0為止。如果有兩個線程都在sem_wait()中等待同一個信號量變成非零值。

那麼當它被第三個線程增加 一個「1」時,等待線程中只有一個能夠對信號量做減法並繼續執行,另一個還將處於等待狀態。sem_trywait(sem_t *sem)是函數sem_wait的非阻塞版,它直接將信號量sem減1,同時返回錯誤代碼。

signal操作:

sig是傳遞給signal的唯一參數。執行了signal()調用後,進程只要接收到類型為sig的信號,不管其正在執行程序的哪一部分,就立即執行func()函數。當func()函數執行結束後,控制權返回進程被中斷的那一點繼續執行。

signal()會依參數signum 指定的信號編號來設置該信號的處理函數。當指定的信號到達時就會跳轉到參數handler指定的函數執行。

當一個信號的信號處理函數執行時,如果進程又接收到了該信號,該信號會自動被儲存而不會中斷信號處理函數的執行,直到信號處理函數執行完畢再重新調用相應的處理函數。但是如果在信號處理函數執行時進程收到了其它類型的信號,該函數的執行就會被中斷。

(5)c語言wait函數信號量為0擴展閱讀:

signal操作的注意點:

1、不要使用低級的或者STDIO.H的IO函數

2、不要使用對操作

3、不要進行系統調用

4、不是浮點信號的時候不要用longjmp

5、signal函數是由ISO C定義的。因為ISO C不涉及多進程,進程組以及終端I/O等,所以他對信號的定義非常含糊,以至於對UNIX系統而言幾乎毫無用處。

參考資料來源:網路-signal

參考資料來源:網路-sem_wait

⑥ c語言實例,linux線程同步的信號量方式 謝謝

這么高的懸賞,實例放後面。信號量(sem),如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以"sem_"打頭。線程使用的基本信號量函數有四個。

信號量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux只支持為0,即表示它是當前進程的局部信號量),然後給它一個初始值VALUE。
等待信號量。給信號量減1,然後等待直到信號量的值大於0。
intsem_wait(sem_t*sem);
釋放信號量。信號量值加1。並通知其他等待線程。
intsem_post(sem_t*sem);
銷毀信號量。我們用完信號量後都它進行清理。歸還佔有的一切資源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}

⑦ C語言的sleep,wait,delay函數有什麼區別

wait 是等待子進程的返回
sleep 參數指定暫停時間, 單位是 s
delay 參數指定暫停時間, 單位是 ms
所以 sleep(n) == delay(1000*n)
原型:extern void sleep(unsigned int sec);
用法:#include <system.h
功能:短暫延時
說明:延時sec秒舉例:// sleep.c
#include <system.h
main(){int c;clrscr();printf("\nHello, world!");sleep(1);clrscr();printf("\nHi, guys");getchar();return 0;}原型:extern void delay(unsigned int msec);
用法:#include <system.h
功能:短暫延時
說明:延時msec*4毫秒舉例:// delay.c
#include <system.h
main(){int c;clrscr();printf("\nHello, world!");
delay(250); // 250*4=1000msec=1secclrscr();printf("\nHi, guys");getchar();return 0;}wait(等待子進程中斷或結束)相關函數waitpid,fork表頭文件#include<sys/types.h
#include<sys/wait.h
定義函數pid_t wait (int * status);函數說明
wait()會暫時停止目前進程的執行,直到有信號來到或子進程結束。如果在調用wait()時子進程已經結束,則wait()會立即返回子進程結束狀態
值。子進程的結束狀態值會由參數status 返回,而子進程的進程識別碼也會一快返回。如果不在意結束狀態值,則參數status可以設成NULL。子進程的結束狀態值請參考waitpid()。
返回值如果執行成功則返回子進程識別碼(PID),如果有錯誤發生則返回-1。失敗原因存於errno中。

⑧ C語言中,函數返回值為零是什麼意思不加return 0對得到的函數有什麼影響嗎加了會產生什麼效

在C語言中,默認返回0的時候表示成功,其它表示失敗,在標准C函數庫中,如果返回值不為0表示失敗,且返回值對應一個errno,每個errno對應了一種錯誤解釋。

如果函數聲明了返回值,但沒有return 0,則編譯時會報錯

⑨ 管程的wait操作和信號量的wait操作有什麼區別

wait(s) 就是s減1
如果原來是0
那麼就是wait(0)
還記得C語言中的while嗎 while(0)就是不進入循環 一直等到while(1)再進入循環
(可以這么理解,但實際while直接不執行循環里的語句,跳過去了)
如果原來>0
直接減1,s的資源少了一個
如果原來