finding children processes of a particular process (UNIX) - c

i am required to produce something similar to ptree in solaris for my assignment.
to do something like that i would need the child PID of the processes.
i already have the list of processes running on the machine but the only information i have are the parent PID of each processes. is there any way to fetch the child PID of each process in the system?
the program will be ran on a solaris machine.

You said you already have the list of processes and their parents. To find the child PIDs of any process, simply loop over the processes looking for ones whose parent PID is the PID of the process you're investigating.

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.

How to child process can print its root parents ID

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.

Get child list of parent process in C

I have a PID of process that may contain childs. How can I get the PID of all child processes? I make my own PTY handler, so when user run a shell in this handler he may run anymore programs ( directly from shell ), every ran program becomes a child of shell. So, when I press Ctrl+C I need to send signal to the most new process, so need to know PID of that last one.
You should keep explicitly all the pids (result of fork(2)...) of your child processes (and remove a pid once you waited it successfully with wait(2) etc...)
It is up to you to choose the data structures to keep these pids.
Any other approach (e.g. using proc(5)... which is what ps and pstree are doing.) is not very portable and inefficient.
So the basic rule is that every time you call fork you should explicitly keep its result (and test for the 3 cases: 0 if in child process, >0 if in parent process, <0 on error) and use that at wait time.
Read Advanced Linux Programming; it has many pages relevant to that subject.
You might also be interested by process groups and sessions. See setpgrp(2), setsid(2), daemon(3), credentials(7) etc. Notice that with a negative or zero pid kill(2) can send a signal to a process group, and that you could also use killpg(2) for that purpose.

number of child processes

Is there relyable way in UNIX to know how many child processes has my certain child process? For example, main process forks child process which exec login program, can i know whether login forked or not?
UPD i'm using C
Children are linked via their ppid (parent pid) to the parent, so it's just a matter of following those links, depending on what language you use to implement that. The pstree command uses this to display the process tree.
Regarding "reliable", you have to handle processes appearing and disappearing all the time, best is to snapshot the running processes as fast as possible, and only then analyze the data.
You have to go through all processes, checking their PPID (Parent Process ID) and compare that to the PID of the process you want the children of.
It's crude, but you could just use a popen version of what this page is doing, and parse the returned values.
Instead of grep-ing for httpsd you grep for your process. Or you could use the --ppid switch on the ps command and just get all the child processes of this parent process.

Resources