Sponsored Content
Top Forums Programming Two player game, forking & sockets Post 302528684 by Corona688 on Tuesday 7th of June 2011 05:15:05 PM
Old 06-07-2011
Quote:
Originally Posted by Shang
I have read about different approaches and decided to use SysV shared memory for a start.
Any particular reason? It's the same thing in the end, just a lot more convoluted.
Quote:
Assuming that we don't know how many clients(c) will connect and how many games(floor(c/2)) will be started the question is how to manage this shared memory segment?
My first idea was to create an array of pointers to game_state (type, see below), which would be the argument for shmat() function and store information about all games.
I don't understand what you mean by 'argument for shmat'. Do the games inherit shared memory for the parent or not? When you fork(), shared memory is inherited, so if the parent had it the children will start off with it and not need to fool with shmat().

Pointers won't work right in shared memory. Imagine that process A allocates memory and shoves the pointer into the shared mem. Process B tries to use this pointer, but process B has a different heap than process A. So B tries to use memory it never malloc()ed and crashes.

You have to store the contents in the shared memory, never heap pointers. Even pointers to the shared memory might not work unless every process maps the memory in the exact same place. You could store an array index, if the array was in shared mem too.
Quote:
But how to dynamically expand this array in case of new game start without ruining attachment?
What do you mean by 'ruining attachment'?

I know that when mmap() in a segment, pages won't actually be allocated until you use them. Just make a big enough memory segment in the first place and the OS will dynamically allocate more pages as you use them. I'm not sure sysv has this same ease and simplicity.
Quote:
How to implement reading and modification of shared memory segment in turns? (player1-mainprocess-player2-mainprocess-player1-mainprocess-player2-....)
How about, each player gets its own sem, so player 1 waits on sem 1, does its turn, posts on sem 2. Player 2 waits on sem 2, does its turn, and posts on sem 3. Player 3 waits on sem 3, does its turn, posts on sem 1.

That could be a ridiculous number of semaphores for a lot of clients, though. You could have an integer stored in shared memory of whose turn it is, and when a client is created they poll it until they're told it's their turn, then they sem_wait()/sem_post()/sem_wait()/sem_post()... I think the order would remain stable after that.

As for your game structure -- instead of a structure full of big arrays, how about a big array of structures?
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Association b/w sockets & processes

Hi, Is there any way i can know the association between sockets and the processes which created them. :confused: (5 Replies)
Discussion started by: soorajmu
5 Replies

2. Linux

Having player problem && need your help.

My System is FC3, The following Players don't run well: 1).Helix Player couldn't play MP3 and *.wma files, 2).Totem is the same, 3)XMMS couldn't play .wma files. I am a newer of Linux,but I like Linux,just as you. Could you help me? Thank you! (1 Reply)
Discussion started by: letcorpmuv
1 Replies

3. Programming

Forking in a loop

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation. Because I am stuck with this. #include <stdio.h> main(){ int i = 0; printf("I am the... (1 Reply)
Discussion started by: manjuWicky
1 Replies

4. UNIX for Advanced & Expert Users

Forking

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation with the schedular functionality it will help a lot. Because I am stuck with this. #include <stdio.h> main(){... (3 Replies)
Discussion started by: manjuWicky
3 Replies

5. HP-UX

Mozilla & Macromedia Flash Player Plugin Problem

Hello All, I am using Mozilla 1.7.8 on hp-ux 11.00, and install flash player 6 for it. it is giving following errors and get crashed. when i want to open a site need flash plugin. Gtk-WARNING **: invalid cast from `GtkSuperWin' to `GtkWidget' Gtk-WARNING **: invalid cast from `GtkSuperWin'... (1 Reply)
Discussion started by: Awadhesh
1 Replies

6. Programming

forking within a thread

Is it safe to call fork+exec in a multithreaded application. Because In my multithreaded application, I need to execute another program in each thread. I am using solaris 10. Any suggestions pls. (2 Replies)
Discussion started by: axes
2 Replies

7. Programming

need help in forking

I have an input file with contents like: 5785690|68690|898809 7960789|89709|789789 7669900|87865|659708 7869098|65769|347658 so on.. I need to pass this file to 10 parallely running processes (forking)so that each line is processed by a process and no line is processed twice and write the... (1 Reply)
Discussion started by: rkrish
1 Replies

8. Linux

Unable to install VLC media player or any other player in SL 6.3 distro

Hi, I am unable to install VLC or any other media players in my SL 6.3 distro. I am using yum utility to install the packages, but i am getting the below error messages, --> Processing Dependency: libpng15.so.15()(64bit) for package: vlc-core-2.0.3-1.fc18.x86_64 --> Processing... (1 Reply)
Discussion started by: vel4ever
1 Replies
SEM_INIT(3)						     Linux Programmer's Manual						       SEM_INIT(3)

NAME
sem_init - initialize an unnamed semaphore SYNOPSIS
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); Link with -pthread. DESCRIPTION
sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the sem- aphore. The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes. If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap). If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), and so on. Initializing a semaphore that has already been initialized results in undefined behavior. RETURN VALUE
sem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error. ERRORS
EINVAL value exceeds SEM_VALUE_MAX. ENOSYS pshared is nonzero, but the system does not support process-shared semaphores (see sem_overview(7)). ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +-----------+---------------+---------+ |Interface | Attribute | Value | +-----------+---------------+---------+ |sem_init() | Thread safety | MT-Safe | +-----------+---------------+---------+ CONFORMING TO
POSIX.1-2001. NOTES
Bizarrely, POSIX.1-2001 does not specify the value that should be returned by a successful call to sem_init(). POSIX.1-2008 rectifies this, specifying the zero return on success. SEE ALSO
sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-09-15 SEM_INIT(3)
All times are GMT -4. The time now is 03:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy