public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: Get the cygwin PID of a Win PID
@ 2008-06-27  7:58 Borislav Ivanov
  2008-06-27 11:30 ` Corinna Vinschen
  0 siblings, 1 reply; 10+ messages in thread
From: Borislav Ivanov @ 2008-06-27  7:58 UTC (permalink / raw)
  To: cygwin

Thank you Reini.

However when I use this function it returns -1. Here's my program,
sample output and strace snip:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

pid_t pid;
pid_t cygwin_pid;
int res;

extern pid_t cygwin_winpid_to_pid(int);

int
main(
	int argc,
	char **argv)
{

	if (argc == 2) {
        pid = atol(argv[1]);
	}
	else {
		fprintf(stderr, "Missing process id\n");
		exit(1);
	}

  	printf("\nChecking for cygwin pid of pid %d", pid);
   	cygwin_pid = cygwin_winpid_to_pid(pid);
   	printf("\nCygwin pid found for pid %d is %d", pid, cygwin_pid);
   	if (cygwin_pid != -1) {
       	printf("\nCheck for running using cygwin pid");
       	res=kill(cygwin_pid, 0);
       	printf("\nkill(%d, 0) = %d", cygwin_pid, res);        		
   	}
   	else {
   		printf("\nFailed to get cygwin pid.");
   		exit(-1);
   	}

   	if (res == -1) {
       	printf("\nExitting 0");
        exit(0);
    } else {
      	printf("\nExitting 1");
        exit(1);
    }
}

This outputs:

D:\>testkill 1980

Checking for cygwin pid of pid 1980
Cygwin pid found for pid 1980 is -1
Failed to get cygwin pid.

ps output is:

D:\>ps -W | grep mysql
     2744       1    1384       1980    ?   18 15:07:22
/drives/c/mysql/bin/mysqld-nt
     1980       0       0       1980    ?    0 15:07:23
c:\mysql\bin\mysqld-nt.exe

strace snip (I removed printf statements and recompiled):

 1662  221904 [main] testkill 3952 fhandler_console::open: opened
conin$ 0x3, conout$ 0x17
 1185  223089 [main] testkill 3952 fhandler_console::output_tcsetattr:
0 = tcsetattr (,6002001C) (ENABLE FLAGS 3) (lflag 107 oflag 9)
 1263  224352 [main] testkill 3952 build_fh_pc: fh 0x61168BD8
  607  224959 [main] testkill 3952 fhandler_base::set_flags: flags
0x10002, supplied_bin 0x0
 1133  226092 [main] testkill 3952 fhandler_base::set_flags:
O_TEXT/O_BINARY set in flags 0x10000
 1140  227232 [main] testkill 3952 fhandler_base::set_flags: filemode
set to binary
 1230  228462 [main] testkill 3952 init_cygheap::manage_console_count:
fhandler_console::open: console_count 3, amount 1, ctty /dev/console,
avoid_freeing_console 0
 1695  230157 [main] testkill 3952 fhandler_console::open: opened
conin$ 0x7, conout$ 0x1B
 1177  231334 [main] testkill 3952 fhandler_console::output_tcsetattr:
0 = tcsetattr (,6002001C) (ENABLE FLAGS 3) (lflag 107 oflag 9)
 1236  232570 [main] testkill 3952 fhandler_console::need_invisible:
invisible_console 0
 1124  233694 [main] testkill 3952 dll_crt0_1: user_data->main 0x401050
  611  234305 [main] testkill 3952 __set_errno: void dll_crt0_1(void*):946 val 0

 1276  235581 [main] testkill 3952 __set_errno: pid_t
cygwin_winpid_to_pid(int):1086 val 3
 1373  236954 [main] testkill 3952 close: close (0)
  648  237602 [main] testkill 3952 init_cygheap::manage_console_count:
fhandler_console::close: console_count 2, amount -1, ctty
/dev/console, avoid_freeing_console 0
 1655  239257 [main] testkill 3952 close: 0 = close (0)
  919  240176 [main] testkill 3952 close: close (1)
  674  240850 [main] testkill 3952 init_cygheap::manage_console_count:
fhandler_console::close: console_count 1, amount -1, ctty
/dev/console, avoid_freeing_console 0

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: Get the cygwin PID of a Win PID
@ 2008-06-28  1:04 Borislav Ivanov
  0 siblings, 0 replies; 10+ messages in thread
From: Borislav Ivanov @ 2008-06-28  1:04 UTC (permalink / raw)
  To: cygwin

>>> bash-3.2$ ps -W | grep mysql
>>>       992    4572     992       5900  con  500 14:35:13
>>> /drives/d/mysql/bin/mysqld-nt
>>>      5900       0       0       5900    ?    0 14:35:14
>>> d:\mysql\bin\mysqld-nt.exe
>
>ps -W does not have a perfect algorithm for knowing when to mask the
>windows process but it seems like you should be able to just ignore the
>line which has a '?' in it.

Well I just noticed that the other line in ps (with different win pid
and cygwin pid) displays the actual cygwin pid (992) that when passed
to my program gave the expected result from 1.3. This means that
kill(992, 0) returns -1 for not running process and != -1 for running
process. Since mysql saves the win pid (5900) in its pid file and ps
somehow finds out the corresponding cygwin pid (992) (the line with no
'?') I thought that there's some method to retrieve it and keep my
program's past behaviour with minimal change.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: Get the cygwin PID of a Win PID
@ 2008-06-27 11:41 Borislav Ivanov
  2008-06-27 11:49 ` Corinna Vinschen
  0 siblings, 1 reply; 10+ messages in thread
From: Borislav Ivanov @ 2008-06-27 11:41 UTC (permalink / raw)
  To: cygwin

Thank you again,

I looked further and found out that this happens when I start the
mysql-nt application from bash.

Here's the result when I start it from cmd - only one process listed
with equal cywgin and win PIDs:

D:>mysql\bin\mysqld-nt.exe --defaults-file=conf\mysql.conf

bash-3.2$ ps -W | grep mysql
     6044       0       0       6044    ?    0 13:24:25
D:\mysql\bin\mysqld-nt.exe
	
And then the bash version:
	
bash-3.2$ mysql/bin/mysqld-nt.exe --defaults-file=conf/mysql.conf

bash-3.2$ ps -W | grep mysql
      992    4572     992       5900  con  500 14:35:13
/drives/d/mysql/bin/mysqld-nt
     5900       0       0       5900    ?    0 14:35:14
d:\mysql\bin\mysqld-nt.exe

The funny thing is that with cygwin 1.3 both commands invoked only one
process. This is mysql 5.0.38. My question is: Is there a way to make
this work like in 1.3 (producing only one process with the same pid I
can use later), or I should look for a workaround?

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 10+ messages in thread
* Get the cygwin PID of a Win PID
@ 2008-06-26 15:32 Borislav Ivanov
  2008-06-27  0:49 ` Reini Urban
  0 siblings, 1 reply; 10+ messages in thread
From: Borislav Ivanov @ 2008-06-26 15:32 UTC (permalink / raw)
  To: cygwin

Hello,

I'm currently updating cygwin from 1.3.22 to 1.5.25 and I encountered
a problem with Winpids/Cygwinpids. I have a custom tool that checks if
a process is running by invoking kill(PID, 0). The PID is retrieved
from a PID file which all applications write on startup. On 1.3 I get
this:

d:\>ps -W | grep mysql
     1860       0       0       1860    ?    0 15:54:44
c:\mysql\bin\mysqld-nt.exe

and on 1.5 I get:

D:\>ps -W | grep mysql
     2744       1    1384       1980    ?   18 15:07:22
/drives/c/mysql/bin/mysqld-nt
     1980       0       0       1980    ?    0 15:07:23
c:\mysql\bin\mysqld-nt.exe

On both 1.3 and 1.5 my pid file contains the Winpid. The difference
here is that on 1.3 kill(Winpid, 0) succeeds but on 1.5 fails with
errno=ESRCH, while kill(Cygwinpid, 0) behaves as expected.

I searched through the list, but found only transition from cygwinpid
to winpid (by using winpid = cygwin_internal(CW_CYGWIN_PID_TO_WINPID,
pid)). Is there some analogue to retrieve the corresponding cygwinpid
of a winpid?

Thank you,
Borislav Ivanov

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-06-27 15:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-27  7:58 Get the cygwin PID of a Win PID Borislav Ivanov
2008-06-27 11:30 ` Corinna Vinschen
2008-06-27 11:49   ` Markus Hoenicka
2008-06-27 12:18     ` Corinna Vinschen
  -- strict thread matches above, loose matches on Subject: below --
2008-06-28  1:04 Borislav Ivanov
2008-06-27 11:41 Borislav Ivanov
2008-06-27 11:49 ` Corinna Vinschen
2008-06-27 15:49   ` Christopher Faylor
2008-06-26 15:32 Borislav Ivanov
2008-06-27  0:49 ` Reini Urban

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).