What are PIDs used for? [closed] - pid

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I know how to get a PID of a running process in Ubuntu, but aside from killing a running process, what else can the PID be used for?

The question is a bit broad. Maybe it helps to explain what a PID is:
Meaning of PID
PID = Process ID = Process Identifier
Every process on your system is assigned an unique number. This number is the PID. The PID is only there so that you can specify a process unambiguously. So the question should rather be:
What operations can be done on processes.
Operations on Processes
Sending signals (with kill). Signals can be used for more than just killing programs, for instance dd can output its current progress when sending the USR1 signal.
Changing priority (with renice). Processes have a priority which determines how much CPU time they get compared to other processes. The priority can be changed by you.

Related

Can I use 0 as a signal parameter in pthread_kill [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I know that pthread_kill requires Signal as a parameter but I am just curious about that what will be happen if I will give 0 as a signal (no SIGUSR1 or any other) .
Thread will be still killed in above situation ?
I am trying to learn these stuffs I am putting my hands in threading in C.
It will happen exactly what is described in the man page :
If sig is 0, then no signal is sent, but error checking is still performed.
That means it will act like pthread_kill has send the signal, but without sending it. It's usefull when you try to check wether a particular PID respond to signal or not. It's a "dry-run".
Edit : Well, out of habit I've writted "PID", but it's more the "TreadId" than any other things.

exec family function in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
As we know that exec family function calls eventually calls execve() which is a system call and remaining are library functions. So, typically, whatever implications applies on execve() are true for other exec*() functions as well.
I would like to know if, during execution of execve(), all signals are blocked until it succeed, or if there is a way to pass signal to that pid which corresponds to exec? (I know it does not return on success and further execution of calling function does not happen)
I am not sure I got your question right, so feel free to correct me if I am wrong.
But, basically, yes, system calls can be considered as 'atomic' from the process point of view. So, once the execve() system call is started, only the kernel has the hand on it and it won't release the process until running the new command or failing with an error code or raise the SIGKILL signal (as SIGKILL is the only unblockable signal).
But, once the execve() spawned a new process (and returned from the kernel), it is perfectly interruptible with any signal.

How to check if child process is finished in c by its pid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am building a very tiny shell in c,
I have the option on running programs in the background.
I keep a list of all my jobs meaning all the ones in the bg.
Now if i want to go and update this list, how can i check is a process is finished or if its still running.
ps
if i waited with waitpid for some process, will i still be able to check if the process is done? (i mean if i used waitpid it took the process of zombie state.
You should be able to call waitpid, passing it the process id and the WNOHANG option and call the WIFEXITED macro on the integer returned through status argument. See Just check status process in c.

Is it possible to get parent threadID from child? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to add instrumentation into my code that will print out something like
'Thread 1 forks Thread 2'
Any suggestions on how I can achieve this?
Terminology correction: one thread may create another thread, not fork, which is usually
used to mention one process forking another.
No, a thread has no way to get another thread's identifier. On Linux, you can check if gettid() == getpid() to find if it's the main thread. Solaris has thr_main() to identify if the caller is main thread or not. FreeBSD has pthread_main_np() for the same purpose.
But there's no way to identify parent-child relationship between any threads. Any thread can create more threads. You'll have to use pass the thread identifiers around when creating threads or use global data structure to maintain this information.

How to know whether a running application has exited and based on that do something in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do i know that a process [not called by c] has exited and based on that do something in C?
For eg. there is a running application , say notepad. I create an application to delete the text file created by it. I cannot do that while it is open . So how do I know when notepad exits and based on that take a decision in C.,.,
If you know the PID of the process, you can use the kill() function. Sending signal 0 to the process is guaranteed not to kill it, but merely to report on whether it is running.
If you don't know the PID of the process, there is often a pidfile on disk for the process concerned (e.g. /var/run/processname.pid) - look at the file that starts it for more information. If this PID file is not there, the process should not be running. If it is there, the file contains (as text) the PID of the process, which you can check using the method above.
If you do not know the PID of the process and it does not have a PID file, then you will have to walk /proc, or shell out to pgrep or similar. Avoid this if possible.

Resources