This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Get the cygwin PID of a Win PID


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]