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.
this game is run by the user ,I need the game run between two process the one guess and the other sleep ,until get the result .I need the answer by use the sleep and weak up.
ok please can any one solved with the pipes
or give me any hint to help me.
I want the first process guess the answer if it is not correct sleep this process and allow to the second process guess the answer if it right answer terminate the game ,if it is wrong sleep process two and weak up process one to guess the new answer and so as,,,
Still missing what you mean Could you explain what you mean by 'first process'? As far as I can see from your code you are not dealing with multiple processes.
Yes, actually my teacher for first part of my project asked to do it as the code above ,
But the second part as I described between 2 process using pipes
I know the Question is not easy and the big problem not clear how to do it
Also no one in the class until now, know how to do it
And date time is this week
As a start, write the two program, each using their standard input/output, only add an extra feature to write every output to stderr, too, so that you can see what's happening:
Code:
fprintf (stdout, "So you entered %d. It's too small.\n", tryvalue); fflush (stdout);
fprintf (stderr, "So you entered %d. It's too small.\n", tryvalue); fflush (stderr);
when both works, try something like this:
Code:
mknod prg1-to-prg2 p
mknod prg2-to-prg1 p
./prg1 <prg2-to-prg1 >prg1-to-prg2 &
./prg2 <prg1-to-prg2 >prg2-to-prg1 &
ok sorry can you help me to run this game by the computer not by the user. I mean the computer guess the answer until access to the right answer.
i wish understand what I mean.
unsure what you want exactly but if you want two processes to be able to 'communicate' naively you could have the two read and write to a shared file
proc1:
read in file
read in stdin
check stdin against file
react
Code:
#!/bin/bash
while [[ 17 ]]; do #17 is always 17 so infinite loop
fi=$(cat .sharedfile)
echo ..$fi
read userin
if [[ $userin == $fi ]]; then
echo "already in state"
else
echo "changed state: $userin"
echo $userin>.sharedfile
fi
done
proc2:
infinite while loop
read in file
depending on current file contents either:
react
or, sleep for some finite time(1,3,17 or however many seconds)
loop
Code:
#!/bin/bash
while [[ 17 ]] ; do #17 is always 17 so infinite loop
fi=$(cat .sharedfile)
if [[ $fi == "on" ]]; then
echo "onit"
else
echo "."
$(sleep 1) #sleep for 1second
fi
done
running the first process..
Code:
# cat .sharedfile
off
# ./proc1
..off
off
already in state
..off
on
changed state: on
while proc2..
Code:
.
.
.
onit
onit
onit
onit
if proc2 reads the file and decides to sleep, or wait for the file to change, and proc1 alters the shared file while proc2 is sleeping, when the proc2 sleep completes it will check the new state of the file, if it has gone unchanged it will sleep again, if changed it can perform some function that was predicated on a specific state of the shared file
the length of the sleep will be determined by how often a check is necessary and how much CPU usage you are willing to sacrifice for this game
Last edited by cin_; 05-04-2014 at 04:30 PM.
Reason: gramm`err
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.