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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:16:48+00:00 2026-06-18T06:16:48+00:00

So the program below accesses a piece of shared memory at location shmid and

  • 0

So the program below accesses a piece of shared memory at location “shmid” and attaches it to pointer “total.” A child process is then created that accesses and modifies this piece of memory and then once complete, another child process is created and does the same, and then a 3rd child. Once these 3 children have finished modifying the value and finished executing, the parent process releases the shared memory and then the program exits. I was under the impression that this “shmctl” block would have the desired effect, but it doesn’t seem to.

if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
{
  perror ("shmctl");
 exit (-1);
}

I don’t believe it does, because following this block I have the following which during execution printed out the same value as before. Doesn’t this indicate that the pointer is still accessing the same bit of memory, or am I mistaken?

printf("value after memory release:%d\n", total->value);

Thank you for your input!

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>

/* change the key number */
#define SHMKEY ((key_t) 5600) //7890

typedef struct
{
  int value;
} shared_mem;

shared_mem *total;

/*----------------------------------------------------------------------*
 * This function increases the value of shared variable "total"
 *  by one all the way to 100000
 *----------------------------------------------------------------------*/

void process1 ()
{
  int k = 0;

  while (k < 100000)
  {
      k++;
      total->value = total->value + 1;
  }
  printf ("From process1 total = %d\n", total->value); //process 1 prints out total and  returns to main() function
}

/*----------------------------------------------------------------------*
 * This function increases the vlaue of shared memory variable "total"
 *  by one all the way to 170000
 *----------------------------------------------------------------------*/

void process2 ()
{
  int k = 0;

  while (k < 170000)
  {
  k++;
  total->value = total->value + 1;
  }
  printf ("From process2 total = %d\n", total->value); //process 2 prints out total and returns to main() function
}
/*----------------------------------------------------------------------*
 * This function increases the vlaue of shared memory variable "total"
 *  by one all the way to 200000
 *----------------------------------------------------------------------*/

void process3 ()
{
  int k = 0;

  while (k < 200000)
  {
    k++;
    total->value = total->value + 1;
  }
  printf ("From process3 total = %d\n", total->value); //process 3 prints out total and returns to main() function
}

/*----------------------------------------------------------------------*
 * MAIN()
 *----------------------------------------------------------------------*/

int main()
{
  int shmid;
  int pid1;
  int pid2;
  int pid3;
  int ID;
  int status;

  char *shmadd;
  shmadd = (char *) 0;

/* Create and connect to a shared memory segmentt*/

if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0)
{
  perror ("shmget");
  exit (1);
}


if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1)
{
  perror ("shmat");
  exit (0);
}

total->value = 0;

if ((pid1 = fork()) == 0) //first child created
{
  process1(); //first child process begins
}

while((ID = wait(&status)) != -1) //parent loops until 1st child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished


if ((pid1 != 0) && ((pid2 = fork()) == 0)) //second child created
{
  process2(); //second child process begins
}   

while((ID = wait(&status)) != -1) //parent loops until 2nd child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished

if ((pid1 != 0) && (pid2 != 0) && ((pid3 = fork()) == 0)) //third child created
{
  process3(); //third child process begins
}

while((ID = wait(&status)) != -1) //parent loops until 3rd child is finished
printf("child %d is finished\n", ID); //parent prints out returned value after child is finished



if ((pid1 != 0) && (pid2 != 0) && (pid3 != 0)) 
{
  if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
  {
 perror ("shmctl");
 exit (-1);
  }

  printf ("\t\t  End of Program.\n"); //prints after all children have finished

  printf("value after memory release:%d\n", total->value);
}
return 0;
} 

/***** Note:  loop for parent to wait for child processes to finish and print ID of each child*****/
  • 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-18T06:16:49+00:00Added an answer on June 18, 2026 at 6:16 am

    From the shmctl man page:

    IPC_RMID

    Mark the segment to be destroyed. The segment will only actually be
    destroyed after the last process detaches it (i.e., when the
    shm_nattch member of the associated structure shmid_ds is zero). The
    caller must be the owner or creator, or be privileged. If a segment
    has been marked for destruction, then the (nonstandard) SHM_DEST flag
    of the shm_perm.mode field in the associated data structure retrieved
    by IPC_STAT will be set.

    The caller must ensure that a segment is eventually destroyed;
    otherwise its pages that were faulted in will remain in memory or
    swap.

    i.e. your code marks the segment for destruction, but does not detach it until program exit.

    If you want to detach it earlier, you can call shmdt(total);. If you do this before that last printf, the printf is likely to cause a seg fault.

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

Sidebar

Related Questions

Below program contains two show() functions in parent and child classes, but first show()
The example program below compiles two in-memory assemblies. The first compilation works fine. The
The program below prints the following data: Wed,Jun,13,10:37:34,2012,759,41,0,30,10,0,0,1 Wed,Jun,13,10:38:34,2012,767,33,0,25,6,0,0,2 Wed,Jun,13,10:39:34,2012,758,42,0,32,10,0,0,0 Wed,Jun,13,10:40:35,2012,758,42,0,29,11,0,0,2 Wed,Jun,13,10:41:35,2012,761,39,0,34,5,0,0,0 Wed,Jun,13,10:42:35,2012,769,31,0,22,6,0,0,3 Wed,Jun,13,10:43:35,2012,754,46,0,29,17,0,0,0
The program below prints: my name is:null my name is:null Someclass static init AFAIK
This program below moves the last (junior) and the penultimate bytes variable i type
(In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a
The below program calculates 2 raised to the power n without using any loop,runtime
In the below program,i am not getting values from printf . #include<stdio.h> int main()
In this below program i am printing the contents of the div when i
I have the below program that will move files from one directory to other.

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.