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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:26:02+00:00 2026-05-18T23:26:02+00:00

When trying run this code: At first it prints Process some_id BEFORE enter for

  • 0

When trying run this code: At first it prints “Process some_id BEFORE enter” for each semaphor (2 times). Then it hangs. What is incorrect?

# include <sys/ipc.h>
# include <sys/sem.h>
# include <unistd.h>
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <iostream>

int seminit()
{
    key_t key = ftok("/bin", 1);
    int semid = semget (key, 1, IPC_CREAT | IPC_EXCL | 600);
    if(-1 == semid)
        if(EEXIST == errno)
            semid = semget(key, 1, 0);
    return semid;
}

void uninit(int semid)
{
        semctl(semid, 0, IPC_RMID);
}

void semlock(int semid)
{
    struct sembuf p_buf;
    p_buf.sem_num = 0;
    p_buf.sem_op = -1;
    p_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &p_buf, 1) == -1)
        printf("semlock failed: ERRNO: %d\n", errno);
}
void semunlock(int semid)
{
    struct sembuf v_buf;
    v_buf.sem_num = 0;
    v_buf.sem_op = 1;
    v_buf.sem_flg = SEM_UNDO;
    if(semop(semid, &v_buf, 1) == -1)
        printf("semunlock failed: ERRNO: %d\n", errno);
}
void some_function()
{
    int semid = seminit();
    pid_t pid = getpid();
    printf("Process %d BEFORE enter\n", pid);
    semlock(semid);
    printf("Process %d IN Critical section\n", pid);
    sleep(10);
    semunlock(semid);
    printf("Process %d AFTER leave\n", pid);
    uninit(semid);
}

int main(int argc, char** argv) 
{
    for(int i = 0; i < 2; ++i)
        if(0 == fork())
            some_function();
    return (EXIT_SUCCESS);
}
  • 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-18T23:26:02+00:00Added an answer on May 18, 2026 at 11:26 pm

    Seems that only a child is generated (I think that is not intended), still, I believe there is a missing wait before the return of the main process, which means that the main process will end faster than the child process and let it “hanged” (this can be part of the issue but is not maybe the hole issue, check that for cycle before).

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

Sidebar

Related Questions

I'm trying to run this code but this error appear: Uncaught TypeError: string is
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
I am trying to run this code : exec myStoredProcedure Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier), 744,
I am trying to run this seemingly simple piece of code which is repeated
Trying to convert this c code into MIPS and run it in SPIM. int
I am trying to run my code so it prints cyclic permutations, though I
I'm trying to run this MPI Fortran code. There are several problems: 1) when
So I'm trying to run this code... my $filePath = $ARGV['0']; if ($filePath eq
I'm facing segmentation fault problem when trying to run this code: #include<stdio.h> #include<stdlib.h> int

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.