LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2014, 04:59 AM   #1
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Rep: Reputation: Disabled
Angry Play between 2 Process


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.

PHP Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
 
#define GAME_LOWER_LIMIT 0
#define GAME_UPPER_LIMIT 100
 
int main(){
  
int  number;
    
int  done 0;
    
char line[32];
    
int  numGuesses 0;


    
srand(time(0));
    
number GAME_LOWER_LIMIT rand() % (GAME_UPPER_LIMIT GAME_LOWER_LIMIT 1);


    while (!
done)
    {
        
printf("Guess a number between %d and %d: "GAME_LOWER_LIMITGAME_UPPER_LIMIT);


        if (
fgets(linesizeof(line), stdin) != NULL)
        {
            
int guess;


            if (
sscanf(line"%d", &guess) == 1)
            {
                if (
number == guess)
                {
                    
printf"\nYou guessed correctly after %d times!\n\n"numGuesses);
                    
done 1;
                }
                else
                {
                    
printf("Your guess was too %s.\nTry again: "number guess "high" "low");
                    
numGuesses++;
                }
            }
            else
            {
                
printf("\nIllegal input; try again...\n\n");
            }
        }
        else
        {
            
printf("\nIllegal input; try again...\n\n");
        }
    }



 
Old 05-03-2014, 12:53 PM   #2
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
How I can do it plz help me
the time is paaassing no time

 
Old 05-03-2014, 02:17 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
I need the game run between two process the one guess and the other sleep
Please explain this further as currently I do not understand the question?
 
Old 05-03-2014, 02:35 PM   #4
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
Ok
I will explain

Last edited by one girl; 05-04-2014 at 01:57 AM.
 
Old 05-04-2014, 01:57 AM   #5
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
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,,,

Last edited by one girl; 05-04-2014 at 01:58 AM.
 
Old 05-04-2014, 04:57 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 05-04-2014, 05:12 AM   #7
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-04-2014, 05:33 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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 &
 
Old 05-04-2014, 06:00 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Or, if you don't want manually duplicate stdout to stderr:

Code:
mknod prg1-to-prg2 p
mknod prg2-to-prg1 p
./prg1 <prg2-to-prg1 | tee prg1-to-prg2 &
./prg2 <prg1-to-prg2 | tee prg2-to-prg1 &
Of course you can (and should) write a main program (in shell or C or other language), that does these redirections:
Code:
$ cat run_both.sh
#!/bin/sh
mknod prg1-to-prg2 p
mknod prg2-to-prg1 p
./prg1 <prg2-to-prg1 | tee prg1-to-prg2 &
./prg2 <prg1-to-prg2 | tee prg2-to-prg1 &
wait
 
Old 05-04-2014, 06:15 AM   #10
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-04-2014, 08:48 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please write the other program (the one that makes the guesses). (Or hire someone to write it.)
 
Old 05-04-2014, 09:01 AM   #12
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Original Poster
Rep: Reputation: Disabled
If I know how to do it ,
I will do

but really I do know how to start
 
Old 05-04-2014, 04:04 PM   #13
cin_
Member
 
Registered: Dec 2010
Posts: 281

Rep: Reputation: 24
a dot file

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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Videos don't play over network, wireless or wired, using sftp but will play locally rrrssssss Gentoo 5 12-30-2010 12:19 PM
Finding the Process ID of a Process While Initiating the Process senthilmuthiah Linux - Newbie 7 04-02-2009 10:37 AM
RealPlayer/Helix Player will not play Mov and AVI files. Can I make it play them. Chargh Fedora 4 03-25-2008 02:37 PM
Burnt Audio CD by K3B CANNOT play on external CD playing device, but can play on comp robin.com.au Linux - Software 14 05-27-2007 04:30 AM
how can i slow down the mp3 play process ewt3y Linux - Software 6 06-24-2005 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration