Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:06:15+00:00 2026-05-27T14:06:15+00:00

Can anyone help with writing a semaphore function in fortran? I have multiple processes

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:06:15+00:00Added an answer on May 27, 2026 at 2:06 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help? I have been designing a site using Javascript but the rest
Can anyone help? I have been using the entity framework and its going well
can anyone help? I have a simple html file which i am filling via
Can anyone help? I have an issue with calling a asp.net webservice from jquery..
Can anyone please help me in writing rspec for the following method The write_entry_to_xml
Can anyone help me with why this JavaScript is not writing to the definition
can anyone help? I have just started asp.net mvc and its all working.. entering
Can anyone help me in writing the code for drawing arrow mark? I am
Can anyone help me in writing a regular expression on the following description? The
I need help writing a hash function. I don't really understand them too much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.