我和一位朋友目前正致力于c大学的基本多线程示例。我们应该用多线程缓冲区解决生产者/消费者问题。我们有一个使用互斥和条件变量的工作版本,但尝试使用信号量和互斥量解决这个 有三个主要问题。
问题1 :如果我们首先启动消费者,他有时会随机消耗无效的字符并崩溃。
问题2:如果我们首先启动生产者,他有时会在消费者启动之前不生成任何字符,这会导致问题1.
问题3:我们的制作人不会在消费者正在消费的缓冲区中每次插入后填充整个缓冲区,无论有多少生产者。
根据我们给出的伪代码示例至少问题2& 3不应该存在。我真的很感激任何答案,因为我现在无法找到错误。
消费者:
void *consumer_sem(void *args) {
printf("cons started\n");
char c;
while (cons_running) {
sem_wait(occupied);
pthread_mutex_lock(&mutex);
c = consume();
pthread_mutex_unlock(&mutex);
sem_post(free);
printf("consumer consumed %c\n\n", c);
sleep(2);
}
}
制片:
void *producer1_sem(void *args) {
printf("prod1 started\n");
char c;
int index=0;
while (prod1_running) {
c = lowercase[index];
index=next(index);
sem_wait(free);
pthread_mutex_lock(&mutex);
add(c);
pthread_mutex_unlock(&mutex);
sem_post(occupied);
printf("producer1 produced something!\n");
printf("%d elements in buffer\n\n",getElemsInBuffer());
sleep(3);
}
}
主:
sem_t *occupied, *free;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int main(void) {
occupied=sem_open("/occupied", O_CREAT, 0644, 0);
free=sem_open("/free", O_CREAT, 0644, BUFFER_SIZE);
//some unrelated code called
pthread_create(&thread_ids[0], NULL, producer1_sem, NULL);
pthread_create(&thread_ids[1], NULL, producer2_sem, NULL);
pthread_create(&thread_ids[2], NULL, consumer_cond, NULL);
}
答案 0 :(得分:0)
函数sem_open创建全局(名为)信号量,该信号量在调用sem_unlink()
之前一直存在。
当您第二次(以及进一步)运行程序时,sem_open
会重复使用已存在的信号量,并且不会重置其值。您可以轻松检测到:
// With O_EXCL opening already existed semaphore will fail.
occupied=sem_open("/occupied", O_CREAT | O_EXCL, 0644, 0);
if(occupied == SEM_FAILED) {
perror("Failed to create new semaphore");
exit(1);
}
实际上,当信号量仅由单个进程(但是多个线程)使用时,使用sem_init
初始化它就足够了:
sem_t occupied;
//...
int main()
{
sem_init(&occupied, 0 /* do not share between processes*/, 0 /* initial value*/);
//...
}
或者,您可以在尝试创建新信号量之前销毁旧信号量:
// Destroy the semaphore if it exists.
sem_unlink("/occupied");
// Create the semaphore again.
occupied = sem_open("/occupied", O_CREAT | O_EXCL, 0644, 0);
答案 1 :(得分:0)
我设置并运行(通过make
文件)发布的代码;
以下是我使用的makefile.mak
,我通过make -f makefile.mak
SHELL = /bin/sh
#
# macro of all *.c files
# (NOTE:
# (the following 'wildcard' will pick up ALL .c files
# (like FileHeader.c and FunctionHeader.c
# (which should not be part of the build
# (so be sure no unwanted .c files in directory
# (or change the extension
#
SRC := $(wildcard *.c)
#OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
#INC := $(SRC:.c=.h)
MAKE := /usr/bin/make
CC := /usr/bin/gcc
CP := cp
MV := mv
#LDFLAGS := -L/usr/local/lib
DEBUG := -ggdb3
CCFLAGS := $(DEBUG) -Wall -Wextra -pedantic -Wconversion -std=gnu11
#CPPFLAGS += =MD
LIBS :=
.PSEUDO: all
all: gestore personA personB
#
# link the .o files into the executable
# using the linker flags
# -- explicit rule
#
getstore: getstore.o
#
# ======= $(name) Link Start =========
$(CC) $(DEBUG) -o $@ $^ $(LIBS)
# ======= $(name) Link Done ==========
#
personA: personA.o
#
# ======= $(name) Link Start =========
$(CC) $(DEBUG) -o $@ $^ $(LIBS)
# ======= $(name) Link Done ==========
#
personB: personB.o
#
# ======= $(name) Link Start =========
$(CC) $(DEBUG) -o $@ $^ $(LIBS)
# ======= $(name) Link Done ==========
#
#
#create dependancy files -- inference rule
# list makefile.mak as dependancy so changing makfile forces rebuild
#
%.d: %.c
#
# ========= START $< TO $@ =========
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
# ========= END $< TO $@ =========
#
# compile the .c file into .o files using the compiler flags
# -- inference rule
#
%.o: %.c %.d
#
# ========= START $< TO $@ =========
$(CC) $(CCFLAGS) -c $< -o $@ -I.
# ========= END $< TO $@ =========
#
.PHONY: clean
clean:
# ========== CLEANING UP ==========
rm -f *.o
rm -f *.d
rm -f getstore
rm -f personA
rm -f personB
# ========== DONE ==========
# include the contents of all the .d files
# note: the .d files contain:
# <filename>.o:<filename>.c plus all the dependencies for that .c file
# I.E. the #include'd header files
# wrap with ifneg... so will not rebuild *.d files when goal is 'clean'
#
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif