Creating three children in C that have the same parent - c

My problem is that the children does not have the same parent and does not appear correctly, here is my code:
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid[3];
pid[0] = fork();
if(pid[0] == 0)
{
/* First child */
pid[1] = fork();
if(pid[1] == 0)
{
/* First child continued */
printf("Hello, I'm the first child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
else
{
/* Second child */
pid[2] = fork();
if(pid[2] == 0)
{
/* Second child continued */
printf("Hello, I'm the second child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
else
{
/* Third child */
printf("Hello, I'm the first child! My PID is : %d, My PPID is: %d", getpid(), getppid());
sleep(1);
}
}
}
else
{
/* Parent */
sleep(1);
wait(0);
printf("Hello, I'm the parent! My PID is : %d, My PPID is: %d", getpid(), getppid());
}
return 0;
}
As of right now when i run the program i will get this as output in bash, where bash has the PID of 11446:
>Hello, I'm the third child! My PID is: 28738, My PPID is: 28735
>Hello, I'm the first child! My PID is: 28742, My PPID is: 28738
>Hello, I'm the second child! My PID is: 28743, My PPID is: 28738
>Hello, I'm the parent! My PID is: 28753, My PPID is: 11446
How do i get the first child to appear first, second child to appear second and the third child to appear last, and get all the children to have the PPID 28753

From man fork:
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in
the parent, no child process is created, and errno is set
appropriately.
Your if-else conditions are swapped.

Related

How to obtain the child process PID in C

In order to get the PID of the child process I am doing this.
pid_t pid;
pid=fork();
if(pid==0){
//child's work
}else{
printf("The child's PID is %d",pid);
}
I want to print the child's pid from parent! So is it ok if I printf pid or do I need to use getpid()?
On success fork returns the pid of the child process, so pid will be the pid of the child process, So I'd say it's correct.
getpid is to get the pid of the current process, so it's not suited to get the child's pid.
I think this sums up the question :
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("In child => Own pid : %d\n", getpid());
printf("In child => Parent's pid : %d\n", getppid());
}
else{
printf("In Parent => Child's pid is %d\n", pid);
printf("In Parent => Own pid : %d\n", getpid());
}
return 0;
}

Creating of specific process tree

I'm trying to create a process tree as on image. Down below is my code, that i wrote. It works properly but only a half. The output of my code is on the second screenshot. The problem is, that i don't know, how to make the last generation.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int i;
int j;
pid_t ppid;
pid_t cpid;
ppid = getpid();
printf("I'm the parent, my PID is: %d\n", ppid);
for (i = 0; i < 4; i++) {
ppid = fork();
if (ppid == 0) {
printf("Hello, my PID is: %d, my parent's PID is %d\n", getpid(), getppid());
for(j = 0; j < 2; j++) {
ppid = fork();
if (ppid == 0) {
printf("Hello, my PID is: %d, my parent's PID is %d\n", getpid(), getppid());
sleep(60);
printf("I'm process %d and I'm done\n", getpid());
exit(0);
}
}
sleep(60);
printf("I'm process %d and I'm done\n", getpid());
exit(0);
}
}
sleep(1);
printf("I'm process %d. Waiting for one of my children to complete", getpid());
wait(NULL);
printf("Eltern: I'm done\n");
printf("... and bye. \n");
}
If I understand your question correct, you just need to add a fork in the inner most if-statement.
if (ppid == 0) {
printf("Hello, my PID is: %d, my parent's PID is %d\n", getpid(), getppid());
// Add one more fork here
sleep(60);
printf("I'm process %d and I'm done\n", getpid());
exit(0);
}

getpid() returns unexpected value

