Fortran 信号量

发布于 2024-12-21 23:07:28 字数 873 浏览 5 评论 0原文

谁能帮忙用 Fortran 写一个信号量函数?我有多个进程正在运行,我必须使用信号量来同步它们。对于 C++ 等可以找到这样的代码,但我找不到 fortran 的任何这样的代码。

如果我可以从 Fortran 代码调用 C/C++ 函数,那也足够了,因为 C++ 信号量函数已经存在。

PS:(附加说明)这是适用于 C++ 的代码。我有一些 Fortran 应用程序(作为标准基准测试的一部分),但没有信号量代码来同步它们。

  int get_semaphore ()
  {
   int sem_id;
   sem_id = semget(SEM_ID, 1, IPC_CREAT | 0666);
if (sem_id == -1) {
    perror("get_semaphore: semget");
    exit(1);
}
return sem_id;
}


int set_semaphore (int sem_id, int val)
  { 
   return semctl(sem_id, 0, SETVAL, val);
  }

void decrement_semaphore (int sem_id)
{
 struct sembuf sem_op;
 sem_op.sem_num = 0;
 sem_op.sem_op = -1;
sem_op.sem_flg = 0;
semop(sem_id, &sem_op, 1);
 }

 void wait_semaphore (int sem_id)
{   
 struct sembuf sem_op;
sem_op.sem_num = 0;
sem_op.sem_op = 0;
sem_op.sem_flg = 0;
  semop(sem_id, &sem_op, 1);
  } 

感谢您提前的帮助。

Can anyone help with writing a semaphore function in fortran? I have multiple processes running and I have to synchronize them using semaphore. Such a code could be found for C++ etc., but I could not find any such code for fortran.

If I could call C/C++ function from fortran code, that would also suffice, since C++ semaphore function is already there.

PS: (Additional Explanation) Here is the code which works for C++. I have some fortran applications (as part of standard benchmark) but no semaphore code to synchronize them.

  int get_semaphore ()
  {
   int sem_id;
   sem_id = semget(SEM_ID, 1, IPC_CREAT | 0666);
if (sem_id == -1) {
    perror("get_semaphore: semget");
    exit(1);
}
return sem_id;
}


int set_semaphore (int sem_id, int val)
  { 
   return semctl(sem_id, 0, SETVAL, val);
  }

void decrement_semaphore (int sem_id)
{
 struct sembuf sem_op;
 sem_op.sem_num = 0;
 sem_op.sem_op = -1;
sem_op.sem_flg = 0;
semop(sem_id, &sem_op, 1);
 }

 void wait_semaphore (int sem_id)
{   
 struct sembuf sem_op;
sem_op.sem_num = 0;
sem_op.sem_op = 0;
sem_op.sem_flg = 0;
  semop(sem_id, &sem_op, 1);
  } 

Thanks for the help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-12-28 23:07:28

OpenMP 在术语“锁”下提供信号量。通常不会使用这些,因为 OpenMP 提供了更高级别的任务结构,但如果您想自己做,这可能是使用 Fortran 进行显式锁定/解锁的一种方法。

PS 如果您想通过调用已有的 C 代码来从 Fortran 中执行此操作,则可以使用 Fortran 2003 的 ISO C 绑定来完成。这里有很多关于其工作原理的问题/答案。我为您的 Fortran 程序起草了声明以匹配 C 例程。它们告诉 Fortran 编译器如何使用 C 编译器的调用约定来调用 C 例程。这些未经测试,可能需要调试:

use iso_c_binding

interface semaphore_stuff

   function get_semaphore () bind (C, name="get_sempaphore")
      import
      integer (c_int) :: get_semaphore
   end function get_semaphore ()

   function set_semaphore (sem_id, int val) bind (C, name="get_sempaphore")
      import
      integer (c_int) :: set_semaphore
      integer (c_int), intent (in), value :: sem_id
      integer (c_int), intent (in) value :: val
   end function set_semaphore

   subroutine decrement_semaphore (sem_id) bind (C, name="decrement_semaphore")
      import
      integer (c_int), intent (in), value :: sem_id
   end subroutine decrement_semaphore

   subroutine wait_semaphore (sem_id) bind (C, name="wait_semaphore")
      import
      integer (c_int), intent (in), value :: sem_id
   end subroutine wait_semaphore

end interface semaphore_stuff

OpenMP provides semaphores under the term "lock". Normally these aren't used since OpenMP provides higher level tasking constructs, but if you want to do it yourself, that could be a way to do explicit locking/unlocking with Fortran.

P.S. If you want to do it from Fortran by calling the C code that you already have, that could by done by using the ISO C Binding of Fortran 2003. There are many questions/answers here about how this works. I drafted declarations for your Fortran program to match the C routines. These tell the Fortran compiler how to call the C routines using the calling conventions of the C compiler. These are untested and may need debugging:

use iso_c_binding

interface semaphore_stuff

   function get_semaphore () bind (C, name="get_sempaphore")
      import
      integer (c_int) :: get_semaphore
   end function get_semaphore ()

   function set_semaphore (sem_id, int val) bind (C, name="get_sempaphore")
      import
      integer (c_int) :: set_semaphore
      integer (c_int), intent (in), value :: sem_id
      integer (c_int), intent (in) value :: val
   end function set_semaphore

   subroutine decrement_semaphore (sem_id) bind (C, name="decrement_semaphore")
      import
      integer (c_int), intent (in), value :: sem_id
   end subroutine decrement_semaphore

   subroutine wait_semaphore (sem_id) bind (C, name="wait_semaphore")
      import
      integer (c_int), intent (in), value :: sem_id
   end subroutine wait_semaphore

end interface semaphore_stuff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文