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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:38:40+00:00 2026-06-08T11:38:40+00:00

I’ve implemented a library that simulates pipe() system call , based on shared memory

  • 0

I’ve implemented a library that simulates pipe() system call , based on shared memory .

Now , the code works OK when I’m not using any fork() , e.g. without invoking any child processes .

My library needs to work with any given int main() program , so the basic problem here is that the modifications with the semaphores should be made with in the library , and not in a main program .

The library :

Here is the library :

static int flag = FALSE;
static int mutex_init = 0;
static pthread_mutex_t lock;

#define BUFFER 4096
int my_new_finish()
{
    return 1;  // always successful
}


void error_out(const char *msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}

Now , this library works OK when I’m not using main program that invoke fork() .

But , when I do use fork() , all hell brakes loose .

For example :

#include <stdio.h>
#include <stdlib.h>
int main()

{
    int spd, pid, rb;
    char buff[4096];
    my_new_init();

    if (my_new_fifo("tmp_shm_pipe",0666) < 0)
    {
        perror("my_new_fifo");
        exit(1);
    }

    if (fork()) 
    {
        spd = my_new_open("tmp_shm_pipe", O_RDONLY, 0600);
        if (spd < 0)
        {
            perror("PARENT: my_new_open");
            exit(1);
        }
        rb = my_new_read(spd, buff, sizeof(buff));
        if (rb > 0)
            write(1, buff, rb);
    }

    else
    {
        spd = my_new_open("tmp_shm_pipe", O_WRONLY, 0600);
        if (spd < 0)
        {
            perror("SON: my_new_open");
            exit(1);
        }
        my_new_write(spd, "hello world!\n", sizeof("hello world!\n"));
    }

    my_new_close(spd);
    my_new_un_link("tmp_shm_pipe");
    my_new_finish();

    return 0;
}

My questions are :

  1. How can I use semaphores with the above library , where I don’t “know” the main() program that I’d be given ?

  2. I’ve tried to put semaphores in the library (not in the main() program) but
    it didn’t work out good . Can you please explain how can I do that correctly ?

Remarks :

  1. Please pay attention that this main is just an example , and I could be given countless other main program.

  2. This is homework

Much appreciated

  • 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-06-08T11:38:42+00:00Added an answer on June 8, 2026 at 11:38 am

    In your function my_new_init you’d need to create a shared semaphone in shared memory – but have a guard around it so that it is only called once; this guard would typically be inside the library module using a module static (or class static) variable.

    sem_t *my_semaphone;
    static int init = 0;
    
    int my_new_init()
    {
        if (!init)
        {
            my_semaphone = mmap(NULL, sizeof *my_semaphone, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
            if (!sem_init(my_semaphone, 1, 1))
            {
                init = TRUE;
            }
            else
                perror("Sem_init");
        }
        return 1; // always successful
    }
    

    Then in my_new_read at the top:

    ssize_t my_new_read(int spd, void *buf, size_t count)
    {
        char array[4096];
        memset(array, '\0', 4096);
        ssize_t returnVal = 0;
    
        sem_wait(my_semaphone);    
    

    and in my_new_write release the semaphone after you’ve written something.

        sem_post(my_semaphone);
        return returnVal;
    

    The above may need improvement as it is possible that sem_wait will return before data is ready so it may be wise to use a control structure at the beginning of your shared memory segment.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.