的pthread条件和工艺终止 [英] pthread conditions and process termination

查看:136
本文介绍了的pthread条件和工艺终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进程共享的pthread条件(关联互斥)。如果一个进程在此条件下等待(使用pthread_cond_wait()的或那么pthread_cond_timedwait())被终止,会发生什么?在此条件仍然被其他进程使用吗?

I have a process-shared pthread condition (with associated mutex). What would happen if a process waiting on this condition (using pthread_cond_wait() or pthread_cond_timedwait()) gets terminated? Can this condition still be used by other processes?

在我的方案进程#1等待的状态,被终止。流程#2在某些时候发现它是唯一一个现在使用的条件,并调用pthread_cond_destroy()在

In my scenario process #1 waits on the condition and gets terminated. Process #2 at some point sees that it's the only one now using the condition and calls pthread_cond_destroy().

什么读音字看到的是,pthread_cond_destroy()在简单地挂起。有没有人遇到同样的问题?

What i m seeing is that pthread_cond_destroy() simply hangs. Has anyone encountered the same problem?

从pthread_cond_destroy的手册页()它说,破坏的一些线程仍在等待不确定的行为结果的条件。在我的情况下没有人再等待,当进程#2调用pthread_cond_destroy()在因等待进程#1被终止,但显然病情本身仍认为有一个等待的线程。

From the man page of pthread_cond_destroy() it says that destroying the condition on which some thread is still waiting results in undefined behavior. In my case nobody is waiting anymore when process #2 calls pthread_cond_destroy() because the waiting process #1 was terminated, but apparently the condition itself still thinks there is a waiting thread.

有没有解决这个问题的办法吗?

Is there any way around this problem?

编辑:

根据要求我寄样品程序(我扭转P1和放大器; P2这里):

Per request, i post the sample programs (i reversed p1 & p2 here):

p1.cpp:

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

struct MyCond {
    pthread_mutex_t m;
    pthread_cond_t c;
};

int main()
{
    pthread_mutexattr_t ma;
pthread_mutexattr_init(&ma);
pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED);

pthread_condattr_t ca;
pthread_condattr_init(&ca);
pthread_condattr_setpshared(&ca, PTHREAD_PROCESS_SHARED);

int fd = shm_open("/test_cond_p", O_RDWR|O_CREAT, 0666);
ftruncate(fd, sizeof(MyCond));

MyCond *c = (MyCond *)mmap(NULL, sizeof(MyCond),
    PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
//close (fd);

pthread_mutex_init(&c->m, &ma);
pthread_cond_init(&c->c, &ca);
printf("Inited MyCond, %x\n", c);

puts("Press Enter to continue");
fgetc(stdin);

    int r = pthread_cond_signal(&c->c);
    printf("After pthread_cond_signal, r=%d\n", r);

puts("Before pthread_cond_destroy");
r = pthread_cond_destroy(&c->c);
printf("After pthread_cond_destroy, r=%d\n", r);
r = pthread_mutex_destroy(&c->m);
printf("After pthread_mutex_destroy, r=%d\n", r);

munmap(c, sizeof(MyCond));
shm_unlink("/test_cond_p");

return 0;
}

p2.cpp:

p2.cpp:

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>

struct MyCond {
pthread_mutex_t m;
pthread_cond_t c;
};

int main()
{
int fd = shm_open("/test_cond_p", O_RDWR, 0666);

MyCond *c = (MyCond *)mmap(NULL, sizeof(MyCond),
    PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
//close (fd);

pthread_mutex_lock(&c->m);
puts("Before pthread_cond_wait");
int r = pthread_cond_wait(&c->c, &c->m);
printf("After pthread_cond_wait, r=%d\n", r);

munmap(c, sizeof(MyCond));
return 0;
}

P1运行,然后再运行P2中,调用pthread_cond_wait之前,按Ctrl-C它说了。然后preSS在P1的外壳Enter键。

Run p1 first, then run p2, after it says "before pthread_cond_wait", Ctrl-C it. Then press Enter in p1's shell.

起初,我是无法重现的窍门,但我有两个pthread_cond_destroy()在和pthread_mutex_destroy()返回EBUSY。

At first, i was not able to reproduce the hang, but i had both pthread_cond_destroy() and pthread_mutex_destroy() to return EBUSY.

但现在的杭再现,如果我们pthread_cond_destroy(之前调用调用pthread_cond_signal())(见上code)。

But now the hang reproduces if we call pthread_cond_signal() before pthread_cond_destroy() (see the code above).

推荐答案

似乎P2方法是永远等待条件变量,因为P1进程没有机会发送通知,按ctrl-C被终止。当你和其他人已经提到的pthread条件变量并不知道原来的进程终止。

It seems that the p2 process is waiting on the conditional variable eternally since the p1 process has no chance to send a notification being terminated by ctrl-c. As you and other people have already mentioned pthread conditional variable does not "know" about its original process termination.

如果您无法使用另外一个进程间通信的功能,仍然坚持共享互斥和条件变量,我曾经想捕捉信号。

If you cannot use another inter process communication features and still insist on shared mutex and conditional variable, I would think about trapping the signal.

这篇关于的pthread条件和工艺终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