How to child process can print its root parents ID - c

If in a simple C program I am writing 4 calls to the fork() API and when I execute this program then the total processes created will be 16 on Linux.
Using getpid() can get the process id of that current process.
Using getppid() can get the parent process id of the calling process.
Question: How can the last child know the process id of the first process
(the ancestor's parent id) from which I call the fork() API four times?
NOTE: The assumption is that all processes are running; nobody died.

Just use
first = getpid()
before all the forks, and use that variable in the children.

1) You can use process tree command called ps l (for ppid seperate table is there.)
2) Using getppid() you can check recursively and reach to main process. Here you need some smart logic for this.

Related

How can I check when a Linux process and ALL its children exited?

I write a program in C. I do fork() in the main process in order to do execve() in the forked child process to execute an unknown app (given by a user in the command line). I know a PID of the process of the executed app - it is returned by fork(), but this unknown app can possibly fork() many times and I do not know PIDs of all its children (they are grandchildren of the main parent process). How can I check in the main parent process WHEN its child process (it is the unknown app) and ALL children of the unknown app exit? (I do not know even how many children it can have and I do not know PIDs of these children).
This can be done by making your parent process a subreaper. A subreaper gets all children orphaned by its descendants, which would traditionally always go to init (process ID 1). The subreaper status needs to be enabled before forking the interesting child process. Once this is done, a waitpid() or similar call for any process will return the child process and all orphaned descendants until it returns error [ECHILD] when the entire tree is gone.
On Linux, this is enabled using prctl()'s PR_SET_CHILD_SUBREAPER option and on FreeBSD this is enabled using procctl() PROC_REAP_ACQUIRE command (see man pages for details).
On Linux you will be able to monitor only one child process individually this way, since the orphans do not remember from which original fork call they came. On FreeBSD, PROC_REAP_GETPIDS allows distinguishing individual subtrees, although this is less efficient if the tree contains many processes.
You can use waitpid(-1,NULL, WNOHANG) to tell if one child has exited. If you receive a positive number (a pid) then one child has exited. In your parent process you have a line that checks if the amount of child processes you have, here called x, is more than 0. if it is use this command to see if any child process has ended. If you have x items then when you add an item increment x and when one exits decrement x. When x, the amount of children you have, is zero all you children have been killed.

How to name processes and insert values in respective process as named in UNIX

I need to create processes using fork and assign respective values to them according to their names/labels. And then transfer the values using pipes() in C language. My question is, is there a way to name processes?
Each process has an unique identifier, PID. In Linux (as well as in all POSIXy and Unix systems) these identifiers are positive integers, with 1 being special, init.
(By unique, above, I mean that every active (running) process and zombie process have their own PID, and no two share the same PID. However, old PIDs are eventually reused, sometimes within seconds; it depends on the system. Do not expect the PID of a dead process to stay unused!)
When you fork a child process, fork() returns (pid_t)0 in the child process, and the child process PID in the parent process. By storing the PIDs of the child processes you create in an array, the parent process can tell which child is which.
Each process can always call getpid() to find out what it's own PID is.
A process can also call getppid() to find out the PID of their parent process (the PID of the process that called fork() that created this process). However, if the real parent has already died/exited, getppid() will return 1. (This is also half of the reason we have an init process. The init process is responsible for exactly two things: being the parent of orphan processes, and reaping them when they die; and to start all the other processes when the operating system boots up.)
Neither getpid() or getppid() can ever fail; you can rely on them to always return the respective PIDs.
Overall, this kind of design is extremely common in practice. All daemons that use multiple processes to do their work, do basically exactly this. It means this exercise is relevant in practice, too.

Running a separate process within a parent process

I am creating a parent process that creates a child using fork(), and then creates a shared memory block that can be accessed by both the parent and the child. I have created child processes using the WIN32 api (by creating several .exe files and running them all and then calling them using the createProcess() function), but am a bit unsure of the steps for linux.
I have created a separate C file that contains the child's code. I assume I will create the child process using fork, then while in the child process (pid == 0) use an exec() command to call the other C file? Do I need to first run the other file? Or can it just be sitting in the same directory?
Any help is appreciated
Thanks
After fork (), there are two processes, the parent and the child. The fork system call in the parent process returns the process id of the child. In the child process, fork () returns 0. So, if the return value of fork () is zero, you can call an exec system call with the executable file name of the program for the child process.

How can I split a process in C in different processes?

How can I split processes using C?
For example, in bash, I can use the command:
screen
You need to use fork() function.
The only way in C to create a new child process is the system call fork. All new process on UNIX/Linux are created with fork.
The child process is a copy of the parent process, the return value of fork call indicate if the current process is the parent (>0 : PID of the child process) or the child (0) or if the child process cannot be created (-1).

c language problem

i have a c problem can any one help me . i wrote a code of process creation in c language , it uses pid & fork() call. the fork call is use for making child process now can any body tell me how to make parent process? i know that creating a child in process make the process parent automatically but i want to make a parent process right from the start so can any one tell me how to do this,
Secondly i just create the process i don't know how to use it i cant assign any work(computation) to it.so can any one tell me how to use the process for work?
Third one i want to give a name to my process how can i do it & how i can control their execution?
please if anyone can enlighten me than please help me to understand all this.
i shall be thank full for this forever
thanks in advance
The fork call creates a new process that is identical to the existing process except for a few minor differences such as its pid, parent pid. The original process carries on from exactly the same place and this is the parent process. Which means your question is basically meaningless. You don't create the parent process, the original process becomes the parent process once it creates a child process.
It's a bit like asking "I created a child by getting pregnant and giving birth, but how do I create the parents?" You are a parent automatically.
Back to computers...
When you fork, the system call returns the pid of the child to the parent and 0 to the child, so you should have code something like:
int pid = fork();
if (pid == 0)
{
// in child, do child processing - normally exec an executable
}
else if (pid > 0)
{
// in parent, do some processing - often wait for child to complete
}
else
{
// fork failed - handle the error
}
When you fork a process, one process becomes two processes. Each continues running at exactly the same place. The only difference is that the fork returns the PID of the child process to the parent process and returns the value 0 to the child process.
Without any help, the child process does not know its parent. If the two processes need to communicate with one another then they will need to use some sort of IPC mechanism.
A common form of IPC is pipe. If one opens pipes before forking, then both the child and the parent keep the open file descriptors. This will allow both processes to communicate with one another. The parent is now free to communicate its PID to the child process if so desired.
Secondly i just create the process i don't know how to use it i cant assign any
work(computation) to it
You need to use fork and exec in pair make it run the program you want to execute.
Here is the wiki link for more information.
Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. fork() is the name of the system call that the parent process uses to "divide" itself ("fork") into two identical processes. After calling fork(), the created child process is actually an exact copy of the parent - which would probably be of limited use - so it replaces itself with another process using the system call exec().
To create a parent process, take your code for creating a child and reverse the roles of parent and child. Presto change-o, the new process is the parent and the old process is the child.
For communications between the processes, use a pipe(2), or several. Also, there is shared memory.
To control execution, use kill(2) and wait(2). I'm not sure about assigning names, we might need to know what platform you're on.
About renaming (I assume you mean the name displayed by ps), to "rename" a process, just copy your new name into argv[0]

Resources