ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have some trouble finishing my first shared memory game. It's meant to play the well known Tic Tac Toe game between two processes. On the game board 1 stands for X's and 2 stands for O's.
Here's my first program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "header.h"
// function checks whether current player (process) won
int won(Table t)
{
if (t.matrix[1] > 0) {
if ((t.matrix[1] == t.matrix[2]) && (t.matrix[1] == t.matrix[3]))
return 1;
else
if ((t.matrix[1] == t.matrix[4]) && (t.matrix[1] == t.matrix[7]))
return 1;
else
if ((t.matrix[1] == t.matrix[5]) && (t.matrix[1] == t.matrix[9]))
return 1;
}
if (t.matrix[9] > 0) {
if ((t.matrix[9] == t.matrix[8]) && (t.matrix[9] == t.matrix[7]))
return 1;
else
if ((t.matrix[9] == t.matrix[6]) && (t.matrix[9] == t.matrix[3]))
return 1;
}
return 0;
}
//deletes shared memory
void removeshm(int shmid)
{
shmctl(shmid, IPC_RMID, NULL);
printf("Shared memory deleted.\n");
}
//prints gameboard
void print_table(Table t)
{
int i, j;
printf("The board: \n");
for (i=0; i < t.rows; i++) {
for (j=0; j < t.columns; j++) {
printf("%d ",t.matrix[i * t.columns + j]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[])
{
if( argc != 1 )
{
printf("Error: no parameter needed!\n");
exit(0);
}
//for details, check header
Table t;
t.key = 100;
t.rows = 3;
t.columns = 3;
t.matrix[10] = 0;
//connecting to shared memory
int shmid = shmget(t.key, 9 * sizeof(int), IPC_CREAT | 0666);
if (shmid == -1) {
printf("Existing shared memory. \n");
}
else
printf("Creating new shared memory \n");
if ((t.matrix = shmat(shmid, NULL, 0)) == (void *) -1) {
perror("Some error!");
exit(1);
}
srand(getpid());
while(1) {
//during even steps it's the turn of current player
if (t.matrix[10] % 2 == 0) {
int pos = rand() % (9 - 1) + 1;
//player chooses a free field on the gameboard (field with value of zero)
while (t.matrix[pos] != 0)
pos = rand() % (9 - 1) + 1;
t.matrix[pos] = 1;
t.matrix[10]++;
printf("No. of current step: %d \n", t.matrix[10]);
print_table(t);
if (won(t) == 1) {
t.matrix[10] = 10;
break;
}
}
printf("\n\n");
sleep(5);
if (t.matrix[10] >= 9)
break;
}
removeshm(100);
removeshm(shmid);
}
Second program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "header.h"
int won(Table t)
{
if (t.matrix[1] > 0) {
if ((t.matrix[1] == t.matrix[2]) && (t.matrix[1] == t.matrix[3]))
return 1;
else
if ((t.matrix[1] == t.matrix[4]) && (t.matrix[1] == t.matrix[7]))
return 1;
else
if ((t.matrix[1] == t.matrix[5]) && (t.matrix[1] == t.matrix[9]))
return 1;
}
if (t.matrix[9] > 0) {
if ((t.matrix[9] == t.matrix[8]) && (t.matrix[9] == t.matrix[7]))
return 1;
else
if ((t.matrix[9] == t.matrix[6]) && (t.matrix[9] == t.matrix[3]))
return 1;
}
return 0;
}
void removeshm(int shmid)
{
shmctl(shmid, IPC_RMID, NULL);
printf("Shared memory deleted.\n");
}
void print_table(Table t)
{
int i, j;
printf("Board: \n");
for (i=0; i < t.rows; i++) {
for (j=0; j < t.columns; j++) {
printf("%d ",t.matrix[i * t.columns + j]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[])
{
if( argc != 1 )
{
printf("ERROR: no parameter needed!\n");
exit(0);
}
Table t;
t.key = 100;
t.rows = 3;
t.columns = 3;
int shmid = shmget(t.key, 9 * sizeof(int), IPC_EXCL);
t.matrix = shmat(shmid, NULL, 0);
while(1) {
//during odd steps it's the turn of second player
if (t.matrix[10] % 2 == 1) {
int pos = rand() % (9 - 1) + 1;
while (t.matrix[pos] != 0)
pos = rand() % (9 - 1) + 1;
t.matrix[pos] = 2;
t.matrix[10]++;
printf("No. of current step: %d \n", t.matrix[10]);
print_table(t);
if (won(t) == 1) {
t.matrix[10] = 10;
break;
}
}
sleep(5);
if (t.matrix[10] >= 9)
break;
}
shmdt(t.matrix);
removeshm(100);
removeshm(shmid);
}
Also, my header file contains the following:
Code:
typedef struct{
int rows;
int columns;
key_t key;
int *matrix;
}Table;
My problem is that both server and client stop with segmentation fault, and I don't know why. Program prints the gameboard at each step, but it won't stop once someone has won.
Could someone help me with this?
If you wish to use element matrix[10], then change the allocation size to 11*sizeof(int)
Also add more debug-printf, and after every printf use fflush(stdout).
Also pos should be between 1 and 9: pos = rand() % 9 + 1
Thank you, I fixed that, but program stops at step no. 8 when there's only one free field left so I have to kill processes with CTRL + C.
Also, after editing and recompiling, the program won't even start the game, it stops with message "Creating new shared memory".
EDIT:
Okay, program seems to exit after 9 steps (when gameboard has no free fields left) and deletes shared memory segment, but it won't exit sooner in case one of the players wins.
Last edited by LinaBellus; 12-13-2018 at 03:19 AM.
Reason: Fixed a few things.
Okay, I fixed it, it works well now. The problem was firstly with the print_table function, both loops must start from 1 and not 0, and a few other smaller issues.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.