I am trying to run this piece of hello world code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
char string1[] = "\n Hello";
char string2[] = " World.\n";
int main(void)
{
int childpid;
if((childpid = fork() ) == -1){
printf("\n Can't fork.\n");
exit(0);
}
else if(childpid == 0){ /* Child process */
printf("\n Child: My pid = %d, Parent pid = %d \n", getpid(), getppid());
exit(0);
}
else{ /* Parent Process */
printf("\n Parent: Child pid = %d, My pid = %d, Parent pid = %d \n", childpid, getpid(),getppid());
exit(0);
}
}
The output I am getting is different each time with regard to the child's parent PID :
~$ ./f
Parent: Child pid = 6394, My pid = 6393, Parent pid = 27383
Child: My pid = 6394, Parent pid = 1842
~$ ./f
Parent: Child pid = 6398, My pid = 6397, Parent pid = 27383
Child: My pid = 6398, Parent pid = 6397
When I checked to which process the pid = 1842 belongs to I found that it is the pid of /sbin/upstart --user. Anybody has an interpretation of these results please.
You have race condition. Sometimes, your parent finishes a little bit faster, these are the cases when you see this strange result (different parent ID).
Sleep for a while inside parent and see what happens.
In fact, you can wait for your child: waitpid
...
int status;
waitpid(childpid, &status, 0);
printf("\n Parent: Child pid = %d, My pid = %d, Parent pid = %d \n", childpid, getpid(),getppid());
exit(0);
...

Parent/Child process print

I'm writing a C program that creates a child process. After creating the child process, the parent process should ouput two messages: "I am the parent" then it should print "The parent is done". Same for child process "I am child" and "The child is done". However I want to make sure, the second message of the child is always done before the second message of the parent. How can I achieve to print "The child is done" and "The parent is done" rather than printing their pid?
#include <unistd.h>
#include <stdio.h>
main()
{
int pid, stat_loc;
printf("\nmy pid = %d\n", getpid());
pid = fork();
if (pid == -1)
perror("error in fork");
else if (pid ==0 )
{
printf("\nI am the child process, my pid = %d\n\n", getpid());
}
else
{
printf("\nI am the parent process, my pid = %d\n\n", getpid());
sleep(2);
}
printf("\nThe %d is done\n\n", getpid());
}
You could have a flag variable, that is set in the parent, but then the child clears it. Then simply check for that for the last output.
Something like
int is_parent = 1; // Important to create and initialize before the fork
pid = fork();
if (pid == -1) { ... }
if (pid == 0)
{
printf("\nI am the child process, my pid = %d\n\n", getpid());
in_parent = 0; // We're not in the parent anymore
}
else { ... }
printf("\nThe %s is done\n\n", is_parent ? "parent" : child");
Call wait(2) in the parent process for the child to complete.
else
{
wait(0);
printf("\nI am the parent process, my pid = %d\n\n", getpid());
}
You should check if wait() succeeds and main()'s return type should be int.

Tracing the execution of a process

I have this code that gets its process ID and its parent process:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int pid;
printf("I am the original process with PID %d and PPID %d. \n", getpid(), getppid());
pid = fork();
if (pid >0){
printf("I am the original process with PID %d and PPID %d \n", getpid(), getppid());
printf("My child’s pid is %d \n" , pid);
}
else if(pid == 0) {
printf ("I am the original process with PID %d and PPID %d \n", getpid(), getppid());
}
else if (pid == 1){
printf ("Error – no child process was created \n");
}else{
printf ("Error – system error \n");
}
printf (" PID %d terminates \n", getpid()); /* both processes execute this instruction */
return 0;
}
OUTPUT
I am the original process with PID 1009 and PPID 964.
I am the original process with PID 1009 and PPID 964
My child’s pid is 1010
PID 1009 terminates
I am the original process with PID 1010 and PPID 1009
PID 1010 terminates
Few questions that confuses me..
how does this code is executed?
In the output, you can see that it runs the code under under if(pid == 0) while the condition if(pid > 0) is already executed. how come pid is equal to 0? while it was set already to greater than 0.
Lastly , what does fork() really do?
fork() makes multiple processes, or a "child". So the parent and child execute the code. The parent's pid is > 0 and the child's is ==0.
So, the parent and child execute at similar times starting at the fork command. So let's start with the parent. The parent checks the first statement (pid > 0) and finds it is true, so it prints out the two statements. Then it goes all the way to the print statement after the last else.
Now to the child. The child checks the first if statement, and is false. Checks the next (pid == 0) and finds it is true. So now it's going to print out that statement. Now it will skip to the print statement after the else and prints again.
Note: The parent and child can execute at different times, so if you run the code multiple times the output may be in a different order.

Resources