Results 1 to 2 of 2
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 ...
- 04-10-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
shared memory problem
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 !
Code:#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; }
- 04-11-2010 #2Just Joined!
- Join Date
- Apr 2010
- Posts
- 2
stuck a wait(NULL) and it works . any ideas why the wait bit isnt processing ptherwise?


Reply With Quote