ciaolore
This commit is contained in:
parent
6154b4302e
commit
32332ee2bf
|
@ -2,6 +2,7 @@
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"errexit.h": "c",
|
"errexit.h": "c",
|
||||||
"structures.h": "c",
|
"structures.h": "c",
|
||||||
"custom_msgq.h": "c"
|
"custom_msgq.h": "c",
|
||||||
|
"errno.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -10,7 +10,8 @@ extern int _SHMID;
|
||||||
void setupServerShm();
|
void setupServerShm();
|
||||||
void setupClientShm();
|
void setupClientShm();
|
||||||
|
|
||||||
int getShmid(); //FIXME: dovrebbero andare nel .c perche non vanno usate da altri file
|
int getShmidServer();
|
||||||
|
int getShmidClient(); //FIXME: dovrebbero andare nel .c perche non vanno usate da altri file
|
||||||
tile_t * shmServerAt();
|
tile_t * shmServerAt();
|
||||||
tile_t * shmClientAt();
|
tile_t * shmClientAt();
|
||||||
void shmDt(void *shm_ptr);
|
void shmDt(void *shm_ptr);
|
||||||
|
|
|
@ -2,34 +2,64 @@
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <custom_shm.h>
|
#include <custom_shm.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <errExit.h>
|
#include <errExit.h>
|
||||||
#include <forza4.h>
|
#include <forza4.h>
|
||||||
|
|
||||||
int _SHMID;
|
int _SHMID;
|
||||||
|
|
||||||
int getShmid();
|
|
||||||
tile_t *shmServerAt();
|
tile_t *shmServerAt();
|
||||||
|
|
||||||
void setupServerShm() {
|
void setupServerShm() {
|
||||||
_SHMID = getShmid();
|
_SHMID = getShmidServer();
|
||||||
_BOARD = shmServerAt();
|
_BOARD = shmServerAt();
|
||||||
|
|
||||||
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
for (int i = 0; i < _ROWS * _COLLUMS; i++) {
|
||||||
_BOARD[i] = EMPTY;
|
_BOARD[i] = EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupClientShm() {
|
void setupClientShm() {
|
||||||
printf(PCLIENT "Setting up shm\n");
|
printf(PCLIENT "Setting up shm\n");
|
||||||
_SHMID = getShmid();
|
_SHMID = getShmidClient();
|
||||||
_BOARD = shmClientAt();
|
_BOARD = shmClientAt();
|
||||||
}
|
}
|
||||||
|
|
||||||
int getShmid() {
|
int getShmidClient() {
|
||||||
int shmid = shmget(ftok(KEYFILE, SHMKEY), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
shmget(ftok(KEYFILE, SHMKEY), _ROWS * _COLLUMS * sizeof(tile_t), S_IRUSR | S_IWUSR);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getShmidServer() {
|
||||||
|
key_t key = ftok(KEYFILE, SHMKEY);
|
||||||
|
int shmid = shmget(key, _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
|
||||||
if (shmid == -1) {
|
if (shmid == -1) {
|
||||||
errExit("shmget", "getShmid");
|
// if errno was set to EEXIST, that means the memory location is alredy allocated
|
||||||
|
// the server fork and the child remove the memory
|
||||||
|
if (errno == EEXIST) {
|
||||||
|
char buf[100];
|
||||||
|
sprintf(buf, "the board alredy exit, I'll remove it now\n");
|
||||||
|
warningMsg(buf);
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
|
char str_key[16];
|
||||||
|
sprintf(str_key, "0x%08x", key);
|
||||||
|
printf("%s\n", str_key);
|
||||||
|
char *vec[] = {"/bin/ipcrm", "-M", str_key, NULL};
|
||||||
|
|
||||||
|
if ((execv("/bin/ipcrm", vec)) == -1) {
|
||||||
|
errExit("execv", "getShmidServer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (wait(0) == -1 && errno == EINTR);
|
||||||
|
|
||||||
|
return getShmidServer();
|
||||||
|
}
|
||||||
|
errExit("shmget", "getShmidServer");
|
||||||
}
|
}
|
||||||
|
|
||||||
return shmid;
|
return shmid;
|
||||||
|
|
|
@ -50,7 +50,7 @@ void setupServer(int argc, char *argv[]){
|
||||||
|
|
||||||
input_server_t checkServerInput(int argc, char *argv[]){
|
input_server_t checkServerInput(int argc, char *argv[]){
|
||||||
if (argc < 5) {
|
if (argc < 5) {
|
||||||
printf("\033[92m<Help>\033[39m ./F4Server ROW COL PLAYER1 PLAYER2\n"
|
printf(PHELP "./F4Server ROW COL PLAYER1 PLAYER2\n"
|
||||||
"\t- ROW \t\tnumber of rows\n"
|
"\t- ROW \t\tnumber of rows\n"
|
||||||
"\t- COL \t\tnumber of rows\n"
|
"\t- COL \t\tnumber of rows\n"
|
||||||
"\t- PLAYER1 \tplayer one token\n"
|
"\t- PLAYER1 \tplayer one token\n"
|
||||||
|
|
Loading…
Reference in New Issue