Jump to content

shared memory problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
c0d31

c0d31

    Newbie

  • Members
  • Pip
  • 3 posts
im trying to get the following code to share a bit of memory btn a parent and child process.get input from parent execute in child , store result in shared memory then display in parent.
all im getting thus far is error 218. what m i missin !



#include <iostream>

#include <string>

#include <unistd.h>

#include <sys/wait.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>


#define MAX_SEQUENCE 10


typedef struct {

	long fib_sequence [MAX_SEQUENCE];

	int sequence_size;

} shared_data;


using namespace std;


long fib( unsigned long n )

{

	if( n <= 1 )

	{		

		return 1;

	}

	else 

	{

		return fib(n-1)+fib(n-2);

	}

}


void printFib( shared_data * me , unsigned long t )

{

		int ct = 0;

	

		me->fib_sequence[ct] = 0;

	

		for( unsigned long i = 0; i <= t ; i++ )

		{

		ct++;

		me->fib_sequence[ct] = fib( i );

		}


}


int main(int argc, char** argv)

{

	pid_t pID,wpi;

	int status,seg_id;

	shared_data * shared_mem;

	

	int vic = atol(argv[1]);

	

	if( (vic < 0) || (vic > MAX_SEQUENCE) )

	{

		cout<<"Number Error" <<endl;

		exit(1);

	}

	

	seg_id = shmget(IPC_PRIVATE,sizeof(shared_data), 0644 | IPC_CREAT );

	

	shared_mem = ( shared_data * ) shmat( seg_id , NULL , 0 );

	

	shared_mem->sequence_size = vic;

	

	pID = fork();


	if( pID == 0 )

	{

		printFib( shared_mem , vic);

	}

	else

	{

	  wpi = waitpid( pID,&status,WNOHANG);

	  

      if( !WIFEXITED(status) )

      {

         cerr << "waitpid() exited with an error: Status= " 

              << WEXITSTATUS(status) << endl;

      }

      else if( WIFSIGNALED(status) )

      {

         cerr << "waitpid() exited due to a signal: " 

              << WTERMSIG(status)    << endl;

      }

      else

      {

		  cout << shared_mem->fib_sequence <<endl;

		  shmdt(shared_mem);

		  shmctl(seg_id,IPC_RMID,NULL);

	  }		

	}

	

	return 0;

}




#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I think this is an operating system design book problem...

I don't know what does this error mean. But what does this line means
cout << shared_mem->fib_sequence <<endl;
I guess you are gonna print the address of the shared_mem->fib_sequence

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I just tell you a simple way for parent process. Parent process can just wait and print the fibonacci series.

if(pid > 0) {
wait(NULL);
//print the fibonacci series
//detach the shared memory
}

Then, for detaching the shared memory, it should be
shmdt((void *)shared_mem); 


#4
c0d31

c0d31

    Newbie

  • Members
  • Pip
  • 3 posts
thanks veda , that worked ! yeah its an OS qn , its usually the bits i cant see after 10 coffes and an all-nighter that get ya :)

#5
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Make sure, you do all kinds of error checking... Specially for shared memory and creating a child process....

#6
c0d31

c0d31

    Newbie

  • Members
  • Pip
  • 3 posts
will do , thks again :thumbup:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users