4 Replies - 867 Views - Last Post: 15 December 2009 - 10:03 AM Rate Topic: -----

#1 pic8pt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 17-May 09

Forks and Unending processes

Posted 14 December 2009 - 01:50 PM

A = init_sem(1);
EspacoEmbarque = init_sem(4);
Aterrei = init_sem(0);
ProntoParaPartir = init_sem(0);
FilaD = init_sem(3);
querLevantarVoo = init_sem(0);

int pp = fork();

	   if(pp == 0){
		  int i, n;
		  for(i = 0; i<3; i++){
			n = rand()%2;
			 alarm(3);
			   if(n == 0){	
				PA();
			   }else{
				CA();
			   }
		  }
	   }else{
		  int i, n;
		  for(i = 0; i<3; i++){
			  n = rand()%2;
			  alarm(3);
			   if(n==0){
				CE();
			   }else{
				PE();
			   }
		  }
	   }



 void PA(){
	P(A);
	  P(EspacoEmbarque);
		printf("- > produzir A <- \n");
	  V(Aterrei);
	V(A);
  }

  void CA(){
	P(A);
	  P(querLevantarVoo);
		printf("-> consumir A <-");
	V(A);
  }

  void PE(){
	P(ProntoParaPartir);
	  P(FilaD);
		printf("- > produzir E < -");
	V(querLevantarVoo);
  }

  void CE(){
	P(Aterrei);
	   printf("- > Consome E < - ");
	V(ProntoParaPartir);
  }



Hi guys,

I have a problem i do not comprehend. I am testing this skeleton for a project, and there are things which go beyond my wisdom and i was hoping someone could help me clarifying it.

First point is: If in my child i have PA and in the father i have CE, the program will not end, unless it dies by the alarm that i've putten there exactly for that reason, any idea why?

Second point: if you analyse the code, basicly PA -> CE -> PE -> CA thats how its supposed to work, is there ANYWAY that in a fork like that one that i present there i can have some sort of variable or i dno, that allows me to do the full cycle without the program ending in the middle of it? Because theoricly if you count the semaphores it works, but in practise it doesnt and its driving me crazy.

Thanks a bunge i really hope anyone can give me some lights.

PS: Sorry for not changing the variable names to english, it is currently in Portuguese.

This post has been edited by pic8pt: 14 December 2009 - 01:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Forks and Unending processes

#2 sabriath   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 13-December 09

Re: Forks and Unending processes

Posted 14 December 2009 - 02:16 PM

First:

When you fork() your process, a child is created and 'fork' returns 0, you use getppid() to get the parent pid, and getpid() to get the current pid. Inside the child process, you want to use the exit() function returning a value. For example:

child = fork();

if (child == 0)
{
	//we are the child, so it will run this
	exit(5); //<-- returns 5 to a wait() function
}
else if (child == -1)
{
	//there was an error
}
else
{
	//we are the parent, and 'child' holds the pid
}


