🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Fork + Shared Variables

Started by
6 comments, last by Bregma 17 years, 6 months ago
I'm looking to create a _very_ simple front end to my server. I just need to input a command using fork. Right now I'm inputting a line from the child process, but I'm not sure how to send that command to the parent process. Any help will be greatly appreciated!
see you in heaven
Advertisement
I think you might get more help in the Unix forum so I'm going to move your post there.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Dang, ostracized by the man! I'm just a little put off by the Unix forum because it looks like it has a weak user base. But thanks, I hope it does help. I'm open to any other suggestions to do what I need.
see you in heaven
It's a bit long to explain. It's just techincal , not very complicated.

To make variables that are shared between processes you need to use :

int shmget(key_t key, int size, int flags) ;

char *shmat(int segid, char *addr, int flags) ;


Basically, you reserve some memory as a shared memory using those functions.

One important thing to remember is that you must acces shared memory trough pointers.

Just google those functions(sorry, I don't have any specific links) to get a detailed description of it's usage.

Don't forget to release the shared memory with
int shmdt(char *addr) ;
and destroy it with:
int shmctl(int segid, int cmd, struct shmid_ds *sbuf) ;


I hope this will set you in the right direction.
-----------------Always look on the bright side of Life!
Its a server?

Why not do the communication via sockets? TCP for text transfer would be easy. Then you could run the front end from a remote machine with no extra work as a benefit!
Be careful about shared memory, you may well run into trouble with asynchronous modifications.

Another suggestion would be to create a pipe, give one (write) end to the child and keep another (read) end in the parent. Then, you can have the parent read from the pipe whenever new data is necessary. If you do not want to block the process, you can also use signalling between processes to notify the parent that new data is present in the pipe.
This file show a function called exec_filter that will create a child and communicate with it. It would have to be modified a bit to suit your needs. The child could have a loop reading commands from stdin and writing them back to the parent. The parent in this function would be your main process, running some loop probably, so the use of select is necessary.
Quote: Original post by lorrain
I'm looking to create a _very_ simple front end to my server. I just need to input a command using fork. Right now I'm inputting a line from the child process, but I'm not sure how to send that command to the parent process.


I would recommend the best way is to use a file descriptor.

First off, by using a file descriptor, you can test your functions using stdin/stdout and redirecting input from a file. You can also receive input from a remote client without modification.

To use a file descriptor with a fork()ed child, create a pipe(2) before the fork() and dup2(2) the stdin and stdout file descriptors. You can then read one end of the pipe in the parent and write the other end in the child.

You will want to look into the select(2) call for driving your event loop in the parent process.

--smw

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement