linux process with status 'D' mean TASK_UNINTERRUPTIBLE, so I write a programer to test it on Ubuntu 16.04   My code:  #include &lt;unistd.h&gt; #include &lt;iostream&gt; using namespace std;  int main(void) {     pid_t  pid = vfork();     if (pid == -1) {         cout&lt;&lt;"for error"&lt;&lt;endl;     } else if (pid == 0) {         cout&lt;&lt;"this child is "&lt;&lt;getpid()&lt;&lt;endl;         sleep(1000);     } else {         cout&lt;&lt;"this parent is "&lt;&lt;getpid()&lt;&lt;endl;     }     return 0; }   after I run the programmer, the status of the parent process is 'D'   When I use kill command to kill the parent process, the parent process was be killed successfully, so anything wrong in my code ?   Thanks very much !!!
122537
Sometimes whenever I write a program in Linux and it crashes due to a bug of some sort, it will become an uninterruptible process and continue running forever until I restart my computer (even if I log out).  My questions are:   What causes a process to become uninterruptible? How do I stop that from happening? This is probably a dumb question, but is there any way to interrupt it without restarting my computer?
82797
If I start a process and then delete the binary of it, I can still recover it from /proc/&lt;pid&gt;/exe:  $ cp `which sleep` . $ ./sleep 10m &amp; [1] 13728 $ rm sleep $ readlink /proc/13728/exe                            /tmp/sleep (deleted) $ cp /proc/13728/exe ./sleep-copy $ diff sleep-copy `which sleep` &amp;&amp; echo not different not different $ stat /proc/13728/exe    File: ‘/proc/13728/exe’ -&gt; ‘/tmp/sleep (deleted)’   Size: 0           Blocks: 0          IO Block: 1024   symbolic link   On the other hand, if I make a symbolic link myself, delete the target and attempt to copy:  cp: cannot stat ‘sleep’: No such file or directory   /proc is an interface to the kernel. So does this symbolic link actually point to the copy loaded in memory, but with a more useful name? How does the exe link work, exactly?
69935
Before systemd, one used to be able to see a shutdown process in the list of processes (e.g. ps -ef | grep shutdown).  Now however, the most recent advice I can find is to use systemctl status, but I'm not having any luck:  root@m________a:~# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description:    Ubuntu 16.04.1 LTS Release:    16.04 Codename:   xenial root@m________a:~# shutdown -r 14:00 Shutdown scheduled for Mon 2016-10-10 14:00:00 BST, use 'shutdown -c' to cancel. root@m________a:~# ps -ef | grep "shutdown" root     10584 10508  0 13:44 pts/0    00:00:00 grep --color=auto shutdown root@m________a:~# systemctl status systemd-halt.service ● systemd-halt.service - Halt    Loaded: loaded (/lib/systemd/system/systemd-halt.service; static; vendor preset: enabled)    Active: inactive (dead)      Docs: man:systemd-halt.service(8) root@m________a:~# systemctl status systemd-reboot.service  ● systemd-reboot.service - Reboot    Loaded: loaded (/lib/systemd/system/systemd-reboot.service; static; vendor preset: enabled)    Active: inactive (dead)      Docs: man:systemd-halt.service(8) root@m________a:~# systemctl status systemd-poweroff.service  ● systemd-poweroff.service - Power-Off    Loaded: loaded (/lib/systemd/system/systemd-poweroff.service; static; vendor preset: enabled)    Active: inactive (dead)      Docs: man:systemd-halt.service(8)   I would be very grateful if anyone can explain how I can detect a pending scheduled shutdown and, indeed, why such a shutdown doesn't seem to show up using systemctl
118279
I'm creating child processes with fork() in C/C++. When the parent process ends (or is killed for some reason) I want all child processes to be killed as well. Is that done automatically by the system? Or I have to do it myself?  Thanks.    Pre-existing similar questions:
27686
I'm going through , Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel. It's from 2001, so a bit old. But I find it quite good anyhow.  However, I got to a point when it diverges from what my Linux produces in the shell output. On page 92 (116 in the viewer), the chapter 4.5 GNU/Linux Thread Implementation begins with the paragraph containing this statement:     The implementation of POSIX threads on GNU/Linux differs from the   thread implementation on many other UNIX-like systems in an important   way: on GNU/Linux, threads are implemented as processes.   This seems like a key point and is later illustrated with a C code. The output in the book is:  main thread pid is 14608 child thread pid is 14610   And in my Ubuntu 16.04 it is:  main thread pid is 3615 child thread pid is 3615   ps output supports this.  I guess something must have changed between 2001 and now.  The next subchapter on the next page, 4.5.1 Signal Handling, builds up on the previous statement:     The behavior of the interaction between signals and threads varies   from one UNIX-like system to another. In GNU/Linux, the behavior is   dictated by the fact that threads are implemented as processes.   And it looks like this will be even more important later on in the book. Could someone explain what's going on here?  I've seen this one , but it doesn't help much. I'm confused.  This is the C code:  #include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt;  void* thread_function (void* arg) {     fprintf (stderr, "child thread pid is %d\n", (int) getpid ());     /* Spin forever. */     while (1);     return NULL; }  int main () {     pthread_t thread;     fprintf (stderr, "main thread pid is %d\n", (int) getpid ());     pthread_create (&amp;thread, NULL, &amp;thread_function, NULL);     /* Spin forever. */     while (1);     return 0; }
109593
Let's assume process runs in ulimited environment :  ( ulimit  ... -v ... -t ... -x 0 ... ./program )   Program is terminated.  There might be many reasons : memory/time/file limit exceeded ; just simple segfault ; or even normal termination with return code 0.  How to check what was the reason of program termination, without modifying program?  P.S. I mean "when binary is given". Maybe some wrapper (ptrace-ing etc) might help?
96984
Consider the following script:  #!/bin/bash  set -o pipefail set -o history  trapper() {    func="$1" ; shift    for sig ; do       trap "$func $sig" "$sig"    done }  err_handler () {   case $2 in     INT)        stop_received=1     ;;     TSTP)     ;;     ERR)        if [[ $2 != "INT" ]]; then # for some reason, ERR gets triggered on SIGINT           code=$?           if [ $code -ne 0 ]; then              echo "Failed on line $1"              echo "$BASH_COMMAND returned $?"              echo "Content of variables at the time of failure:"              echo "$(set -o posix; set)"              exit 1           fi        fi     ;;   esac }  main() {    ping -c 5 www.google.com # this is a test to see if INT interrupts main()    # do a bunch of stuff, I mean a real bunch of stuff, like check some    # files, do some processing, connect to a database,    # do more processing, move some files around, you get the drift }  exec &gt; &gt;(tee -a my.log) exec 2&gt;&amp;1  trapper 'err_handler $LINENO' INT TSTP ERR while main do   if [[ "$stop_received" == "1" ]]; then      break   fi   setsid sleep 2 &amp; wait done trap ERR   What I am trying to accomplish is to run the script in an infinite loop until either the function main() returns some non zero value, i.e. some error occurred, or SIGINT is received.  However, I don't want SIGINT to stop main() from executing, in other words, if the script receives a SIGINT, it should wait for main() to finish, then exit nicely. But when I hit CTRL+C, I can see that ping is interrupted. At the moment I commented out everything under main() just to see if this works. Since ping gets interrupted, I am assuming other commands under main() would also get interrupted. When ping is interrupted, the processing jumps to the line where I check if $stop_received=1 and then the loop breaks and the script quits. If I replace the break with just an echo, then the script just continues on to the next iteration of the while main loop.  How can I stop SIGINT from interrupting the currently running command(s)? Since my script does a bunch of stuff, including DML statements in a database, interrupting main() would cause lot of grief.  Secondly, the script does not trap ctrl+z either. Or rather, the script just gets stuck on ctrl+z requiring a kill pid to terminate. I assumed that sleep being a child of bash as opposed to the script itself, ctrl+z would cause the script to pause, leaving sleep in limbo land. Hence the setsid and wait on sleep, but it still hangs.  thanks.
4366
I imagine that kill -9 still just sends a signal to a process. If the process is not behaving well, I would imagine that kill -9 would have no effect. Indeed, today for the first time I saw kill -9 have no effect when sent to a stuck ruby process. So here is my question: Is there some harsher way to kill a process?
1123
What is the primary difference between the two? I always see this in the right click menu in windows. What happens if you end a process that is relied upon by another process? Is there a way to "kill" a process tree in linux or is the technology different?
84046
Our application uses an init.d script to start and stop the application as a service. Under CentOS 7, /sbin/init is symlinked to systemd, so I can start my application using either:  service myapp start   or   systemctl start myapp   The issue I am having is that running stop on the service using either service or systemctl will not stop my application. The output of systemctl status:  [root@nec04 ~]# systemctl status myapp myapp.service - SYSV: Service script to start/stop my application    Loaded: loaded (/etc/rc.d/init.d/myapp)    Active: inactive (dead) since Mon 2015-10-05 15:17:41 CEST; 22h ago   Process: 31850 ExecStop=/etc/rc.d/init.d/myapp stop (code=exited, status=0/SUCCESS)   Process: 21054 ExecStart=/etc/rc.d/init.d/myapp start (code=exited, status=0/SUCCESS)   Using the service command:  [root@nec04 ~]# service myapp status Local database at :3307 is started Watchdog is running Application is running   Why does systemctl think my application is not running? Could it be that systemctl is not calling the stop function because it thinks my application is already stopped?
20992
I have process id 1234. This process contains thread id 1235. When I use ls -l /proc I see only the pid (1234) but when I open the thread status file using cat /proc/1235/status I can see data. Why is that? Can I access with C code directly with tid? /proc/1235/mem without knowing the process id?
89313
Is there a way to kill a process tree using the terminal?  By killing a process tree, I mean killing all descendants of a process along with the process it self.  I tagged this question htop because I am using that utility a lot these days and it would be cool if there was an easy way to kill a process tree inside htop (something better than just selecting all the processes in the subtree).
2490
I have a process I can't kill with kill -9 &lt;pid&gt;. What's the problem in such a case, especially since I am the owner of that process. I thought nothing could evade that kill option.
66030
Can somebody please explain when parent process receives the exit status of a dead child process via wait, who actually reallocates the memory of the child process and removes it from the process table?
80589
How can I determine if a Win32 thread has terminated?  The documentation for  warns to not to use it for this reason since the error code STILL_ACTIVE can be returned for other reasons.  Thanks for the help! :)
42649
I'm studying 'operation system concepts' on my own and I'm studying the chp3. process part.  There is an example where the 'fork()' function is called and depending of the returned pid value like the following:  pid=fork(); if(pid&lt;0){ //error stuff } else if(pid==0){ // child process stuff } else{ // parent process stuff }   What confused me here is that if this code is executed, among the three scenarios of 'if's, only one would be executed which means that only one out of parent/child procedures would be executed.  But reading a bit more carefully, I found a sentence that kind of helped me solve the confusion but not entirely.     The new process consists of a copy of the address space of the   original process.   From my imagination, I guess this means that whenever the 'fork()' calls is executed, somehow an exact copy of this code will be duplicated and be run as 'child' process when the original c code will be run as 'parent'.  Am I understanding this right?  Also, what does the 'address space' have to do with this? Again, using my imagination I assume that, since the execution of parent code means that the code is loaded to the RAM and executed where it will have a segment of RAM assigned to the code, this segment will be copied to a new segment somewhere else located in the RAM and be assigned for the child process.  Is my understanding correct?
66749
I've read and understood about how you create a daemon process, but from everything I read I never really understood why it needs to be done.  I've read that we do the fork - setsid - fork to avoid the process to gain control of a terminal, but what does this mean ? If I start a program in the background using &amp; (for example './script &amp;' ), what makes this process' execution different than if I ran normally a program that turns itself into a daemon ?  Does this simply mean that if I logout the background process will stop and the daemon will keep running ? I'm really having trouble understanding the 'gain control of a terminal' thing.  The reason this bothers me is because I'm working on an embedded RPi on a robot and I thus need to make programs start on boot. Currently I'm just starting them from rc.local with a command like this su user -c 'python /home/user/launcher.py &amp;' &amp;. I've never had any problem with the program starting on boot (I can even see the process using ps -e when SSHing to the RPi), but I would like to know if there's any risk / if it's bad practice.