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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:36:09+00:00 2026-05-20T09:36:09+00:00

I have a code where I am working on a set of 3 semaphores.

  • 0

I have a code where I am working on a set of 3 semaphores. I have a two sets
struct sembuf wait[3],signal[3];

I have initialized each one of them. wait is initialized to -1 and signal is initialized to 1

I then set their values as 2 using the function semctl(semid,0,SETALL,2); which works successfully. I then check if their values are set, and they are set.

Then I do semop(semid,&wait[1],2); . this should wait on both the semaphores and decrease their values. So I expect the values of three semaphores to be 2,1,1 by this time, but to my surprise it decreased the value of 1st semaphore twice and I see the values as 0,2,2.

Can anyone tell me why is this happening.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>

int main(int argc,char *argv[]){

    key_t key1 = 12345;
    int semid;
    unsigned short *semval;

    struct sembuf wait[3],signal[3];
    semval = (unsigned short*) malloc(sizeof(unsigned short) * 3);

    wait[0].sem_num = 0;
    wait[0].sem_op = -1;
    wait[0].sem_flg = SEM_UNDO;

    signal[0].sem_num = 0;
    signal[0].sem_op = 1;
    signal[0].sem_flg = SEM_UNDO;

    wait[1].sem_num = 0;
    wait[1].sem_op = -1;
    wait[1].sem_flg = SEM_UNDO;

    signal[1].sem_num = 0;
    signal[1].sem_op = 1;
    signal[1].sem_flg = SEM_UNDO;

    wait[2].sem_num = 0;
    wait[2].sem_op = -1;
    wait[2].sem_flg = SEM_UNDO;

    signal[2].sem_num = 0;
    signal[2].sem_op = 1;
    signal[2].sem_flg = SEM_UNDO;

    semid = semget(key1,3,IPC_CREAT);
    printf("ALLOCATING THE SEMAPHORES = %s\n",strerror(errno));

    semval[0] = semval[1] = semval[2] = 2;
    semctl(semid,0,SETALL,semval);
    printf("SETTING SEMAPHORE VALUES = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("Initialized Semaphore values : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semop(semid,&wait[1],2);
    printf("WAITING ON SEMAPHORES 2 AND 3 = %s\n",strerror(errno));

    semctl(semid,0,GETALL,semval);
    printf("VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : %d--%d--%d\n",semval[0],semval[1],semval[2]);

    semctl(semid,0,IPC_RMID);
    printf("SEMAPHORE REMOVED = %s\n",strerror(errno));
    return 0;
}

and here is the output

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc -g -o sem3 sem3.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./sem3
ALLOCATING THE SEMAPHORES = Success
SETTING SEMAPHORE VALUES = Success
Initialized Semaphore values : 2--2--2
WAITING ON SEMAPHORES 2 AND 3 = Success
VALUES AFTER WAITING ON SEMAPHORES 2 AND 3 : 0--2--2
SEMAPHORE REMOVED = Success

Finally managed to write a blog on it.
http://systemsdaemon.blogspot.com/2011/02/system-v-semaphores-for-babies.html

  • 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-20T09:36:10+00:00Added an answer on May 20, 2026 at 9:36 am

    You used sem_num=0 in all of your parameters.

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

Sidebar

Related Questions

I have a code which is working fine and displaying some set of values
I have the code below working. How do I read and set the item
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
I used to have this code working with my Tomcat server: HttpRequestBase targetRequest =
I’m trying to issue web requests asynchronously. I have my code working fine except
this seems to be weird but it really happened. I have this code working
I have working code of js, it shows different div for selected option. <html>
We have the following code working for a complex rails form with checkboxes. I'm
I have this JQuery code working however I need to know whether how to
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);

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.