What im trying to do is use shared memory and increment a value in it so that i can test to see how many times i get the actual number i was looking for which is 4000000 or more/less. Im using 2 forks so that i have 2 child processes creating the additions but for some reason it seems to do things correctly but then it just doesnt stop.
Here is an example of the output i get. it would appear that the first 4 lines are what im trying to create but it just keeps going. Ive tried to figure out whats going on but i dont really understand what is going on between the parent and child processes if anyone could give some input that would be great thanks in advance.

Hello from Child 8496
Hello from Child 8497
Hello from Parent
counter: 4084001
Hello from Child 8496
Hello from Parent
counter: 6084001
Hello from Child 8495
Hello from Child 8498
Hello from Parent
counter: 6306803
Hello from Child 8495
Hello from Parent
counter: 8306803

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

typedef struct
   {
   int value;
   }  shared_mem;

   shared_mem   *counter;

   /* This fcn should increase the value of the shared variable "counter" all the way
   up to 2000000 */

   process1()
    {
    		int x;
			for (x=0;x<2000000;x++)
 			counter->value = counter->value+1;
    }

   /* This fcn should also increase the value of the shared memory variable "counter" all the way
   up to 2000000 */

   process2()
    {
    		int y;
			for (y=0;y<2000000;y++)
			counter->value = counter->value+1;
    }

/*		The Main Body					*/

 main()
  {
		key_t		key = IPC_PRIVATE;  /* shared memory key */
		int		shmid;      /* shared memory ID */
		shared_mem	*shmat1;   /* function to allocate shared memory */
		int		pid1;	    /* process id for child1 */
		int		pid2;	    /* process id for child2 */

		
		/* attempts to attach to an existing memory segment	*/

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

		/*attempts the shared memory segment	*/

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

		/*initializing shared memory to 0 */
		 
		 counter->value = 0;
		
			
			 
		/* fork process one here */
			
			
			fork();
			pid1 = getpid();
			
		   //call process1 to increase shared memory counter
			process1();
			fprintf(stderr,"Hello from Child %d\n",pid1);
			kill(pid1);
			
		/* fork process two here */
		
		
			fork();
			pid2 = getpid();
			
		   //call process2 to increase shared memory counter
			process2();
			fprintf(stderr,"Hello from Child %d\n",pid2);
			kill(pid2);
			
		/* parent waits (use wait();) for child processes to finish. */
			
			//wait for child processes to be done
			wait(pid1);
			wait(pid2);
			fprintf(stderr,"Hello from Parent\n");
			
			
		/* parent process reports value of counter */
		
		fprintf(stderr,"counter: %d\n",counter->value);
		
		/* parent process exits safely */
		exit(1);
		
		
		
		   /*deallocate shared memory */
		      if(shmctl(shmid, IPC_RMID, (struct shmid_ds *)0)== -1){
			    perror("shmctl");
			  exit(-1);
		     }
		exit(0);

	}

Recommended Answers

All 2 Replies

In order to use shared memory with more than one process or thread you have to implement some way to lock the share memory so that no more than one process/thread is accessing it at any given instance. Semaphores are often used for that purpose. Read some of these links, ignoring those that are for MS-Windows because they will not be relevent to *nix system.

So where's the if / else logic around your fork calls?

Because I can't see how you can possibly get the output, from the code you supply.

I mean

/* fork process one here */
			
			
			fork();
			pid1 = getpid();
			
		   //call process1 to increase shared memory counter
			process1();
			fprintf(stderr,"Hello from Child %d\n",pid1);
			kill(pid1);

This code runs for BOTH the parent and the child.
Then both the parent AND the child die with the kill(pid).
And that's it - end of program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.