# Process, where each process generates a random integer

**URL:** <https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098>\
**Category:** Homework & Coursework Questions\
**Created:** [February 6, 2012, 3:02pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098 "2012-02-06T15:02:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![manisum](https://community.unix.com/user_avatar/community.unix.com/manisum/32/7751_2.png) [@manisum](https://community.unix.com/u/manisum)\
**Post date:** [February 6, 2012, 3:02pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098/1 "2012-02-06T15:02:09Z")

</div>

Hello all, I am writing a program where user enters an integer and the program creates that number of processes. Each child process generates a random integer. When a child process calls a procedure say Myprocedure it should terminate where as the parent process wait for the child to terminate.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 6, 2012, 3:06pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098/2 "2012-02-06T15:06:22Z")

</div>

Is this homework?

What have you tried?

---

<div class="post-metadata">

**Author:** ![manisum](https://community.unix.com/user_avatar/community.unix.com/manisum/32/7751_2.png) [@manisum](https://community.unix.com/u/manisum)\
**Post date:** [February 6, 2012, 3:51pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098/3 "2012-02-06T15:51:01Z")

</div>

yeah sort of a HW, but more of a prac program. so far I have tried this - should the fork() be in a separate loop, like first create all processes and then print random integer and at last terminate.  
int Myproc(int id) // couldn't figure out this part.

```nohighlight
{
  if(!id)
  return wait(id);

  else
  exit(1);
}

int main()
{
	pid_t p_id;
        int num;
        printf("Enter Number of processes : ");
        scanf ("%d",num);
for (int i = 0; i < num - 1; i++)
	
{
        p_id = fork();

	if(p_id == -1){
		printf("Unable to create process/child");
		return(-1);}

	if(!p_id)
		//printf("Parent Control Starts here.");
	else
	// Child Control
        // Generate random number.
        printf (" Child ID = %d --- %d", p_id, (rand() % 100 + 1) ); // random integer between 1 to 100
}// end for

}//end main

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 6, 2012, 3:52pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098/4 "2012-02-06T15:52:19Z")

</div>

Please post your homework in the homework forum following the homework rules.

---

<div class="post-metadata">

**Author:** ![manisum](https://community.unix.com/user_avatar/community.unix.com/manisum/32/7751_2.png) [@manisum](https://community.unix.com/u/manisum)\
**Post date:** [February 6, 2012, 8:33pm UTC](https://community.unix.com/t/process-where-each-process-generates-a-random-integer/304098/6 "2012-02-06T20:33:07Z")

</div>

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Hello all, I am writing a program where user enters an integer and the program creates that number of processes. Each child process generates a random integer. When a child process calls a procedure say Myprocedure it should terminate where as the parent process wait for the child to terminate.

1. Relevant commands, code, scripts, algorithms:

rand() fork()

1. The attempts at a solution (include all code and scripts):  
int Myproc(int id) // couldn't figure out this part.
2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):  
NU-FAST, Pakistan, Ms. Mehreen, CS314, www[dot].nu[dot].edu

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
