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);
}