- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在做一个学校项目,我们必须制作一个多线程网络服务器。当我在我的信号量上调用 sem_wait
时遇到问题(应该初始化为 0 但似乎已经被 sem_post()
编辑为 1)。我得到一个 SIGABRT。
我在下面附上我的代码,我在导致我的问题的行上发表了评论。我花了几个小时使用调试器,但运气不佳。
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
#include <string.h>
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <vector>
#include <semaphore.h>
#include <stdio.h>
#include <cstdlib>
#include <strings.h>
#define PORTNUM 7000
#define NUM_OF_THREADS 5
#define oops(msg) { perror(msg); exit(1);}
#define FCFS 0
#define SJF 1;
void bindAndListen();
void acceptConnection(int socket_file_descriptor);
void* dispatchJobs(void*);
void* replyToClient(void* pos);
//holds ids of worker threads
pthread_t threads[NUM_OF_THREADS];
//mutex variable for sleep_signal_cond
pthread_mutex_t sleep_signal_mutex[NUM_OF_THREADS];
//holds the condition variables to signal when the thread should be unblocked
pthread_cond_t sleep_signal_cond[NUM_OF_THREADS];
//mutex for accessing sleeping_thread_list
pthread_mutex_t sleeping_threads_mutex = PTHREAD_MUTEX_INITIALIZER;
//list of which threads are sleeping so they can be signaled and given a job
std::vector<bool> *sleeping_threads_list = new std::vector<bool>();
//number of threads ready for jobs
sem_t available_threads;
sem_t waiting_jobs;
//holds requests waiting to be given to one of the threads for execution
//request implemented as int[3] with int[0]== socket_descriptor int[1]== file_size int[2]== file_descriptor of requested file
//if file_size == 0 then HEAD request
std::vector<std::vector<int> >* jobs = new std::vector<std::vector<int> >();
pthread_mutex_t jobs_mutex = PTHREAD_MUTEX_INITIALIZER;
int main (int argc, char * const argv[]) {
//holds id for thread responsible for removing jobs from ready queue and assigning them to worker thread
pthread_t dispatcher_thread;
//initializes semaphores
if(sem_init(&available_threads, 0, NUM_OF_THREADS) != 0){
oops("Error Initializing Semaphore");
}
if(sem_init(&waiting_jobs, 0, 0) !=0){
oops("Error Initializing Semaphore");
}
//initializes condition variables and guarding mutexes
for(int i=0; i<NUM_OF_THREADS; i++){
pthread_cond_init(&sleep_signal_cond[i], NULL);
pthread_mutex_init(&sleep_signal_mutex[i], NULL);
}
if(pthread_create(&dispatcher_thread, NULL, dispatchJobs, (void*)NULL) !=0){
oops("Error Creating Distributer Thread");
}
for (int i=0; i<NUM_OF_THREADS; i++) {
pthread_mutex_lock(&sleeping_threads_mutex);
printf("before");
sleeping_threads_list->push_back(true);
printf("after");
pthread_mutex_unlock(&sleeping_threads_mutex);
}
printf("here");
for (int i=0; i<NUM_OF_THREADS; i++) {
//creates threads and stores ID in threads
if(pthread_create(&threads[i], NULL, replyToClient, (void*)i) !=0){
oops("Error Creating Thread");
}
}
/*
if(sem_init(&available_threads, 0, NUM_OF_THREADS) !=0){
oops("Error Initializing Semaphore");
}
if(sem_init(&waiting_jobs, 0, 0) !=0){ //this is the semaphore thats used in the sem_wait
oops("Error Initializing Semaphore");
}*/
bindAndListen();
}
//binds to socket and listens for connections
//being done by main thead
void bindAndListen(){
struct sockaddr_in saddr;
struct hostent *hp;
char hostname[256];
int sock_id, sock_fd;
gethostname(hostname, 256);
hp = gethostbyname(hostname);
bzero(&saddr, sizeof(saddr));
//errno = 0;
bcopy(hp->h_addr, &saddr.sin_addr, hp->h_length);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORTNUM);
saddr.sin_addr.s_addr = INADDR_ANY;
sock_id = socket(AF_INET, SOCK_STREAM, 0);
if(sock_id == -1){
oops("socket");
printf("socket");
}
if(bind(sock_id, (const sockaddr*)&saddr, sizeof(saddr)) ==0){
if(listen(sock_id, 5) ==-1){
oops("listen");
}
//each time a new connection is accepted, get file info and push to ready queue
while(1){
int addrlen = sizeof(saddr);
sock_fd = accept(sock_id, (sockaddr*)&saddr, (socklen_t*)&addrlen);
if (sock_fd > 0) {
acceptConnection(sock_fd);
}else {
oops("Error Accepting Connection");
}
}
}else{
oops("there was an error binding to socket");
}
}// end of bindAndListen()
//accepts connection and gets file info of requested file
//being done by main thread
void acceptConnection(int sock_fd){
printf("**Server: A new client connected!");
//only using loop so on error we can break out on error
while(true){
//used to hold input from client
char* inputBuff = new char[BUFSIZ];
int slen = read(sock_fd, inputBuff, BUFSIZ);
//will sit on space between HEAD/GET and path
int pos1 = 0;
//will sit on space between path and HTTP version
int pos2 = 0;
//need duplicate ptr so we can manipulate one in the loop
char* buffPtr = inputBuff;
//parses client input breaks up query by spaces
for(int i=0; i<slen; i++){
if(*buffPtr == ' '){
if (pos1 == 0) {
pos1 = i;
}else {
pos2 = i;
break;
}
}
buffPtr++;
}
if((pos1 - pos2) >=0){
std::string str = "Invalid Query";
write(sock_fd, str.c_str(), strlen(str.c_str()));
break;
}
printf("slen length %d\n", slen);
std::string* method = new std::string(inputBuff, pos1);
printf("method length %lu\n",method->length());
//increment the ptr for buff to the starting pos of the path
inputBuff += ++pos1;
printf("pos2 - pos1 %d\n", (pos2 - pos1));
printf("pos1 = %d pos2 = %d\n", pos1, pos2);
std::string* path = new std::string(inputBuff, (pos2 - pos1));
printf("path length %lu\n", path->length());
printf("part1 %s\n", method->c_str());
printf("part2 %s\n", path->c_str());
//opens file requested by client
int fd = open(path->c_str(), O_RDONLY);
if(fd < 0){
std::string* error = new std::string("Error Opening File");
*error += *path + std::string(strerror(errno), strlen(strerror(errno)));
write(sock_fd, error->c_str(), strlen(error->c_str()));
break;
}
int file_size;
if(method->compare("GET") == 0){
//gets file info and puts the resulting struct in file_info
struct stat file_info;
if(fstat(fd, &file_info) !=0){
oops("Error getting file info");
}
file_size = file_info.st_size;
}else if(method->compare("HEAD")){
file_size = 0;
}else{
write(sock_fd, "Invalid Query", strlen("Invalid Query"));
break;
}
//job to be pushed to ready queue
std::vector<int> job;
job.push_back(sock_fd);
job.push_back(file_size);
job.push_back(fd);
//check mutex guarding the ready queue
pthread_mutex_lock(&jobs_mutex);
//push job to back of ready queue
jobs->push_back(job);
//unlock mutex guarding the ready queue
pthread_mutex_unlock(&jobs_mutex);
//increment number of jobs in ready queue
sem_post(&waiting_jobs);
} //end of while(true)
// we only end up here if there was an error
fflush(stdout);
close(sock_fd);
}// end of acceptConnection()
//routine run by dispather thread
void *dispatchJobs(void*){
while(true){
//wait for a thread to be available to execute a job
sem_wait(&available_threads);
//wait for a job to be waiting in the ready queue
sem_wait(&waiting_jobs); //this is the line thats crashing
//aquire lock to check which threads are waiting
pthread_mutex_lock(&sleeping_threads_mutex);
//go through list of threads to see which is waiting
for(int i=0; i<sleeping_threads_list->size(); i++){
if(sleeping_threads_list->at(i)){
//unlocks lock for access to list of waiting threads
pthread_mutex_unlock(&sleeping_threads_mutex);
//allows us access to the list of condition variables to signal the thread to resume execution
pthread_mutex_lock(&sleep_signal_mutex[i]);
pthread_cond_signal(&sleep_signal_cond[i]);
pthread_mutex_unlock(&sleep_signal_mutex[i]);
}
}
}//end of while(true)
}//end of dispatchJobs()
//sends file or metadata to client
//run by worker thread
//pos is position of condition variable that it waits to be signaled in the sleep_signal_cond[] array
void* replyToClient(void* pos){
int position = (long)pos;
while(true){
//waits for dispather thread to signal it
pthread_mutex_lock(&sleep_signal_mutex[position]);
pthread_cond_wait(&sleep_signal_cond[position], &sleep_signal_mutex[position]);
pthread_mutex_unlock(&sleep_signal_mutex[position]);
//lock mutex to get job to be executed
pthread_mutex_lock(&jobs_mutex);
std::vector<int> job = jobs->front();
//removes job from front of vector
jobs->erase(jobs->begin());
//releases mutex
pthread_mutex_unlock(&jobs_mutex);
//socket file descriptor, used for writing to socket
int sock_fd =job[0];
int file_size = job[1];
//file descriptor for requested job
int fd = job[2];
//holds output to be written to socket
char* outputBuffer = new char[BUFSIZ];
//GET request, send file
if(file_size !=0){
int readResult = 0;
while ((readResult = read(fd, outputBuffer, BUFSIZ)) > 0) {
if(write(sock_fd, outputBuffer, readResult) != readResult){
printf("We may have a write error");
}
}
if(readResult < 0){
oops("Error Reading File");
}
if(readResult == 0){
printf("finished sending file");
}
}else{ // HEAD request
}
//increment number of available threads
sem_post(&available_threads);
}
}// end of replyToClient()
最佳答案
再次检查代码的整个逻辑——可以到达这里:
pthread_mutex_lock(&jobs_mutex);
std::vector<int> job = jobs->front();
//removes job from front of vector
jobs->erase(jobs->begin());
//releases mutex
pthread_mutex_unlock(&jobs_mutex);
jobs->size () == 0
,在这种情况下,front()
和 erase()
调用未定义的行为,这很可能会导致您观察到的效果。
检查您的程序在进行以下更改后是否仍然崩溃:
//lock mutex to get job to be executed
pthread_mutex_lock(&jobs_mutex);
if (jobs->size () == 0)
{
pthread_mutex_unlock (&jobs_mutex);
continue;
}
std::vector<int> job = jobs->front();
//removes job from front of vector
jobs->erase(jobs->begin());
//releases mutex
pthread_mutex_unlock(&jobs_mutex);
关于c - POSIX sem_wait() SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7925582/
是否有详细说明从一个 POSIX 版本更改为另一个版本的文档?我正在寻找一些东西,在表格 View 中,详细说明从 2004 年到 2008 年的变化 最佳答案 有点。这是Rationale ,每卷一
根据POSIX FAQ ,该标准已于 2013 年由 IEEE 修订和批准。 与 2008 年的先前标准相比有何变化? 最佳答案 根据 online edition 中的摘要, POSIX.1-2
我正在开发一个简单的并行应用程序,我想在其中使用单个进程来维护有关一系列工作进程的状态信息。设置一个 POSIX 消息队列似乎相对容易,其中所有工蜂都可以向状态维护者发送定期更新。我的问题? POSI
计划使用 posix 信号量来同步 2 个进程。不太确定使用哪个 - 命名或未命名。 各自的优缺点是什么?我如何决定使用哪个?在哪些情况下,一种优于另一种? 谢谢。 最佳答案 如果这两个进程不相关,则
嵌套参数替换在 Zsh 中有效: $ param=abc # nested remove prefix ${...#a} and remove suffix ${...%c} => $ printf
更新:整个问题出在一条错误线上,这是我的 C++ 代码中的语法错误。 在 Linux 上我发现 #define _NSIG 64 在 asm-generic/signal.h ,
我在网上遇到了两个 POSIX 文档。 http://pubs.opengroup.org/onlinepubs/009695399/ (IEEE 标准 1003.1,2004 年版) Abstrac
我正在开发一个简单的软件来检查我是否能够使用我研究过的 POSIX 定时器和信号进行编程。 我正在尝试做一个简单的程序来启动计时器并在一定的纳秒内发出信号 下面的程序运行得不好,所以我写了一些关于我的
如果我有一个通过 shell 运行的应用程序,是否有 POSIX 文档说 --help需要支持吗?我会这么认为,因为这似乎是最流行的终端应用程序(GNU 工具等)中的标准做法。 我很好奇我是否可以使用
自适应 AUTOSAR 基于什么 POSIX PSE51? 在学习自适应 AUTOSAR 时,我发现“自适应 AUTOSAR 基于 POSIX PSE51”。 但是,我不明白什么是 POSIX PSE
GNU bash manual说到shell参数有以下一段话: The command builtin does not prevent builtins that take assignment s
我应该在我的嵌入式 Linux 环境中使用 System V 消息队列还是 Posix 消息队列?项目中常用什么? 最佳答案 两者都有相同的基本工具——信号量、共享内存和消息队列。它们为这些工具提供了
用 tsearch 填充了 POSIX 二叉树后,如何清理整棵树? GCC 提供了 tdestroy 作为扩展,但是如果你想使用 POSIX-only 函数,你该怎么做呢? 我当前的实现使用 twal
来自 C++ 应用程序。为 AIX、HP-UX、Linux、OSX 和 Solaris 编译是否有一种简单的方法来确定应用程序是否可用。在系统启动后 5 分钟内运行? 在 Windows 上我可以这样
System V IPC 和 POSIX IPC 之间有什么区别? 为什么我们有两个标准? 如何决定使用哪些 IPC 函数? 最佳答案 两者都有相同的基本工具——信号量、共享内存和消息队列。它们提供的
根据this ,POSIX 库不包含 getopt.h。但是,我在 unistd.h 中找到了这个: #ifdef __USE_POSIX2 /* Get definitions and proto
我正在尝试使用 POSIX 共享内存和 POSIX 信号量构建客户端服务器应用程序。我是否必须将信号量放在共享内存段内,或者信号量是否可以只是全局变量?我希望遵守 POSIX 约定。 最佳答案 不,信
尝试读取 Rust fn 中任意用户的主目录,并使用 posix::pwd crate。 不幸的是,我找不到任何使用 FFI 的好例子,并且一直在处理关于指针和类型可变性的各种类型错误。 我的(非编译
我阅读了一个信号的手册页使用 man 7 signal我看到两种类型的信号。所以,我有一个问题, Linux 中的POSIX 可靠信号 和POSIX 实时信号 有什么区别? 最佳答案 如今,将这些表述
据我所知,ucontext 提供了比 setjmp 更好的东西。但它已被弃用,现在已从 POSIX 规范中删除。那么它为什么会出现,又为什么会被移除? 最佳答案 makecontext的签名来自 uc
我是一名优秀的程序员,十分优秀!