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)						   BSD Library Functions Manual 					       SEM_INIT(3)

NAME
sem_init -- initialize an unnamed semaphore LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); DESCRIPTION
The sem_init() function initializes the unnamed semaphore pointed to by sem to have the value value. A non-zero value for pshared specifies a shared semaphore that can be used by multiple processes, the semaphore should be located in shared memory region (see mmap(2), shm_open(2), and shmget(2)), any process having read and write access to address sem can perform semaphore opera- tions on sem. Following a successful call to sem_init(), sem can be used as an argument in subsequent calls to sem_wait(3), sem_trywait(3), sem_post(3), and sem_destroy(3). The sem argument is no longer valid after a successful call to sem_destroy(3). RETURN VALUES
The sem_init() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indi- cate the error. ERRORS
The sem_init() function will fail if: [EINVAL] The value argument exceeds SEM_VALUE_MAX. [ENOSPC] Memory allocation error. SEE ALSO
sem_destroy(3), sem_getvalue(3), sem_post(3), sem_trywait(3), sem_wait(3) STANDARDS
The sem_init() function conforms to ISO/IEC 9945-1:1996 (``POSIX.1''). BSD
January 9, 2010 BSD
All times are GMT -4. The time now is 12:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy