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> |
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.
|
I think in this case C or C++ is not important. Also I do not really understand what do you mean by "global variables don't work"
|
well you cant modify one variable and have the parent process save the variable they kinda work like function variables that you can't return.
|
I think you should fflush(stdout) after every printf when using forks, IIRC...
|
Quote:
Quote:
|
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 |
you can use threads in c as well, not only in c++.
http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html https://computing.llnl.gov/tutorials/pthreads/ |
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 |
please show what have you made
|
Quote:
Code:
#include <stdio.h> |
I don't think fork is a good method of achieving your aim. A fork is a different process
where logically your 'tick' is part of the same mass. There already exists a method for interval processing: interval timer Unfortunately the is very little documentation or example for it, Here is an example I knocked up if you are interested: You need to link pthread and realtime libraries. g++ -lrt -lpthread timer.cpp -o timer Code:
/* |
All times are GMT -5. The time now is 08:46 AM. |