Fork multi process with only on parent in C - c

I have a project in which I have to fork A B C and D process from one parent which is the original program/main() function. I used pipes to communicate between the processes, and I went through a lot of coding, but I wasn't able to send the right messages between the processes. However, I decide to go back to understand the basic concepts of fork() so I wrote the following code.
------Edited: My question is that can I use this template to send messages via pipes from all 4 processes and read those messages from parent? If so in which parent section I have to write the reading code?
`
if (fork()) { //parent log
printf("Inside parent\n");
if (fork()) { //parent log
printf("Inside parent\n");
if (fork()) { //parent log
printf("Inside parent\n");
if (fork()) { //parent log
printf("Inside parent\n");
}else { //child process D
printf("Inside process D\n");
_exit(1);
}
}else { //child process C
printf("Inside process C\n");
_exit(1);
}
}else { //child process B
printf("Inside process B\n");
_exit(1);
}
}else { //child process A
printf("Inside process A\n");
_exit(1);
}
My goal is to send messages from A to B and from B to C and from B to D, Also send messages from any process (A B C and D) to the parent.
Is this code template correct? Or there is something wrong with its structure?
Thanks a lot.

While your code does create 4 child processes it can be a lot simpler:
int main()
{
int i;
for(i = 0; i < 4; i++) {
printf("getpid = 0x%X\n", getpid());
if(fork()) {
printf("Inside parent\n");
}
else {
printf("Inside process %c\n", 'A'+i);
exit(1);
}
}
}
The key idea is that you can differentiate between each child by the value of i. You can even also use i in a switch statement:
int main()
{
int i;
for(i = 0; i < 4; i++) {
printf("getpid = 0x%X\n", getpid());
if(fork()) {
printf("Inside parent\n");
}
else {
switch(i) {
case 0:
printf("In process A\n");
break;
case 1:
printf("In process B\n");
break;
case 2:
printf("In process C\n");
break;
case 3:
printf("In process D\n");
break;
}
exit(1);
}
}
}

Related

Not fully understanding fork for switch program output

I have been trying to understand the output of this program, but still I don’t quite get it.
main()
{
int pid, i;
pid = getpid();
for (i = 0; i < 25; i++)
{
switch (fork())
{
case 0:
if (pid % 2 == 0)
{
exit(0);
break;
}
default:
if (pid % 2 != 0)
{
exit(0);
}
}
}
printf("I am the process %d and my father is the process %d\n", getpid(), getppid());
while (wait(NULL) > 0) {}
return 0;
}
When I run this, it returns:
I am the process 11110 and my father is the process 26453
However, if you were to run the above code without both "% 2", it won't return anything.
I am very confused about this. The way I thought it would work (for the code without "% 2") is, for each for iteration:
the child (pid==0) would finish its process (killing the child process) and always break from the switch (not affecting the for loop)
the father/main process will wait until the child dies
next for iteration
Is the above approach correct? If so, how would it be with "% 2"?
Without % 2 you'd get:
switch (fork())
{
case 0:
if (pid == 0)
{
exit(0);
break;
}
default:
if (pid != 0)
{
exit(0);
}
}
Since pid is not 0, the parent would exit(0) immediately after the first fork(), so you won't see a print statement.

C : Store PID in global variable

I am currently working on a school project and I wanted to store the PID of my process in the global array id, so that I can use it in another function :
int id[3];
int main(int agrc,const char* agrv[]) {
for(int i = 0; i < 3; i++) {
if((fork() == 0)) {
id[i] = (int)getpid();
printf("ID[%d] = %d\n",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]\n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]\n",getpid(),getppid());
central_code();
exit(0);
}
}
}
sleep(2);
printf("ID[0] = %d\n",id[0]);
printf("ID[1] = %d\n",id[1]);
printf("ID[2] = %d\n",id[2]);
}
But when I run this the output of the last 3 prints is 0, where it should be the PID of each process. Why does this happen?
On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.
If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:
pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %d\n",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]\n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]\n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}
By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.

I try to create a process using 'fork'

I'm start to studying the fork. while using the fork, I have some problems.
I'm trying to create a single parent process with two child
and two child trying to make each three grandchild.
When I run my code, unlike my expectations, so many child and grandchild come out.
Here my code:
int main()
{
int i, j, rev;
for(i=0;i<2;i++)
{
if((rev=fork())<0) { printf("fork() error\n"); exit(-1); }
else if(rev==0)
{
printf("child %d %d \n",getpid(),getppid());
for(j=0;j<3;j++)
{
if((rev=fork()) <0) { printf("fork() error\n"); exit(-1); }
else if(rev == 0)
{
printf("grandch %d %d \n",getppid(),getpid());
exit(0);
}
}
}
}
printf("parent %d %d \n",getpid(),getppid());
exit(0);
}
How can I correct this code?
One important example before using fork() statements :
//Calculate number of times hello is printed.
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Number of times hello printed is equal to number of process created. Total Number of Processes = 2^n where n is number of fork system calls. So here n = 3, 2^3 = 8.
fork (); // Line 1
fork (); // Line 2
fork (); // Line 3
L1 // There will be 1 child process
/ \ // created by line 1.
L2 L2 // There will be 2 child processes
/ \ / \ // created by line 2
L3 L3 L3 L3 // There will be 4 child processes
// created by line 3
So if you are trying to make two child process and then three grand
child follow something of this sort:
What you should do is something like this for two child processes
if(fork()) # parent
if(fork()) #parent
else # child2
else #child1
After you create process , you should check the return value. If you don't , the second fork() will be executed by both the parent process and the child process, so you have four processes.
If you want to create n child processes , just :
for (i = 0; i < n; ++i) {
pid = fork();
if (pid) { //means pid is non-zero value, i.e, pid>0
continue;
} else if (pid == 0) {
break;
} else {
printf("fork error\n");
exit(1);
}
}
The section of code that runs for the child processes doesn't exit. As a result, they continue on to run more iterations of the outer loop which only the parent process is supposed to run, so they spawn more children.
You need to call exit, or better yet _exit, so that the children don't do that:
int main()
{
int i, j, rev;
for(i=0;i<2;i++)
{
if((rev=fork())<0) { printf("fork() error\n"); exit(-1); }
else if(rev==0)
{
printf("child %d %d \n",getpid(),getppid());
for(j=0;j<3;j++)
{
if((rev=fork()) <0) { printf("fork() error\n"); exit(-1); }
else if(rev == 0)
{
printf("grandch %d %d \n",getpid(),getppid());
_exit(0);
}
}
sleep(1); // stick around so the grandchild can print the parent pid
_exit(0); // exit the child
}
}
printf("parent %d %d \n",getpid(),getppid());
sleep(1); // stick around so the child can print the parent pid
exit(0);
}

getting child process ID from the parent using C

I'm trying to write a C program where I have one parent that create two childs.
My task is to retrieve the process ID of the parent of and both childs. For this I've been using getpid().
Now I've been asked to get the child information from the parent. I don't get how I can do this. Like how can I obtain the processing ID for a child from the parent?
I have this at the moment (simplified)
fork1 = fork();
if (fork1 < 0)
fork error
else if (fork1 == 0) {
child 1
use getpid()
use getppid()
} else {
fork2 = fork();
if (fork2 < 0)
fork error
else if (fork2 == 0) {
child 2
use getpid()
use getppid()
} else
parent again
}
After a minute of googling I found this page, where everything you need is written:
System call fork() is used to create processes. It takes no arguments and returns a process ID.
I highlighted the part which is importand for you, so you don't need to do anything to get the process IDs of the children. You have them already in fork1 and fork2!
Here is some code which will print the PIDs from parent and children.
#include <stdio.h>
int main() {
int fork1 = fork();
if (fork1 < 0) {
printf("error\n");
} else if (fork1 == 0) {
printf("I'm child 1\n");
printf("child 1: parent: %i\n", getppid());
printf("child 1: my pid: %i\n", getpid());
} else {
int fork2 = fork();
if (fork2 < 0) {
printf("error\n");
} else if (fork2 == 0) {
printf("I'm child 2\n");
printf("child 2: parent: %i\n", getppid());
printf("child 2: my pid: %i\n", getpid());
} else {
printf("I'm the parent\n");
printf("The PIDs are:\n");
printf("parent: %i\n", getpid());
printf("child 1: %i\n", fork1);
printf("child 2: %i\n", fork2);
}
}
return 0;
}
fork() returns two different values, it returns zero to the child and returns non-zero positive value to the parent, this value is the child process ID, so in your last else, fork1 is the pid of child1 and fork2 is the pid of child2.

Waiting for all child processes to be created - C

Let's suppose I have a code like the following
switch (fork()) {
case -1:
//error checking
break;
case 0:
//child code
break;
default:
int i;
for (i = 0; i < n; i++) {
switch (fork()) {
case -1:
//error checking
break;
case 0:
//exec
break;
default:
//parent that waits for all childs to be created
break;
}
}
}
How do I make the second parent process wait for all the other processes to be created exactly ...
I was told I had to make a loop but I don't know how to implement it exactly. Supposing there are n child processes.
I think you may have misunderstood the requirement slightly. The term 'second parent' doesn't make a lot of sense to me.
What makes most sense as a requirement is:
Parent process launches N children.
Each child does its appropriate stuff.
The parent process must then wait for all N children to complete.
Then it can report its own completion (or get on with other work, or ...).
In outline, you would then have:
int pid;
for (int i = 0; i < N; i++)
{
switch (pid = fork())
{
case 0:
be_childish(i);
/*NOTREACHED*/
break;
case -1:
// Print error report
break;
default:
printf("Started PID %d\n", pid);
break;
}
}
int status;
while ((pid = wait(&status)) > 0)
{
printf("PID %d exited (status 0x%.4X)\n", pid, status);
}
printf("All done!\n");
Note the /*NOTREACHED*/ comment. I assume that the child process exits from within the be_childish() function. The code could ensure no damage by including an exit(1); or perhaps _exit(1); or _Exit(1);. It is rather important that a child process does not continue the loop.

Resources