944,172 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1302
  • C++ RSS
Jun 26th, 2007
0

which process reads the cin

Expand Post »
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.
Similar Threads
Reputation Points: 14
Solved Threads: 7
Junior Poster
slacke is offline Offline
105 posts
since Jun 2006
Jun 26th, 2007
0

Re: which process reads the cin

what about posting your code?
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Jun 26th, 2007
0

Re: which process reads the cin

Quote ...
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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jun 26th, 2007
0

Re: which process reads the cin

The code is something like this...

C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 14
Solved Threads: 7
Junior Poster
slacke is offline Offline
105 posts
since Jun 2006
Jun 28th, 2007
0

canot pass state values

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.

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 14
Solved Threads: 7
Junior Poster
slacke is offline Offline
105 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reg Function pointers
Next Thread in C++ Forum Timeline: Printing binary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC