急!LINUX下,GCC编译,原程序包含<semaphore.h>头文件,为什么编译时说sem_wait,sem_post等未定义的引用

急!LINUX下,GCC编译,原程序包含<semaphore.h>头文件,为什么编译时说sem_wait,sem_post等未定义的引用,第1张

编译时加上参数:-lpthread

要看报错的阶段,是在编译还是链接阶段.

如果编译时函数没有找到,那是头文件的问题,如果链接时未定义引用,那是c库的问题.

如果你的头文件都正常包含了,那可能你的c库没有使能semaphore的支持.

你就一个信号量,而且两个线程都是先sem_wait,你的两个线程都会卡在sem_wait(&sem1)

你可以把其中给一个线程的sem_wait跟sem_post换个位置试一下

。。

你子线程控制同步了么? 断错误一般是内存操作出错 和oci 或者pthread的关系不大!

void* OracleProcess(GPS_DATA GpsRec) // 数据库数据处理

{

interval = 0

struct HashItem* pHash

pHash = inithashtable(MAX_REC<<2)

char sql[384] = {0}

char temp[256] = {0}

char tName[10] = {0} // 表名字

int i,k

int j = TotalRec >>RATE

double distance

for(i=0i <ji++)

{

sprintf(temp,"%s%f%f%f%d",gps_last[i].tid,gps_last[i].lon,gps_last[i].lat,gps_last[i].speed,gps_last[i].udate)

InsertHash(temp, pHash, MAX_REC<<2) // 插入最后GPS信息到hash

memset(temp,0x00,256)

}

for(i = 0i <TotalReci++)

{

for(k=0k<jk++) // 查询车机是否在册

if(strcmp(GpsRec[i].tid,tid[k]) == 0)

break

if(k <j)

{

if(GpsRec[i].udate != 0.00)

{

distance = InfoUpdate(GpsRec,i)// 最新GPS数据更新

sprintf(temp,"%s%f%f%f%d",GpsRec[i].tid,GpsRec[i].lon,GpsRec[i].lat,GpsRec[i].speed,GpsRec[i].udate)

if(GetHashTablePos(temp, pHash, MAX_REC<<2) == -1) // 查找hash是否存在

{

if (distance >0.0001)

{

sprintf(tName,"GPS_%d_Y",tf[k])

InsertHash(temp, pHash, MAX_REC<<2)// 插入

sprintf(sql,"insert into %s (id,tm_id,lon,lat, speed, utc_time, udate,mileage,DIRECTION,DISTANCE) values (seq_gps.nextVal,'%s','%f','%f','%f','%d','%d','%f','%d','%f','%d')",

tName,GpsRec[i].tid,GpsRec[i].lon,GpsRec[i].lat,GpsRec[i].speed,GpsRec[i].utime,GpsRec[i].udate,GpsRec[i].mileage,GpsRec[i].dir,distance,interval)

printf("%s\n",sql)

oci_excu(oracle_env,(text *)sql,0) // 插入数据

memset(tName,0x00,10)

}

}

memset(sql,0x00,384)

memset(temp,0x00,256)

}

}

}

memset(GpsRec,0x00,sizeof(GpsRec))

free(pHash)

pthread_exit(NULL)

}

void TcpProcess(int tfd) // 处理TCP连接上的事务

{

struct timeval ntime

int index = 0,times,ret

int rlen = 0,rflag = 0

char recvbuf[513] = {0}

bzero(recvbuf,513)

while(1)

{

ret = rlen = read(tfd,recvbuf,512)

if(rlen <= 0)

break

if((rlen%32) == 0) // 32长度为标准TCP信息

{

times = 0

ret >>= 5

while(ret--)

{

if(tflag[tfd] == tfd) // 已经存在的socket

{

LOVENIX *info = (LOVENIX *)malloc(sizeof(LOVENIX))

memset(info,0x00,sizeof(LOVENIX))

if(recvbuf[times] == 0x58 || recvbuf[times] == 0x59)

ProtocolAnalysisLovenixTcp(&recvbuf[times],info)

else if(recvbuf[times] == 0x24)

ProtocolAnalysisLovenixUdp(&recvbuf[times],info)

sprintf(info->tid,"%s",seq[tfd]) // 合成车辆ID

DataProcess(info) // 处理GPS数据

free(info)

gettimeofday(&ntime, NULL)

cntime[tfd] = ntime.tv_sec // 更新时间

times += 32

}

}

}

else if(rlen >32)

{

if(!rflag)

{

if((index = RegLovenix(tfd,recvbuf)) >-1)

{

sprintf(seq[tfd],"%s",tid[index]) // 将对应的socket设备ID保存

gettimeofday(&ntime, NULL)

sfd[tfd] = tfd

cntime[tfd] = ntime.tv_sec

tflag[tfd] = tfd

rflag = 1

}

}

}

if(rlen <512) // 已经读完

break

memset(recvbuf,0x00,rlen)

}

}

void *TcpServer(void *arg)

{

int port = (unsigned int) arg

int efd,i

struct timeval ntime

int listener, nfds, n, listen_opt = 1, lisnum

struct sockaddr_in my_addr, their_addr

socklen_t len = sizeof(their_addr)

lisnum = MAXLISTEN

for(i=0i<MAX_RECi++)

{

sfd[i] = 0

tflag[i] = 0

}

if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) // 开启 socket 监听

{

lprintf(lfd, FATAL, "TCP Socket error!\n")

exit(1)

}

else

lprintf(lfd, INFO, "TCP socket creat susscess!\n")

setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (void *) &listen_opt,(int) sizeof(listen_opt)) // 设置端口多重邦定

setnonblocking(listener)

bzero(&my_addr, sizeof(my_addr))

my_addr.sin_family = PF_INET

my_addr.sin_port = htons(port)

my_addr.sin_addr.s_addr = INADDR_ANY

if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1)

{

lprintf(lfd, FATAL, "TCP bind error!\n")

exit(1)

}

else

lprintf(lfd, INFO, "TCP bind susscess!\n")

if (listen(listener, lisnum) == -1)

{

lprintf(lfd, FATAL, "TCP listen error!\n")

exit(1)

}

else

lprintf(lfd, INFO, "TCP listen susscess!\n")

kdpfd = epoll_create(MAXEPOLLSIZE) // 创建 epoll句柄,把监听socket加入到epoll集合里

ev.events = EPOLLIN | EPOLLET // 注册epoll 事件

ev.data.fd = listener

if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) <0)

lprintf(lfd, FATAL, "EPOLL_CTL_ADD error!\n")

while (1)

{

sem_wait(&sem_tcp)// 等待 sem_TCP

sem_wait(&sem_tp) // 将tp值减一

nfds = epoll_wait(kdpfd, events, MAXEPOLLSIZE, 1)// 等待有事件发生

if (nfds == -1)

lprintf(lfd, FATAL,"EPOLL_WAIT error!\n")

for (n = 0n <nfds++n) // 处理epoll所有事件

{

if (events[n].data.fd == listener) // 如果是连接事件

{

if ((efd = accept(listener, (struct sockaddr *) &their_addr,&len)) <0)

{

lprintf(lfd, FATAL, "accept error!\n")

continue

}

else

lprintf(lfd, INFO, "Client from :%s\tSocket ID:%d\n", inet_ntoa(their_addr.sin_addr) ,efd)

setnonblocking(efd) // 设置新连接为非阻塞模式

ev.events = EPOLLIN | EPOLLET// 注册新连接

ev.data.fd = efd

if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, efd, &ev) <0) // 将新连接加入EPOLL的监听队列

lprintf(lfd, FATAL, "EPOLL_CTL_ADD error!\n")

else

{

gettimeofday(&ntime, NULL)

cntime[efd] = ntime.tv_sec

sfd[efd] = efd

}

}

else if (events[n].events &EPOLLIN)

tpool_add_work(pool, TcpProcess, (void*)events[n].data.fd) // 读取分析TCP信息

else

{

close(events[n].data.fd)

epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd, &ev)

}

}

sem_post(&sem_cm)

sem_post(&sem_udp)

}

close(listener)

}

int DataProcess(LOVENIX *info) // 处理GPS数据

{

if(sflag == 0 &&(CacheRec != TotalRec)) // 缓存1可用且没有满

{

gps_cache[CacheRec].lat = info->lat

gps_cache[CacheRec].mileage = info->mileage

gps_cache[CacheRec].lon = info->lon

gps_cache[CacheRec].speed = atod(info->speed, strlen(info->speed))*0.514444444*3.6

gps_cache[CacheRec].udate = atoi(info->udate)

gps_cache[CacheRec].utime = atoi(info->utime)

gps_cache[CacheRec].dir = atoi(info->dir)

sprintf(gps_cache[CacheRec].tid ,"%s",info->tid)

CacheRec++

// printf("CacheRec %d\tTotalRec %d \t sflag:%d\n",CacheRec,TotalRec,sflag)

if(CacheRec == TotalRec)

{

sflag = 1

pthread_attr_init(&attr) // 初始化属性值,均设为默认值

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)// 设置线程为分离属性

if (pthread_create(&thread, &attr,(void*) OracleProcess,(void*)gps_cache)) // 创建数据处理线程

lprintf(lfd, FATAL, "oracle pthread_creat error!\n")

CacheRec = 0

}

}

else if(sflag == 1 &&(Cache1Rec != TotalRec)) // 缓存2可用且没有满

{

gps_cache1[Cache1Rec].mileage = info->mileage

gps_cache1[Cache1Rec].lat = info->lat

gps_cache1[Cache1Rec].lon = info->lon

gps_cache1[Cache1Rec].speed = atod(info->speed, strlen(info->speed))*0.514444444*3.6

gps_cache1[Cache1Rec].udate = atoi(info->udate)

gps_cache1[Cache1Rec].utime = atoi(info->utime)

gps_cache1[Cache1Rec].dir = atoi(info->dir)

sprintf(gps_cache1[Cache1Rec].tid ,"%s",info->tid)

Cache1Rec++

if(Cache1Rec == TotalRec)

{

sflag = 0

pthread_attr_init(&attr) // 初始化属性值,均设为默认值

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)// 设置线程为分离属性

if (pthread_create(&thread, &attr,(void*) OracleProcess,(void*)gps_cache1)) // 创建数据处理线程

lprintf(lfd, FATAL, "oracle pthread_creat error!\n")

Cache1Rec = 0

}

}

else

{

lprintf(lfd, FATAL, "No cache to use!\n")

return (0)

}

return (1)

}


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/202004.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-02
下一篇2023-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存