- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的操作系统类开发一个项目,并且在使用 pthread 时遇到 SegFault,但我不太确定是什么导致了问题。
至于程序,我正在尝试完成以下程序:
There is a deep canyon somewhere in Kruger National Park, South Africa, and a single rope that spans the canyon. Baboons can cross the canyon by swinging hand- over-hand on the rope, but if two baboons going in opposite directions meet in the middle, they will fight and drop to their deaths. Furthermore, the rope is only strong enough to hold three baboons. If there are more baboons on the rope at the same time, it will break. Assuming that we can teach the baboons to use semaphores, we would like to design a synchronization scheme with the following properties.
- Once a baboon has begun to cross, it is guaranteed to get to the other side without running into a baboon going the other way.
- There are never more than three baboons on the rope. The order of the baboons crossing the rope should be preserved; i.e., the order in which they enter the rope should be the order in which they exit the rope.
- A continuing stream of baboons crossing in one direction should not bar baboons going the other way indefinitely (no starvation). Solve this requirement such that the FIFO order is preserved. That is, a baboon trying to cross to the left/right that arrives earlier than a baboon trying to cross in the opposite direction gets on the rope first.
基本上,我正在读取一个文本文件,然后模拟一个 FIFO 系统,其中一些猴子试图穿过绳索桥。奇怪的是,我能够让程序运行几次,但它经常会导致 SegFault。
pthread_create(&eastern[i],NULL,(void *) &east_side,(void *)&id[i]);
pthread_create(&western[i],NULL,(void *) &west_side,(void *)&id[i]);
其中 east_side 和 west_side 位于下方。
void* east_side(void*arg)
{
int baboon = *(int*)arg;
int on_rope;
sem_wait(&deadlock_protection);
sem_wait(&east_mutex);
east++;
if (east == 1)
{
sem_wait(&rope);
printf("Baboon %d: waiting\n", baboon);
}
sem_post(&east_mutex);
sem_post(&deadlock_protection);
sem_wait(&counter);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Cross rope request granted (Current crossing: left to right, Number of baboons on rope: %d)\n", baboon,3-on_rope);
sleep(travel_time);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Exit rope (Current crossing: left to right, Number of baboons on rope: %d)\n", baboon, 2-on_rope);
sem_post(&counter);
sem_wait(&east_mutex);
east--;
if (east == 0)
sem_post(&rope);
sem_post(&east_mutex);
}
//thread handling west to east travel
void* west_side(void*arg)
{
int baboon = *(int*)arg;
int on_rope;
sem_wait(&deadlock_protection);
sem_wait(&west_mutex);
west++;
if (west == 1)
{
sem_wait(&rope);
printf("Baboon %d: waiting\n", baboon);
}
sem_post(&west_mutex);
sem_post(&deadlock_protection);
sem_wait(&counter);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Cross rope request granted (Current crossing: right to left, Number of baboons on rope: %d)\n", baboon, 3-on_rope);
sleep(travel_time);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Exit rope (Current crossing: right to left, Number of baboons on rope: %d)\n", baboon, 2-on_rope);
sem_post(&counter);
sem_wait(&west_mutex);
west--;
if (west == 0)
sem_post(&rope);
sem_post(&west_mutex);
}
我正在使用纯文本文件中的示例输入
L,R,R,R,R,R,L,L,R
这将创建输出:
sh-4.3$主输入.txt 5
输入为
L R R R R R L L R
狒狒1:请求交叉绳子(从左到右)
狒狒1:WAITING
狒狒1:已批准交叉绳索请求(当前交叉:从左到右,绳索上的狒狒数量:1)
狒狒2:请求交叉绳子(从右到左)
狒狒3:请求交叉绳子(从右到左)
狒狒4:请求交叉绳子(从右到左)
狒狒5:请求交叉绳子(从右到左)
狒狒1:导出绳(当前交叉口:从左到右,绳上狒狒数量:0)
狒狒2:WAITING
狒狒2:已批准交叉绳索请求(当前交叉:从右到左,绳索上的狒狒数量:1)
狒狒3:已批准交叉绳索请求(当前交叉:从右到左,绳索上的狒狒数量:2)
狒狒4:已批准交叉绳索请求(当前交叉:从右到左,绳索上的狒狒数量:3)
狒狒6:请求交叉绳子(从右到左)
狒狒7:请求交叉绳子(从左到右)
狒狒8:请求交叉绳子(从左到右)
狒狒9:请求交叉绳子(从右到左)
段错误(核心转储)
我已经包含了整个文件,以防问题实际上不是我认为的问题所在。
/*include header files*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <semaphore.h>
//#include <stdbool.h>
//compile with command
//gcc -o main *.c -lpthread -lrt
/*semaphores*/
sem_t rope;
sem_t east_mutex;
sem_t west_mutex;
sem_t deadlock_protection;
sem_t counter;
/*global variables*/
int east = 0;
int west = 0;
int travel_time;
/*function prototypes*/
void crossing(int x);
void* east_side(void*);
void* west_side(void*);
/*main function*/
int main(int argc, char *argv[])
{
char c;
int baboonCnt=0;
char temp[100];
sem_init(&rope,0,1); //ensure mutual exclusion on rope ownership
sem_init(&east_mutex,0,1); //east side on travel
sem_init(&west_mutex,0,1); //west side on travel
sem_init(&deadlock_protection,0,1); //used to prevent deadlocks while using semaphores
sem_init(&counter,0,3); //ensure only 3 baboons are allowed on the rope
//ensure all input arguements are entered
if ( argc == 3 )
{
travel_time = atoi(argv[2]);
FILE *file;
int baboonCnt=0;
if (file = fopen(argv[1], "r") )
{
while((c=getc(file))!=EOF)
{
if(c == 'L'|| c == 'R')
{
temp[baboonCnt] = c;
baboonCnt++;
}
}
}
else
{
printf("Unable to read data from the input file.");
return 0;
}
printf("The input is\n");
int j=0;
for(j;j<baboonCnt;++j)
{
printf("%c ",temp[j]);
}
printf("\n");
int id[baboonCnt];
pthread_t eastern[baboonCnt],western[baboonCnt];
int i=0;
for(i;i<baboonCnt;++i)
{
sleep(1);
if(temp[i]=='L')
{
id[i] = i+1;
printf("Baboon %d: Request to cross rope (left to right)\n", i+1);
pthread_create(&eastern[i],NULL,(void *) &east_side,(void *)&id[i]);
}
else if(temp[i]=='R')
{
id[i] = i+1;
printf("Baboon %d: Request to cross rope (right to left)\n", i+1);
pthread_create(&western[i],NULL,(void *) &west_side,(void *)&id[i]);
}
}
int k=0;
printf("before k loop");
for(k;k<baboonCnt;++k)
{
pthread_join(eastern[k],NULL);
printf("eastern",k);
pthread_join(western[k],NULL);
printf("western %d",k);
}
//destroy all semaphores
sem_destroy (&rope);
sem_destroy (&east_mutex);
sem_destroy (&west_mutex);
sem_destroy (&deadlock_protection);
sem_destroy (&counter);
return 0;
}
else
{
printf("Proper command line usage is: \n<name> <filename> <cross time>\n");
}
}
//thread handling the east to west to travel
void* east_side(void*arg)
{
int baboon = *(int*)arg;
int on_rope;
sem_wait(&deadlock_protection);
sem_wait(&east_mutex);
east++;
if (east == 1)
{
sem_wait(&rope);
printf("Baboon %d: waiting\n", baboon);
}
sem_post(&east_mutex);
sem_post(&deadlock_protection);
sem_wait(&counter);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Cross rope request granted (Current crossing: left to right, Number of baboons on rope: %d)\n", baboon,3-on_rope);
sleep(travel_time);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Exit rope (Current crossing: left to right, Number of baboons on rope: %d)\n", baboon, 2-on_rope);
sem_post(&counter);
sem_wait(&east_mutex);
east--;
if (east == 0)
sem_post(&rope);
sem_post(&east_mutex);
}
//thread handling west to east travel
void* west_side(void*arg)
{
int baboon = *(int*)arg;
int on_rope;
sem_wait(&deadlock_protection);
sem_wait(&west_mutex);
west++;
if (west == 1)
{
sem_wait(&rope);
printf("Baboon %d: waiting\n", baboon);
}
sem_post(&west_mutex);
sem_post(&deadlock_protection);
sem_wait(&counter);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Cross rope request granted (Current crossing: right to left, Number of baboons on rope: %d)\n", baboon, 3-on_rope);
sleep(travel_time);
sem_getvalue(&counter, &on_rope);
printf("Baboon %d: Exit rope (Current crossing: right to left, Number of baboons on rope: %d)\n", baboon, 2-on_rope);
sem_post(&counter);
sem_wait(&west_mutex);
west--;
if (west == 0)
sem_post(&rope);
sem_post(&west_mutex);
}
最佳答案
运行 valgrind 几乎可以立即检测到您的错误:
==10217== Use of uninitialised value of size 8
==10217== at 0x4E39241: pthread_join (in /usr/lib64/libpthread-2.18.so)
==10217== by 0x400E64: main (example.c:103)
第 103 行是 pthread_join(eastern[k],NULL);
; valgrind 强调东部数组在未设置时被读取。
您的数组未满足,您访问未设置的元素。您可以通过这种方式修改连接循环:
for(k;k<baboonCnt;++k)
{
if(temp[k]=='L') {
pthread_join(eastern[k],NULL);
printf("eastern %d\n",k);
}
else if(temp[k]=='R') {
pthread_join(western[k],NULL);
printf("western %d\n",k);
}
}
关于c - 使用 pthread 时出现 SegFault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853480/
所以我需要一个简单的分配器来分配(有时使用清零)并随后从映射内存池中释放 4K block 。然而,在实现这个之后,在测试时我发现在释放一两个 block 之后,如果我尝试分配一个 block ,程序
我的任务是用 C 编写一个程序。该程序应该能够检查参数并创建与我提供的参数一样大的数组。我必须用随机数填充数组。到目前为止工作正常。稍后我的任务是使用指针对数组进行排序。第一件事是我不太明白指针是如何
我对 C 很陌生(仍然)所以如果我误解了一些基本的东西,请耐心等待 我有一个简单的程序,它应该将文件作为字符串读取,然后将该字符串拆分成行 - 将结果存储到 n 个字符串数组中。但是,当我运行以下代码
我在工作中使用的应用程序之一遇到了一个奇怪且烦人的问题。该应用程序是用 C++ 编写的,当应用程序终止(主函数返回或调用 exit)时,它会因段错误而崩溃。段错误似乎是由 basic_string 类
我使用 python swig 包装的 C++ 库。在它的 __init__.py 文件中,它 sets在导入包含实现代码的共享对象文件之前,使用 dlopen 标志 RTLD_GLOBAL。 这会导
我在这里遇到了段错误。我很困惑。请帮帮我。 f1 和 y 都是结构体节点的指针。我想把 y 的左转 f1 右转。 #include #include struct node{
我有一个在公共(public)结构中声明的数组,如下所示: uint16_t *registers; 在一个函数中,我正在检索一个字符字符串(存储在缓冲区中,请参阅下面的代码),其中包含以逗号分隔的数
我正在用 C 实现二叉搜索树。下面的代码工作正常,只是当我尝试从树中删除子树时得到 SEGFAULT: 源代码: #include #include struct node { int dat
struct vehicle *add_vehicle(struct vehicle *v){ struct vehicle *newcar = (struct vehicle*)malloc
我正在使用链接列表实现符号表,代码工作正常,但代码中存在内存泄漏, 我有以下结构 struct node { char* pcKey; void* pvValue; struct node
我正在尝试将字符串复制到数组并打印它。它适用于第一个 for 循环,但第二次出现 seg 错误。 main (int argc, char *argv[]){ int argcIndex; cha
自从我用 C 编写代码已经一年了,但我不明白为什么会出现段错误 // Assume all imports are made int printAgain(double** array, int si
这是我的代码。编辑:调用者包含在底部。 该函数读取数据文件,确定有多少行和列,然后将数据存储到 data_array 中。 int getdata(double* *data_array, int*
我认为有两组代码是等效的,但一组会导致段错误,而另一组则不会。我真的很困惑为什么会这样...... 我想创建一个查找函数 此代码确实有效: MyPair *> dummy(x, NULL);
希望有人能提供帮助。我可以毫无错误地编译,我没有发现任何语法错误,但是当我运行它时,它崩溃了。在启动时调试段错误。全面披露,这是作业。我不是要找人来编写这个代码,只是看看我的问题和我现有的代码,也许会
我正在尝试在OpenMP中并行化相当大的for-loop。大约有20%的时间运行正常,但其余时间会因各种段错误而崩溃,例如: *** glibc detected *** ./execute: dou
我有一个模板类 ISingleton class ISingleton { public: static T* getInstance() { lock_guard g
我正在为使用 LibSVM 的 Android 构建 NDK 应用程序。我在 XCode 中为我的 mac 构建了一个等价物(都是 C++) 我发现 Mac 可以高速准确地处理我给它的非常大的特征向量
我在 ARM linux 平台上有一个由简单代码引起的非常奇怪的崩溃。问题是它很少重现(一天一次),另一个问题是它在实际上无法重现的地方崩溃。 让我们从 C++ 代码开始。线程函数执行此操作:
我有这段代码 int main() { int *b = new int(8); cout<<" &b = "<
我是一名优秀的程序员,十分优秀!