In order to get the exit code produced by a child, you must use a wait() or waitpid() function. both of which requires a short* (i think) in order to store the result (or null if you don't care). so using the above we could do either:

short ret;

wait(&ret); //this will wait for the first of any child to return
waitpid(child, &ret); //waits for the specific pid to exit



Second:

If you require a specific way to process functions, then you will need to run them serially, fork() is a parallel function. You can fake serial something like this:

int state = 0;

int c1, c2, c3, c4;
c1 = fork();
if (c1 != 0)
{
  c2 = fork();
  if (c2 != 0)
  {
	c3 = fork();
	if (c3 != 0)
	{
	  c4 = fork();
	  if (c4 == 0)
	  {
		//child process 4 runs this
		while (state != 3);
		CA();
		exit(3);
	  } else {
		 //parent waiting for all returns
		 wait(NULL);
		 wait(NULL);
		 wait(NULL);
		 wait(NULL);
	  }
	} else {
	  //child process 3 runs this
	  while (state != 2);
	  PE();
	  state++;
	  exit(2);
	}
  } else {
	//child process 2 runs this
	while (state != 1);
	CE();
	state++;
	exit(1);
  }
} else {
  //child process 1 runs this
  PA();
  state++;
  exit(0);
}

Was This Post Helpful? 1
  • +
  • -

#3 pic8pt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 17-May 09

Re: Forks and Unending processes

Posted 14 December 2009 - 02:29 PM

View Postsabriath, on 14 Dec, 2009 - 01:16 PM, said:

First:

When you fork() your process, a child is created and 'fork' returns 0, you use getppid() to get the parent pid, and getpid() to get the current pid. Inside the child process, you want to use the exit() function returning a value. For example:

child = fork();

if (child == 0)
{
	//we are the child, so it will run this
	exit(5); //<-- returns 5 to a wait() function
}
else if (child == -1)
{
	//there was an error
}
else
{
	//we are the parent, and 'child' holds the pid
}


In order to get the exit code produced by a child, you must use a wait() or waitpid() function. both of which requires a short* (i think) in order to store the result (or null if you don't care). so using the above we could do either:

short ret;

wait(&ret); //this will wait for the first of any child to return
waitpid(child, &ret); //waits for the specific pid to exit



Second:

If you require a specific way to process functions, then you will need to run them serially, fork() is a parallel function. You can fake serial something like this:

int state = 0;

int c1, c2, c3, c4;
c1 = fork();
if (c1 != 0)
{
  c2 = fork();
  if (c2 != 0)
  {
	c3 = fork();
	if (c3 != 0)
	{
	  c4 = fork();
	  if (c4 == 0)
	  {
		//child process 4 runs this
		while (state != 3);
		CA();
		exit(3);
	  } else {
		 //parent waiting for all returns
		 wait(NULL);
		 wait(NULL);
		 wait(NULL);
		 wait(NULL);
	  }
	} else {
	  //child process 3 runs this
	  while (state != 2);
	  PE();
	  state++;
	  exit(2);
	}
  } else {
	//child process 2 runs this
	while (state != 1);
	CE();
	state++;
	exit(1);
  }
} else {
  //child process 1 runs this
  PA();
  state++;
  exit(0);
}



First of all thanks for the time spent! My idea of the program is that PA can run it self 4 times, unless CA gives him green light to produce more times (up to 3), CE consumes what PA produces and PE produzes for CA to consume.

The thing is, the serialization that you gave me is entirely new to me but i think i understand it, although if i apply it to my code, it will only do PA and then bam, it stays there and blocks and that is my problem with this whole thing. The way that i've done it i dont know how incorrect it is but i've studied java and i'm following some of the knowledge from there, that the if condition could give turns to each method like for example if random = 1 it would do PE, otherwise CA but it is blocking! :(

The way i understand the serialization is that, it will do PA first, then CE, then PE, then CA, is there any reason that it blocks? I mean, taking a look at the semaphores, ie the conditions they need to run, they all complete a circle so calling them 1 by 1 should work at least once, no?


Thank you so much in advance

PS: in the serialization what is the true meaning of the variable state?

This post has been edited by pic8pt: 14 December 2009 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 sabriath   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 26
  • Joined: 13-December 09

Re: Forks and Unending processes

Posted 15 December 2009 - 12:11 AM

View Postpic8pt, on 14 Dec, 2009 - 01:29 PM, said:

First of all thanks for the time spent! My idea of the program is that PA can run it self 4 times, unless CA gives him green light to produce more times (up to 3), CE consumes what PA produces and PE produzes for CA to consume.


You must have a common variable between your forks in order to give the "green light" for the others to go ahead, and then you must go into a stall until it gets the 'green' as well.

Quote

PS: in the serialization what is the true meaning of the variable state?


That is the reason for the 'state' variable, it is the common between the forks to let it know when to go. For parallel programming, you must think in parallel, and this becomes difficult while training, but once you have it, it comes natural.

look at my code again, you'll see that "state" starts at '0'. The ONLY process that has the 'green light' is the first fork child where if you look it jumps down to the "else", then it runs PA(). When that function is finished, 'state' is incremented, which gives the second fork child the green light, if you look it was suspended in a "while (state != 1);" loop. My program serialized what you first said to do (run PA, PE, CE, CA in that order), but you now state you want to do PA 4 times and up to 3 times if CA tells it to and some other things that aren't understood.

Let's break it down on what you want to do and see how to do what you want. For our example we will make a problem (just as an example):

"PA will run constantly, no limit. When CA is done running, it will stop PA from running at the end of it's function. PE does the same thing with CE."

With that, we would do this:

int caState = 0;
int ceState = 0;

int c1, c2, c3;

c1 = fork();
if (c1 == 0)
{
  //we are the first child

  while (caState == 0)
	  PA();

  exit(1);
}

//we don't need an else since code will exit otherwise
//this is parent
c2 = fork();
if (c2 == 0)
{
  //we are the second child

  while (ceState == 0)
	  PE();

  exit(2);
}

//still parent
c3 = fork();
if (c3 == 0)
{
  //third child

  CA();

  //this turns off PA() looping
  caState = 1;

  exit(3);
}

//we don't need to fork again, let the parent handle it
CE();

//turn off PE() looping
ceState = 1;

//now wait until all our children are done
wait(null);
wait(null);
wait(null);

This post has been edited by sabriath: 15 December 2009 - 12:16 AM

Was This Post Helpful? 1
  • +
  • -

#5 pic8pt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 17-May 09

Re: Forks and Unending processes

Posted 15 December 2009 - 10:03 AM

View Postsabriath, on 14 Dec, 2009 - 11:11 PM, said:

View Postpic8pt, on 14 Dec, 2009 - 01:29 PM, said:

First of all thanks for the time spent! My idea of the program is that PA can run it self 4 times, unless CA gives him green light to produce more times (up to 3), CE consumes what PA produces and PE produzes for CA to consume.


You must have a common variable between your forks in order to give the "green light" for the others to go ahead, and then you must go into a stall until it gets the 'green' as well.

Quote

PS: in the serialization what is the true meaning of the variable state?


That is the reason for the 'state' variable, it is the common between the forks to let it know when to go. For parallel programming, you must think in parallel, and this becomes difficult while training, but once you have it, it comes natural.

look at my code again, you'll see that "state" starts at '0'. The ONLY process that has the 'green light' is the first fork child where if you look it jumps down to the "else", then it runs PA(). When that function is finished, 'state' is incremented, which gives the second fork child the green light, if you look it was suspended in a "while (state != 1);" loop. My program serialized what you first said to do (run PA, PE, CE, CA in that order), but you now state you want to do PA 4 times and up to 3 times if CA tells it to and some other things that aren't understood.

Let's break it down on what you want to do and see how to do what you want. For our example we will make a problem (just as an example):

"PA will run constantly, no limit. When CA is done running, it will stop PA from running at the end of it's function. PE does the same thing with CE."

With that, we would do this:

int caState = 0;
int ceState = 0;

int c1, c2, c3;

c1 = fork();
if (c1 == 0)
{
  //we are the first child

  while (caState == 0)
	  PA();

  exit(1);
}

//we don't need an else since code will exit otherwise
//this is parent
c2 = fork();
if (c2 == 0)
{
  //we are the second child

  while (ceState == 0)
	  PE();

  exit(2);
}

//still parent
c3 = fork();
if (c3 == 0)
{
  //third child

  CA();

  //this turns off PA() looping
  caState = 1;

  exit(3);
}

//we don't need to fork again, let the parent handle it
CE();

//turn off PE() looping
ceState = 1;

//now wait until all our children are done
wait(null);
wait(null);
wait(null);



You're awesome.

Since i never learned C and i am already in a C project filled with semaphores and forks and shared memory pointers it gets a bit rough of me but you've spared me of a lot of headaches, thank you so much... from the heart really :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1