getpid and killing single Wine processes

Open forum for end-user questions about Wine. Before asking questions, check out the Wiki as a first step.
Forum Rules
Locked
rwmjones
Newbie
Newbie
Posts: 2
Joined: Fri Aug 28, 2020 4:23 am

getpid and killing single Wine processes

Post by rwmjones »

I'm porting a server (https://github.com/libguestfs/nbdkit) to Windows. One feature of this server is that you can get it to print its own process ID to a pidfile at start up, which you can later use if you need to kill the server. ie On Unix systems:

Code: Select all

nbdkit -P pidfile null
(...later once you've finished with the server ...)

Code: Select all

kill `cat pidfile`
Now I found that getpid() on Wine doesn't return the Unix process ID of nbdkit.exe, instead it returns an internal ID, which for some reason is usually 396. Can I use this number to kill the process under Wine?

I couldn't find a way within Wine to get the Unix process ID, but I found I could read z:\proc\self\stat to get a Unix process ID, and I can kill that. Unfortunately it gets the PID of wineserver, not the program, and obviously killing wineserver doesn't go well.

I think conceptually I just don't understand what would be the best way to translate this idiom into Wine. (FWIW on actual Windows the process ID can be used to select the process, so I think we're OK there).
madewokherd
Level 4
Level 4
Posts: 143
Joined: Mon Jun 02, 2008 5:03 pm

Re: getpid and killing single Wine processes

Post by madewokherd »

Windows process id's don't work the same as Unix process ID's, so Wine needs its own namespace for them. That consistency you've observed is part of it. I think they're also all divisible by 4, and they share a namespace with threads.

You can use "wine taskkill /pid 396", and the taskkill command also exists on Windows and can be used the same way.

I think that if you CreateProcess (or maybe ShellExecute) something like
"z:\bin\sh" "-c" "echo $$ > /full/path/to/pidfile"
it will write your Unix pid to a file. Unfortunately, cmd.exe refuses to do this, so I don't have an easy way to test it.

If you use getpid() within a winelib .dll, it should also give you the Unix pid, but you may not want to rely on this as it makes your dll platform-dependent.
madewokherd
Level 4
Level 4
Posts: 143
Joined: Mon Jun 02, 2008 5:03 pm

Re: getpid and killing single Wine processes

Post by madewokherd »

That sh command won't work, as $$ is the pid of the shell itself, but the approach may be workable.
rwmjones
Newbie
Newbie
Posts: 2
Joined: Fri Aug 28, 2020 4:23 am

Re: getpid and killing single Wine processes

Post by rwmjones »

Thanks, it was taskkill /pid which I was missing.
Locked