getpid() returns unexpected value - c

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

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;
}

How would you change the code so that the child and parent run concurrently?

I am struggling to understand this question. Aren't the child and parent already running concurrently? Would the answer be to remove wait(NULL)? Please help.
Here the Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main (int args, char *argv[])
{
pid_t fork_return;
pid_t pid;
pid=getpid();
fork_return = fork();
if (fork_return==-1) {
// When fork() returns -1, an error has happened.
printf("\nError creating process " );
return 0;
}
if (fork_return==0) {
// When fork() returns 0, we are in the child process.
printf("\n\nThe values are Child ID = %d, Parent ID=%d\n", getpid(), getppid());
execl("/bin/cat", "cat", "-b", "-v", "-t", argv[1], NULL);
}
else {
// When fork() returns a positive number, we are in the parent process
// and the return value is the PID of the newly created child process.
wait(NULL);
printf("\nChild Completes " );
printf("\nIn the Parent Process\n");
printf("Child Id = %d, Parent ID = %d\n", getpid(), getppid());
}
return 0;
}

Create multiple generations of children process

So I'm trying to understand the concept of grandchildren.
I'm able to create a given number of sons (i.e brothers) but I don't know how to create multiple generations .
This is what I did to create one grandson :
int main ()
{
//Displaying the father
printf("Initial : Father = %d\n\n", getpid());
/****** FORK *******/
pid_t varFork, varFork2;
varFork = fork();
if(varFork == -1) //If we have an error, we close the process
{
printf("ERROR\n");
exit(-1);
}
else if (varFork == 0) //if we have a son, we display it's ID and it's father's
{
printf("SON 1\n");
printf(" ID = %d, Father's ID = %d\n", getpid(), getppid());
varFork2 = fork();//creation of the second fork
if(varFork2 == -1) //If we have an error, we close the process
{
printf("ERROR\n");
exit(-1);
}
else if (varFork2 == 0) //now we have the son of the first son, so the grandson of the father
{
printf("\nGRANDSON 1\n");
printf(" ID = %d, Father's ID = %d\n", getpid(), getppid());
}
else sleep(0.1);/*we wait 0.1sec so that the father doesn't die before we can
display it's id (and before the son process gets adopted by a user process descending from the initial process)*/
}
else //in the other case, we have a father
{
sleep(0.1);//again we put the father asleep to avoid adoption
}
return 0;
}
How can I create X generations of grandsons, X being a global variable (1son, 1 grandson, 1 great-grandson, etc.) ?
How can I create X generations
Before forking, decrement X and continue forking inside the child until X is 0.
Or inside the child decrement X and only continue forking if after decrementing it X still is greater 0.
The code might look like this:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
void logmsg(const char *, int);
#define MAX_GENERATIONS (4)
int main(void)
{
pid_t pid = 0;
for (size_t g = 0; g < MAX_GENERATIONS; ++g)
{
logmsg("About to fork.", 0);
switch (pid = fork())
{
case -1:
{
logmsg("fork() failed", errno);
break;
}
case 0:
{
logmsg("Hello world. I am the new child.", 0);
break;
}
default:
{
char s[1024];
snprintf(s, sizeof s, "Successfully created child carrying PID %d.", (int) pid);
logmsg(s, 0);
g = MAX_GENERATIONS; /* Child forked, so we are done, set g to end looping. */
break;
}
}
}
logmsg("Sleeping for 3s.", 0);
sleep(3);
if (0 != pid) /* In case we forked a child ... */
{
logmsg("Waiting for child to end.", 0);
if (-1 == wait(NULL)) /* ... wait for the child to terminate. */
{
logmsg("wait() failed", errno);
}
}
logmsg("Child ended, terminating as well.", 0);
return EXIT_SUCCESS;
}
void logmsg(const char * msg, int error)
{
char s[1024];
snprintf(s, sizeof s, "PID %d: %s", (int) getpid(), msg);
if (error)
{
errno = error;
perror(s);
exit(EXIT_FAILURE);
}
puts(s);
}
The output should look similar to this:
PID 4887: About to fork.
PID 4887: Successfully created child carrying PID 4888.
PID 4887: Sleeping for 3s.
PID 4888: Hello world. I am the new child.
PID 4888: About to fork.
PID 4888: Successfully created child carrying PID 4889.
PID 4888: Sleeping for 3s.
PID 4889: Hello world. I am the new child.
PID 4889: About to fork.
PID 4889: Successfully created child carrying PID 4890.
PID 4890: Hello world. I am the new child.
PID 4890: About to fork.
PID 4889: Sleeping for 3s.
PID 4890: Successfully created child carrying PID 4891.
PID 4890: Sleeping for 3s.
PID 4891: Hello world. I am the new child.
PID 4891: Sleeping for 3s.
PID 4888: Waiting for child to end.
PID 4890: Waiting for child to end.
PID 4891: Child ended, terminating as well.
PID 4890: Child ended, terminating as well.
PID 4887: Waiting for child to end.
PID 4889: Waiting for child to end.
PID 4889: Child ended, terminating as well.
PID 4888: Child ended, terminating as well.
PID 4887: Child ended, terminating as well.
The difference with my code above compared to the proposal I made is that it counts upwards to X.

Creating three children in C that have the same parent

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.

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.

Resources