Sponsored Content
Top Forums Programming Two player game, forking & sockets Post 302528173 by Corona688 on Monday 6th of June 2011 02:57:09 PM
Old 06-06-2011
Okay.

You can create a shared memory segment with mmap. You can map in a file so as to keep memory stored on disk, or just map it in anonymously. It all just acts like memory either way.

Code:
// file-backed
int fd=open("filename", O_RDWR|O_CREAT);
ftrundate(fd, length);
char *mem=mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

// not file-backed
char *mem=mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);

Semaphores will work fine to control it. pthread mutexes will work too, at least in modern linux, but semaphores would be more portable.

The server would do something like this very sketchy code
Code:
char *shared_mem;
sem_t sem;

void child_stuff(void)
{
        while(1)
        {
                sem_wait(&sem);
                shared_mem[0]++;
                sem_post(&sem);
        }
}

int main(void)
{
       shared_mem=mmap(...);

       // Initialize shared semaphore to 1.  It can be locked once, further attempts
       // will block.
       sem_init(&sem, 1, 1); 
       while(1)
       {
               pid_t pid;
               int fd=accept(...);
               if(fd < 0) continue;

               pid=fork();
               if(pid == 0) // child code
               {
                       // A forked process gets a copy of everything the main process
                       // has open.  99% of it the child probably doesn't need, so
                       // close it.
                       //
                       // You can also set files auto-close fork with an ioctl, look for 
                       // close-on-exec.
                       close_positively_everything_except_what_child_needs;
                       child_stuff();
                       exit(0); // DO NOT FORGET!  Don't want child doing parent stuff after child_stuff finishes for some reason
               }
               else if(pid > 0) // parent stuff
                      add_to_list_of_children(pid);
               else
               {
                       perror("fork failed for some reason");
                       break;
               }
       }
}


Last edited by Corona688; 06-06-2011 at 04:10 PM.. Reason: many edits to make code slightly less sketchy
 

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
FISH(6) 							 BSD Games Manual							   FISH(6)

NAME
fish -- play ``Go Fish'' SYNOPSIS
fish [-p] DESCRIPTION
fish is the game ``Go Fish'', a traditional children's card game. The computer deals the player and itself seven cards, and places the rest of the deck face-down (figuratively). The object of the game is to collect ``books'', or all of the members of a single rank. For example, collecting four 2's would give the player a ``book of 2's''. The options are as follows: -p Professional mode. The computer makes a random decision as to who gets to start the game, and then the computer and player take turns asking each other for cards of a specified rank. If the asked player has any cards of the requested rank, they give them up to the asking player. A player must have at least one of the cards of the rank they request in their hand. When a player asks for a rank of which the other player has no cards, the asker is told to ``Go Fish!''. Then, the asker draws a card from the non-dealt cards. If they draw the card they asked for, they con- tinue their turn, asking for more ranks from the other player. Otherwise, the other player gets a turn. When a player completes a book, either by getting cards from the other player or drawing from the deck, they set those cards aside and the rank is no longer in play. The game ends when either player no longer has any cards in their hand. The player with the most books wins. fish provides instructions as to what input it accepts. BUGS
The computer cheats only rarely. BSD
May 31, 1993 BSD
All times are GMT -4. The time now is 07:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy