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 332679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:53:21+00:00 2026-05-12T09:53:21+00:00

In a graduate class, we’ve had to use semaphores to accomplish work with threads.

  • 0

In a graduate class, we’ve had to use semaphores to accomplish work with threads.

We were directed to use sem_init along with a bunch of other sem_* procedure but we were not given much information about the details of each of these sem_* methods.

The prototype (and header file) of sem_init is the following:

#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

but I don’t understand what the pshared value is used for. According to opengroup.org:

If the pshared argument has a non-zero
value, then the semaphore is shared
between processes; in this case, any
process that can access the semaphore
sem can use sem for performing
sem_wait(), sem_trywait(), sem_post(),
and sem_destroy() operations.

but I guess I don’t understand the difference between say 1,2, 10, 25, 50000, etc. I think it is saying that if the value is 0 then the semaphore is not shared. (But then, what is the point?)

How do I appropriately use this pshared parameter?

  • 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-12T09:53:21+00:00Added an answer on May 12, 2026 at 9:53 am

    The GLIBC version of sem_init (what you get if you man sem_init on Linux) has this to say:

    “The pshared argument indicates whether this semaphore is to be
    shared between the threads of a process, or between processes.”

    So pshared is a boolean value: in practice meaningful values passed to it are false (0) and true (1), though any non-0 value will be treated as true. If you pass it 0 you will get a semaphore that can be accessed by other threads in the same process — essentially an in-process lock. You can use this as a mutex, or you can use it more generally for the resource-counting properties of a semaphore. Arguably if pthreads supported a semaphore API you wouldn’t need this feature of sem_init, but semaphores in Unix precede pthreads by quite a bit of time.

    It would be better if the boolean was some kind of enumeration (e.g. SEM_PROCESS_PRIVATE vs SEM_PROCESS_SHARED), because then you wouldn’t have had this question, but POSIX semaphores are a fairly old API as these things go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my graduate class on compiler construction we've been introduced to the concept of
I have to work on a project for my under graduate final year. I
I am teaching a graduate level Python class at the University of Paris, and
I got my theme for graduate work (I dont know if this is the
Error is here: vector<Graduate *> graduates; graduates.push_back(new AliceUniversity(identifier,id,salary,average)); Grandparent class: Graduate::Graduate(char identifier, long id,
I am developing my graduate project, using GWT. I use TOMCAT as the applciation
I an a graduate student of nuclear physics currently working on a data analysis
I am a graduate student of physics and I am working on writing some
I am a graduate student with many scripts, bibliography data in bibtex, thesis draft
I have been taking a few graduate classes with a professor I like alot

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.