which process reads the cin

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 98
Reputation: slacke is an unknown quantity at this point 
Solved Threads: 7
slacke's Avatar
slacke slacke is offline Offline
Junior Poster in Training

which process reads the cin

 
0
  #1
Jun 26th, 2007
I am working on a code which has several processes forked(). (linux - KDevelop)

The processes after they were created runs paralell in the background.
I want to read on a command line (like cinn >> kill=1 ) which tells the main process to kill all the childs and the main p., but I dont exactly know which process reads the input on the console.

How works the consol after forking. Which process reads the data from it?

Thanks for any suggestion.
Last edited by slacke; Jun 26th, 2007 at 2:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: which process reads the cin

 
0
  #2
Jun 26th, 2007
what about posting your code?
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: which process reads the cin

 
0
  #3
Jun 26th, 2007
Which process reads the data from it?
All of them. Each process gets a handle to stdin and the computer makes sure that they all get a chance to read from it, but only one at a time.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 98
Reputation: slacke is an unknown quantity at this point 
Solved Threads: 7
slacke's Avatar
slacke slacke is offline Offline
Junior Poster in Training

Re: which process reads the cin

 
0
  #4
Jun 26th, 2007
The code is something like this...

  1.  
  2. int kill=0;
  3. int proces1;
  4. int proces2;
  5.  
  6. void child2()
  7. {
  8. for(int b=0; b<100; b++)
  9. {
  10. sleep(2);
  11. cout << "2" << endl;
  12. if(kill==1)
  13. {
  14. cout << "killing child 1..." << endl;
  15. proces2=0;
  16. return;
  17. }
  18. }
  19. }
  20. void child1()
  21. {
  22. for(int a=0; a<100; a++)
  23. {
  24. sleep(3);
  25. cout << "1" << endl;
  26. if(kill==1)
  27. {
  28. cout << "killing child 2..." << endl;
  29. proces1=0;
  30. return;
  31. }
  32. }
  33. }
  34.  
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38.  
  39.  
  40. int pid1, pid2;
  41. proces1 = 1;
  42. proces2 = 1;
  43.  
  44. pid1 = fork();
  45. if(pid1 == 0)
  46. {
  47. child1();
  48. }
  49. else
  50. {
  51. cout << "forked 1" << endl;
  52. }
  53.  
  54.  
  55. pid2 = fork();
  56. if(pid2==0)
  57. {
  58. child2();
  59. }
  60. else
  61. {
  62. cout << "forked 2" << endl;
  63. }
  64.  
  65. cout << "main runing" << endl;
  66.  
  67.  
  68. for(int clock=0; clock<100;clock++)
  69. {
  70. if(kill==0)
  71. {
  72. sleep(5);
  73. cout << "more processes needed, program runinig..." << endl;
  74. cin >> kill;
  75. }
  76. else
  77. {
  78. clock=200;
  79. }
  80. }
  81.  
  82. cout << "killing signal received" << endl;
  83.  
  84. sleep(10); // just for sure 10 secs
  85.  
  86. for(int ok=0; ok<1000; ok++)
  87. {
  88. if(proces1==0)
  89. {
  90. cout << "process 1 killed..." << endl;
  91. if(proces2==0)
  92. {
  93. cout << "process 2 killed..." << endl;
  94. ok = 1001;
  95. }
  96. else
  97. {
  98. cout << "zombie processes stil alive..." << endl;
  99. }
  100. }
  101. }
  102.  
  103. return EXIT_SUCCESS;
  104. }

but i'v got a feeling that there is something wrong in basic with that code...

for example I never get the - killing child 1.... or killing child 2...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 98
Reputation: slacke is an unknown quantity at this point 
Solved Threads: 7
slacke's Avatar
slacke slacke is offline Offline
Junior Poster in Training

canot pass state values

 
0
  #5
Jun 28th, 2007
I was trying to pass state values like kill=1, which should tell to all child processes that they should execute their ending processes and tell to the main process if it was succesful.

But I cant just pass any value to childs. I was trying with global variables (kill, processStatus1) but it did not work. Than I try pointers with same result.

  1. int kill;
  2. int processStatus1;
  3.  
  4. int main()
  5.  
  6. int child1, child2, child3 ...
  7.  
  8. child1 = fork();... calls the refresh function for example...
  9. child2 = fork();... calls the virtualSpace function ...
  10. and so on...
  11.  
  12. kill =1;

How can I make this interaction between child and main processes?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC