1、急!LINUX下,GCC編譯,原程序包含<semaphore.h>頭文件,為什麼編譯時說sem_wait,sem_post等未定義的引用
編譯時加上參數:-lpthread
要看報錯的階段,是在編譯還是鏈接階段.
如果編譯時函數沒有找到,那是頭文件的問題,如果鏈接時未定義引用,那是c庫的問題.
如果你的頭文件都正常包含了,那可能你的c庫沒有使能semaphore的支持.
2、emerge python * configure has detected that the sem_open function is broken.
* ERROR: dev-lang/python-2.7.5-r3::gentoo failed (configure phase):
* Broken sem_open function (bug 496328)
* die "Broken sem_open function (bug 496328)";
以上3條提示錯誤,你這個應該是應用程序的編程階段或者測試。錯誤處修改,或者上下路邏輯檢查
3、linux信號燈使用sem_open的錯誤 sem_open_test.c:(.text+0x4c):對『sem_open』未定義的引用
g
4、linux編程時的信號量問題。 我以前用過的信號量頭文件是<semaphore.h>,而現在又發現還有個<sys/sem.h>
信號量在進程是以有名信號量進行通信的,在線程是以無名信號進行通信的,因為線程linux還沒有實現進程間的通信,所以在sem_init的第二個參數要為0,而且在多線程間的同步是可以通過有名信號量也可通過無名信號,但是一般情況線程的同步是無名信號量,無名信號量使用簡單,而且sem_t存儲在進程空間中,有名信號量必須LINUX內核管理,由內核結構struct ipc_ids 存儲,是隨內核持續的,系統關閉,信號量則刪除,當然也可以顯示刪除,通過系統調用刪除,
消息隊列,信號量,內存共享,這幾個都是一樣的原理。,只不過信號量分為有名與無名
無名使用 <semaphore.h>,
有名信號量<sys/sem.h>
無名信號量不能用進程間通信,
//無名與有名的區別,有名需要KEY值與IPC標識
所以sem_init的第二個參數必須為0,,,,
5、procmp與任務管理器生成的mp有什麼不同
在程序開始處
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
SetUnhandledExceptionFilter(回調你自己函數);
在回調函數里用MiniDumpWriteDump
6、如何使用Linux提供的信號量來實現進程的互斥和同步?
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<semaphore.h>
#include<stdlib.h>
#define N 3
pthread_mutex_t mutex_w,mutex_r; // 定義讀寫互斥鎖
sem_t sem_w,sem_r; //定義讀寫信號量
int data[N];
int pos=0;
void *function_w(void *arg)
{
int w = *(int *)arg;
pos = w;
while(1)
{
usleep(100000);
sem_wait(&sem_w);//等待可寫的資源
pthread_mutex_lock(&mutex_w);//禁止別的線程寫此資源
data[pos] = w;
w++;
w++;
w++;
pos++;
pos=pos%N;
pthread_mutex_unlock(&mutex_w);//別的線程可寫此資源
sem_post(&sem_r);// 釋放一個讀資源
}
return (void *)0;
}
void *function_r(void *arg)
{
while(1)
{
sem_wait(&sem_r);//等待可讀的資源
pthread_mutex_lock(&mutex_r);//禁止別的線程讀此資源
printf("%d\n",data[(pos+N-1)%N]);
pthread_mutex_unlock(&mutex_r);//別的線程可讀此資源
sem_post(&sem_w);// 釋放一個寫資源
}
return (void *)0;
}
int main(int argc, char **argv)
{
pthread_t thread[2*N];
int i;
pthread_mutex_init(&mutex_w,NULL);
pthread_mutex_init(&mutex_r,NULL);
sem_init(&sem_w,0,N);
sem_init(&sem_r,0,0);
for(i=0;i<N;i++)
{
if ( pthread_create(&thread[i],NULL,function_w,(void *)&i) < 0)//創建寫線程
{
perror("pthread_create");
exit(-1);
}
}
for(i=N;i<2*N;i++)
{
if ( pthread_create(&thread[i],NULL,function_r,NULL) < 0)//創建讀線程
{
perror("pthread_create");
exit(-1);
}
}
sleep(1);
return(0);
}