草庐IT

c - 进程间信号量有时无法按预期工作

coder 2023-06-21 原文

我有以下 C 代码,其中以 sm 为前缀的变量由两个进程 proc1proc2 共享。因此,信号量也是共享的。这段代码被反复调用。所以如果我说之前的值,那意味着之前迭代的值。

我注意到在我的程序中 proc1 有时会通过 sem_wait( sem_f2l )proc2 不会执行 sem_post( sem_f2l )。我注意到这一点是因为 sm_value_proc1sm_value_proc2 在我的程序中应该具有相同的值,它们确实如此,正如 printfs 和 >>>。但是,带有 的 printf 有时会显示不同的值。差异是由于 proc1 打印了 sm_value_proc2 的先前值,因为 proc1 神秘地有时不等待 sm_f2lproc2 发布。

知道这里出了什么问题吗?

// semaphores are initialized like this -> sem_init( sm_l2f, 1, 0 );
// Note that sm_l2f and sm_f2l are pointers to sem_t

// sm_condition is assigned here by proc1

if ( is_proc1 )
{
    sem_post( sm_l2f );
    // proc2_value should be updated by now here, but sometimes sem_wait
    //  passes without waiting for proc2 to post sm_f2l!
    sem_wait( sm_f2l );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();
        printf( ">>> proc1 value = %u!\n", sm_value_proc1 );

        // If sem_wait and sem_post are working properly, printf will print
        //  the same value for sm_value_proc1 and sm_value_proc2 here, which it 
        //  sometimes doesn't, as the previous value of 
        //  sm_value_proc2 is printed.  
        printf( "<<< proc1 value = %u, proc2 value = %u, barrier_no = %d!\n",
                sm_value_proc1, sm_value_proc2, barrier_no[tid] );
    }
}
else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc2 = calc_value();
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

最佳答案

也许这是问题中的复制/粘贴错误(您正在使用真实代码中的复制/粘贴,对吧?),但看起来您在 proc2 的处理中有一个错误:

// ....

else // is proc2
{
    sem_wait( sm_l2f );
    if ( sm_condition )
    {
        sm_value_proc1 = calc_value();  // <---  this should be assigning to
                                        //       sm_value_proc2
        printf( ">>> proc2 value = %u!\n", sm_value_proc2 );
    }
    sem_post( sm_f2l );
} 

话说回来,也许是实际代码中的复制/粘贴错误?

此外 - 不要忘记 sem_wait() 可以因信号解除阻塞。

关于c - 进程间信号量有时无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8579672/

有关c - 进程间信号量有时无法按预期工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  8. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  9. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  10. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

随机推荐