[SOLVED] C programming game skeleton and fork just don't mix
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.
C programming game skeleton and fork just don't mix
I'm having a problem with fork, the program should compile with gcc.
Fork doesn't print "ticks." each second like I thought it would. and when I do get fork to print, it just spams the terminal. I want to print a "tick." every second or so.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
struct character_file
{
char *name; /* Character name */
char *password; /* Character password */
char *description; /* Character description */
short int maxhits; /* Character hitpoints */
short int maxmana; /* Character mana */
short int maxmoves; /* Character moves */
short int hits; /* Character hitpoints */
short int mana; /* Character mana */
short int moves; /* Character moves */
short int weight; /* Characters weight */
short int height; /* Characters height */
short int str; /* Strength modifier */
short int dex; /* Dexterity modifier */
short int cha; /* Charisma modifier */
short int in; /* Intelligence modifier */
short int wis; /* Wisdom modifier */
short int con; /* Constitution modifier */
short int thaco; /* Thaco - ac = d20 needed to hit */
short int attack; /* Attack + d20 >= (thaco - ac) = hit */
short int ac; /* ac armor class */
short int dr; /* dr + damage dice - ac = damage */
struct objects *carried; /* Character objects they carry */
struct objects *equipped; /* Character objects equipped */
};
struct rooms
{
short int rnum; /* Room number */
char *name; /* Room name */
char *description; /* Room description */
char *flags; /* Room flags ex: Dark Tunnel noshout */
char *types; /* Types of rooms Inside Underwateretc. */
char *exits; /* ex: North east south west up down */
struct room_data *next;
};
struct objects
{
char *name; /* Object name */
char *description; /* Object description */
short int onum; /* Unique object number */
short int type; /* Type of object */
short int weight; /* Object weight */
short int *flags; /* Object flags etc: !warrior cursed */
struct modifiers *mod; /* Object modifiers */
struct objects *next; /* Linked list structure */
};
struct modifiers
{
short int maxhits; /* Max hitpoint modifier */
short int maxmana; /* Max mana point modifier */
short int maxmoves; /* Max move point modifier */
short int hit; /* Hit point modifier */
short int mana; /* Mana point modifier */
short int moves; /* Move point modifier */
short int weight; /* Characters weight */
short int height; /* Characters height */
short int thaco; /* Thaco - ac = d20 needed to hit */
short int attack; /* Attack + d20 >= (thaco - ac) = hit */
short int ac; /* ac armor class */
short int dr; /* dr + damage dice - ac = damage */
short int str; /* Strength modifier */
short int dex; /* Dexterity modifier */
short int cha; /* Charisma modifier */
short int in; /* Intelligence modifier */
short int wis; /* Wisdom modifier */
short int con; /* Constitution modifier */
};
struct command_buffer {
char *buf;
struct command_buffer *next;
};
void skip_spaces(char **string)
{
for (; **string && isspace(**string); (*string)++);
}
int main(int argc, char **argv)
{
pid_t parent_pid;
pid_t child_pid;
parent_pid = getpid();
child_pid = fork();
if ( child_pid == 0)
{
child_pid = getpid();
short int j;
short int second = 0;
for( j = 0; j <= 500000; j++)
{
second++;
if ( second == 10000)
{
printf("Tick.");
second = 0;
}
}
}
if ( getpid() == parent_pid)
{
printf("Parent: parent_pid: %d,\r\n", parent_pid);
printf("Parent: child_pid: %d\r\n", child_pid);
FILE *fp;
bool quit;
char ch = 0x00;
short int x, y;
short int char_count;
short int array [10][2] =
{
{1,1},
{2,2},
{3,3},
{4,4},
{5,5},
{6,6},
{7,7},
{8,8},
{9,9},
{0,0}
};
struct command_buffer *buffer = NULL;
buffer = malloc(sizeof(buffer));
buffer->buf = malloc(101);
buffer->next = NULL;
struct command_buffer *top = NULL;
top = buffer;
for ( x = 0; x < 10; x++)
printf ("%d: size of array:%d\r\n", array[x][0], sizeof(array));
if ((fp = fopen("hometown.map", "w+")) == NULL)
printf("Error opening hometown.map\r\n");
for ( y = 0; y<= 25; y++)
{
for ( x = 0; x <= 50; x++)
{
fwrite(".", 1, 1, fp);
printf(".");
}
fwrite("\n", 1, 1, fp);
printf("\n");
}
fclose(fp);
while ( quit != true )
{
int j;
printf("Enter a line of text (<80 chars)\n");
char_count = 0; ch = getchar();
while( ch != '\n')
{
if ( char_count < 100)
{
buffer->buf[char_count++] = ch;
}
else
{
buffer->buf[char_count] = 0x00;
if ( buffer->next == NULL)
{
buffer->next = malloc(sizeof(buffer->next));
buffer->next->buf = malloc(101);
buffer = buffer->next;
buffer->next = NULL;
}
else
buffer = buffer->next;
buffer->buf[0] = ch;
char_count = 1;
}
ch = getchar();
}
ch = 0x00;
for ( buffer = top; buffer != NULL; buffer = buffer->next)
{
for ( j = 0; j < 101; j++)
buffer->buf[100] = 0x00;
if ( buffer->buf[0] != 0x00)
{
if(!strcmp(buffer->buf, "exit"))
quit = true;
else
printf("%s", buffer->buf);
}
for ( j = 0; j < 101; j++)
buffer->buf[j] = 0x00;
}
buffer = top;
printf("\r\n");
//printf("quit command didn't work\r\n");
}
while ( buffer != NULL)
{
top = buffer->next;
free(buffer);
buffer = top;
}
kill(child_pid);
}
_exit(0);
}
here you can find a good tutorial: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
try to implement 2 functions: parent code and child code and after fork you can call these functions (based on the return value).
instead of the cycle on seconds you would need to use sleep:
while (true) { sleep (1); printf("Tick."); } # or something similar.
Thanks that tick trick worked better then mine. it will starting printing after I'm done looping getchar I think getchar is stopping it's execution. still doesn't print while the other process waits for input.
Hmm tick didnt work actually. some time I get ticks and they don't tick like I want them to. no problem Im gonna have to make it more passive. also global variables don't work for me with fork. but that example uses c++ maybe that will help, but not me.
threads sound promising but Im sure thats c++ and I will have to change alot of malloc calls. Im gonna dang use ncurss and just start passivly checking for input one character at a time I suppose. receving data is easier and you don't need fork either but eventually Ill research c++; so any of you guys can figure this out: http://www.linuxquestions.org/questi...39#post4795539
also download this and make it work without valgrind whining about null pointer comparisons http://o0oo0.net16.net/shared
Hey I'm having trouble with threads:
They don't seem to pay attention to my for loops again like fork:
The up side is the variables work interchangably
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.