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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:00:16+00:00 2026-06-07T22:00:16+00:00

While trying to implement the Named pipe (e.g. two independent unrelated processes that are

  • 0

While trying to implement the Named pipe (e.g. two independent unrelated processes that are using the same shared memory) I keep reading that I’d to use pthread_atfork and atexit.

I’m fully agreeing with the use of mutexes and semaphores — using them we can decide when process A would read/write and when process B would read/write.

But for what reason would I want to use pthread_atfork and threads for that?

EDIT:

An example where not using semaphores would cost dearly :

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <assert.h>

 // Simple busy-wait loop to throw off our timing.
 void busywait(void)
 {
     clock_t t1 = times(NULL);
     while (times(NULL) - t1 < 2);
 }


 int main(int argc, char *argv[])
 {
     const char *message = "Hello World\n";
     int n = strlen(message) / 2;

     pid_t pid = fork();
     int i0 = (pid == 0) ? 0 : n;
     int i;

     for (i = 0; i < n; i++) {
         write(1, message + i0 + i, 1);
         busywait();
     }
 }
  • 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-07T22:00:19+00:00Added an answer on June 7, 2026 at 10:00 pm

    Roughly, your pipe-alike structure should be located in shared memory (e.g. obtained by shm_open) and contain the following members:

    • One mutex controlling all access to the structure.
    • One condition variable for signaling a waiting peer.
    • A buffer of some fixed size.
    • Two offsets into the buffer for the current read and write position.

    The read function should basically obtain the mutex, then check whether there’s data available for read, and if not, wait on the condition variable and loop rechecking for data. Once data is found, you’ll need to signal the condition variable if the read made it possible to fit more data in the buffer to wake up a potentially-waiting writer. Copy the data read into a caller-provided buffer then unlock the mutex.

    The write function should basically obtain the mutex, then check whethere there’s space in the buffer left to write. If not, it should wait on the condition variable and loop rechecking for space available to write. Once space is found, it should copy the data into the buffer, signal the condition variable to wake any reader that might be waiting for data, and unlock the mutex.

    Both the mutex and condition variable need to be created with the process-shared attribute it you’ll be using the “pipe” for communication between processes (not just threads in the same process). You probably also need to think about whether you want to support multiple readers/writers and whether you need single-signal or broadcast-signal semantics for the condition variables. There are a lot of ways to optimize the behavior, but the above outline should give you a general starting point.

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

Sidebar

Related Questions

Hey guys, I'm currently trying to implement a function using C that takes in
While in search of trying to implement unique key validations for my db using
My problem is that while trying to implement usage of the Autoload feature, I
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I'm new to AS3 and I'm getting this error while trying to implement OO
While trying to troubleshoot a problem I'm having with a project, I noticed that
While trying to find an answer to Android Jasper Reporting I found out that
While trying to add value into foreign key field, if same data doesn't exist
While trying to implement the second answer to a previous question , I am
I am received the following error while trying to implement a C# extension function

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.