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.

what about posting your code?

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.

The code is something like this...

int kill=0;
int proces1;
int proces2;

	void child2()
	{
		for(int b=0; b<100; b++)
		{
			sleep(2);
			cout << "2" << endl;
			if(kill==1)
			{
				cout << "killing child 1..." << endl;
				proces2=0;
				return;
			}
		}
	}
	void child1()
	{
		for(int a=0; a<100; a++)
		{
			sleep(3);
			cout << "1" << endl;
			if(kill==1)
			{
				cout << "killing child 2..." << endl;
				proces1=0;
				return;
			}
		}
	}


int main(int argc, char *argv[])
{


	int pid1, pid2;
	proces1 = 1;
	proces2 = 1;
	
	pid1 = fork();
	if(pid1 == 0)
	{
		child1();
	}
	else
	{
		cout << "forked 1" << endl;
	}


	pid2 = fork();
	if(pid2==0)
	{
		child2();
	}
	else
	{
		cout << "forked 2" << endl;
	}

	cout << "main runing" << endl;

	
	for(int clock=0; clock<100;clock++)
	{
		if(kill==0)
		{
			sleep(5);
			cout << "more processes needed, program runinig..." << endl;
			cin >> kill;
		}
		else
		{
			clock=200;
		}
	}
	
	cout << "killing signal received" << endl;

	sleep(10);		// just for sure 10 secs

	for(int ok=0; ok<1000; ok++)
	{
	if(proces1==0)
	{
		cout << "process 1 killed..." << endl;
		if(proces2==0)
		{
			cout << "process 2 killed..." << endl;
			ok = 1001;
		}
		else
		{
			cout << "zombie processes stil alive..." << endl; 
		}
	}
	}

  return EXIT_SUCCESS;
}

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...

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.

int kill;
int processStatus1;

int main()

 int child1, child2, child3 ...
 
 child1 = fork();... calls the refresh function for example...
 child2 = fork();... calls the virtualSpace function ...
 and so on...

 kill =1;

How can I make this interaction between child and main processes?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.