Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Diaganostics.Process.Id Isn't the Same Process Id Shown in Task Manager. Why?

I'm using C#'s System.Diagnostic.Process object.
One of its properties is Id.
The Id this produces is not the same as the PID, shown in Windows Task Manager.
Why is this?

You see, once this process is started.
It launches two other unmanaged processes, for which I can't explicitly get IDs for by object property references.
I have to search through all processes to find them by process name via System.Diagnostics.Process.GetProcesses().

I'm trying to find a reliable way to kill this process and all associated processes by PID, the one that shows in Task Manager.
Is there a better way?

I can't just kill all processes with the associated process names, because that might kill other instances of those processes that have nothing to do with my program.

like image 756
Lonnie Best Avatar asked Feb 23 '10 08:02

Lonnie Best


People also ask

What is PID in Task Manager?

Each process running in Windows is assigned a unique decimal number called the process ID (PID). This number is used in a number of ways, for example to specify the process when attaching a debugger to it.

How do you find the PID of a process?

How to get PID using Task Manager. Press Ctrl+Shift+Esc on the keyboard. Go to the Processes tab. Right-click the header of the table and select PID in the context menu.

Which system is used to retrieve the process ID of the process?

The current process ID is provided by a getpid() system call, or as a variable $$ in shell. The process ID of a parent process is obtainable by a getppid() system call. On Linux, the maximum process ID is given by the pseudo-file /proc/sys/kernel/pid_max .


2 Answers

The key is that you don't want to kill your process by Id. In fact, that's a race condition: your spawned process could die and another process could get created with the same Id. Then when you go to kill it, you would end up killing the new process instead of the old one that was already dead.

The most reliable way to kill spawned processes is to put them in a Job object and terminate the Job once your task is completed.

Here's some simple code to implement a Job class:

class Job
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string lpName);

    [DllImport("kernel32.dll")]
    public static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess);

    [DllImport("kernel32.dll")]
    public static extern bool TerminateJobObject(IntPtr hJob, uint uExitCode);

    IntPtr job;

    public Process StartProc(string commandLine)
    {
        if (job == IntPtr.Zero)
            job = CreateJobObject(IntPtr.Zero, null);
        ProcessStartInfo si = new ProcessStartInfo(@"c:\windows\system32\cmd.exe");
        si.Arguments = "/c " + commandLine;
        si.CreateNoWindow = false;
        si.UseShellExecute = false;
        Process proc = Process.Start(si);
        AssignProcessToJobObject(job, proc.Handle);
        return proc;
    }

    public void TerminateProc()
    {
        // terminate the Job object, which kills all processes within it
        if (job != null)
            TerminateJobObject(job, 0);
        job = IntPtr.Zero;
    }
}
like image 173
Gabe Avatar answered Oct 10 '22 12:10

Gabe


I can't reproduce this. I've just run the following code:

foreach (var proc in Process.GetProcesses()
                            .OrderBy(proc => proc.Id))
{
    Console.WriteLine("{0}: {1}", p.Id, p.ProcessName);
}

All the processes listed have matches the PID in Task Manager. This is using Windows 7 - what OS are you using? If you look at other processes, do they match the PID shown in Task Manager?

like image 44
Jon Skeet Avatar answered Oct 10 '22 13:10

Jon Skeet