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

  • Home
  • SEARCH
  • 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 98851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:16:31+00:00 2026-05-11T00:16:31+00:00

I have two process and a shared memory zone, my workflow is like this.

  • 0

I have two process and a shared memory zone, my workflow is like this. The process A write some data in the shared memory, after that it should wait and send a signal to other process B to start running. The process B should read some data from the shared memory do some stuff write the result, and send a signal to the process A to keep running, after this process B should wait.

Can anyone plese provide an example or a place where I can find how can I stop a process and how can I start running again the process?. I am working in Linux and C++.

I already have semaphores, but the thing that I do not like, it is that one process is stop a bunch of seconds reading all the time from the shared memory, until it detects that it can run. That’s why I was thinkin only in send a signal in the right moment

Update with the solution

I selected the answer of stefan.ciobaca as favourite because is a complete solution that it works and it has a very good explanation. But in all of the other answers there are other interesting options.

  • 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. 2026-05-11T00:16:32+00:00Added an answer on May 11, 2026 at 12:16 am

    Here is a proof-of-concept of how it can be done:

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <unistd.h> #include <assert.h>  typedef void (*sighandler_t)(int);   #define SHM_SIZE 8  /* size of shared memory: enough for two 32 bit integers */  volatile int cancontinue = 0;  void halt(char *err) { perror(err); exit(1); } void handler(int signum) { assert(signum == SIGUSR1); cancontinue = 1; }  int main(void) {   key_t key;   int id;   int *data;   pid_t otherpid;    printf('Hi, I am the %s process and my pid is %d\n',  #ifdef PRODUCER_MODE   'writer' #else   'reader' #endif , getpid());   printf('Please give me the pid of the other process: ');   scanf('%d', &otherpid);    // get a pointer to the shared memory   if ((key = ftok('test_concur.c', 'R')) == -1) halt('ftok');   if ((id = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) halt('shmget');   if ((data = shmat(id, (void *)0, 0)) == (int *)(-1)) halt('shmat');    sighandler_t oldhandler = signal(SIGUSR1, handler);    while (1) { #ifdef PRODUCER_MODE     printf('Enter two integers: ');     scanf('%d %d', data, data + 1);      printf('Sending signal to consumer process\n');     kill(otherpid, SIGUSR1);      printf('Waiting for consumer to allow me to continue\n');     while (!cancontinue);     cancontinue = 0;      if (*data + *(data + 1) == 0) { printf('Sum was 0, exiting...\n'); break; } #else     printf('Waiting for producer to signal me to do my work\n');     while (!cancontinue);     cancontinue = 0;      printf('Received signal\n');     printf('Pretending to do a long calculation\n');     sleep(1);     int sum = *data + *(data + 1);     printf('The sum of the ints in the shared memory is %d\n', sum);      printf('Signaling producer I'm done\n');     kill(otherpid, SIGUSR1);      if (sum == 0) break; #endif   }    signal(SIGUSR1, oldhandler);    /* detach from the segment: */   if (shmdt(data) == -1) {     perror('shmdt');     exit(1);   }    // don't forget to remove the shared segment from the command line with    // #sudo ipcs   // ... and look for the key of the shared memory segment    // #ipcrm -m <key>    return 0; } 

    The above program is actually two programs, a consumer and a producer, depending on how you compile it.

    You compile the producer by making sure that the PRODUCER_MODE macro is defined:

     # gcc -Wall -DPRODUCER_MODE -o producer test_concur.c 

    The consumer is compiled without defining the PRODUCER_MODE macro:

     # gcc -Wall -o consumer test_concur.c 

    The consumer and producer share some global memory (8 bytes pointed to by data); the producer’s role is to read two 32-bit integers from stdin and write them to the shared memory. The consumer reads integers from the shared memory and computes their sum.

    After writing the data to shared memory, the producer signals to the consumer (via SIGUSR1) that it may begin the computation. After the computation is done, the consumer signals to the producer (via SIGUSR1 again) that it may continue.

    Both processes stop when the sum is 0.

    Currently, each program begins by outputing its pid and reading from stdin the other program’s pid. This should probably 😀 be replaced by something smarter, depending on exactly what you are doing.

    Also, in practice, the ‘while (!cancontinue);’-like loops should be replaced by something else :D, like semaphores. At least you should do a small sleep inside each loop. Also, I think you do not truly need shared memory to solve this problem, it should be doable using message-passing techniques.

    Here is an example session, showed in parallel:

         # ./producer                                            # ./consumer     Hi, I am the writer process and my pid is 11357         Hi, I am the reader process and my pid is 11358     Please give me the pid of the other process: 11358      Please give me the pid of the other process: 11357     Enter two integers: 2                                   Waiting for producer to signal me to do my work     3     Sending signal to consumer process                      Received signal     Waiting for consumer to allow me to continue            Pretending to do a long calculation                                         ... some times passes ...                                                             The sum of the ints in the shared memory is 5                                                             Signaling producer I'm done     Enter two integers: 0                                   Waiting for producer to signal me to do my work     0     Sending signal to consumer process                      Received signal     Waiting for consumer to allow me to continue            Pretending to do a long calculation                                         ... some times passes ...                                                             The sum of the ints in the shared memory is 0                                                             Signaling producer I'm done     Sum was 0, exiting...                                  

    I hope this helps. (when you run the programs, make sure the file test_concur.c exists (it’s used to establish the shared memory key (ftok function call)))

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

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 80k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not sure what you mean. If you want to… May 11, 2026 at 4:14 pm
  • Editorial Team
    Editorial Team added an answer <many-to-one/> May 11, 2026 at 4:14 pm
  • Editorial Team
    Editorial Team added an answer You need to tell it to use ExpandableObjectConverter (or a… May 11, 2026 at 4:14 pm

Related Questions

I have two processes one will query other for data.There will be huge amount
New to Linux programming in general. I am trying to communicate with a kernel
In socket programming, you create a listening socket and then for each client that
I've got an object that handles in memory caching for my data access layer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.