Compare commits
2 Commits
e8606a18bd
...
cdc14ee0e7
Author | SHA1 | Date |
---|---|---|
Edi De Candido | cdc14ee0e7 | |
Edi De Candido | 7bea27b8e7 |
|
@ -3,7 +3,6 @@
|
|||
|
||||
|
||||
|
||||
#define KEYFILE "../LICENSE"
|
||||
|
||||
// semaphore
|
||||
union semun {
|
||||
|
@ -13,6 +12,9 @@ union semun {
|
|||
};
|
||||
|
||||
// shared memory
|
||||
#define KEYFILE "../LICENSE"
|
||||
#define SHMERR (void *)-1
|
||||
|
||||
typedef struct {
|
||||
int collums;
|
||||
int rows;
|
||||
|
@ -26,15 +28,18 @@ typedef enum {
|
|||
PLAYER2
|
||||
} tile_t;
|
||||
|
||||
typedef struct {
|
||||
tile_t *board;
|
||||
input_server_t inS;
|
||||
} shm_t;
|
||||
|
||||
static const size_t SHM_SIZE = sizeof(shm_t);
|
||||
|
||||
// signal
|
||||
#define DEFAULT_SIGINT 2
|
||||
#define TIME_TO_RESET 10
|
||||
|
||||
//message queue
|
||||
typedef struct {
|
||||
long mtype;
|
||||
int board_id;
|
||||
input_server_t server_in;
|
||||
char *name_player1, *name_player2;
|
||||
} msgq_t;
|
||||
|
||||
#endif
|
|
@ -1,14 +1,45 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/stat.h>
|
||||
#include <custom_shm.h>
|
||||
#include <structures.h>
|
||||
#include <errExit.h>
|
||||
|
||||
int getShmid() {
|
||||
int shmid = shmget(ftok(KEYFILE, 'z'), SHM_SIZE, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
int getShmid(int row, int col) {
|
||||
int shmid = shmget(ftok(KEYFILE, 'z'), row * col * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (shmid == -1) {
|
||||
errExit("shmget", "getShmid");
|
||||
}
|
||||
|
||||
|
||||
return shmid;
|
||||
}
|
||||
|
||||
tile_t * shmServerAt(int shmid) {
|
||||
tile_t *result = (tile_t *)shmat(shmid, NULL, 0);
|
||||
if (result == SHMERR) {
|
||||
errExit("shmat", "shmServerAt");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
tile_t * shmClientAt(int shmid) {
|
||||
tile_t *result = (tile_t *)shmat(shmid, NULL, SHM_RDONLY);
|
||||
if (result == SHMERR) {
|
||||
errExit("shmat", "shmClientAt");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void shmDt(void *shm) {
|
||||
if (shmdt(shm) == -1) {
|
||||
errExit("shmdt", "shmDt");
|
||||
}
|
||||
}
|
||||
|
||||
void shmServerRm(int shmid) {
|
||||
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
||||
errExit("shmctl", "shmServerRm");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue