I tried running the suggested command, which returns the PID of the
bash shell from where the command was executed.

The thing is I need to get the PID value from the ps command instead
of WINPID in a C/C++ application.

Tried looking for some similar discussion in the mailing lists of
Cygwin and MSYS2 and respective bug reports.
But could not locate anyone having such a requirement.
Otherwise, I can probably get it from the system() call using ps
command and grep/awk processing it (not ideal solution).


On Thu, Nov 12, 2020 at 5:51 PM Cyrille Lefevre via Msys2-users
<msys2-users@lists.sourceforge.net> wrote:
>
> > On Thu, Nov 12, 2020 at 1:41 PM Cyrille Lefevre via Msys2-users
> > <msys2-users@lists.sourceforge.net> wrote:
> >>
> >> Hi,
> >>
> >> try using /proc/self/stat, see details from
> >> https://man7.org/linux/man-pages/man5/proc.5.html
> >> windows pid is got from /proc/self/winpid
>
> Le 12/11/2020 à 09:36, Ameya Vikram Singh a écrit :
>  > Hi,
>  >
>  > I actually tried the following code:
>  > ////////////////////////////////////////////////////////////////
>  > #include <stdio.h>
>  > #include <unistd.h>
>  >
>  > int main()
>  > {
>  >          pid_t pid = getpid();
>  >
>  >          FILE *fi = fopen("/proc/self/stat", "r");
>  >          if(fi)
>  >          {
>  >                  printf("Able to open file\n");
>  >          }
>  >          else
>  >          {
>  >                  printf("Unable to open file\n");
>  >          }
>  >
>  >          //system("ps aux");
>  >          printf("PID: %llu\n", pid);
>  >
>  >          return 0;
>  > }
>  > ////////////////////////////////////////////////////////////////
>  >
>  > I get the following print on the bash console for MinGW 64-bit:
>  > Unable to open file
>  > PID: 15072
>  >
>  > It seems in the MinGW context the UNIX layer emulation is disabled.
>  > Just wanted to know if there is an API something similar to:
>  > "cygwin_winpid_to_pid" to fetch the PID reflected by the ps command
> for MinGW?
>  >
>  > If there is one how to link to it?
>
> could you try this at the bash prompt level :
>
> $ read pid rest < /proc/self/stat; echo $pid : $rest
> 680 : (bash) R 679 680 680 8912896 -1 0 22761 22761 0 0 101078 106296
> 101078 106296 20 0 0 0 2352723 9093120 1721 345
>
> Regards,
> Cyrille Lefevre
>
>
>
>
> _______________________________________________
> Msys2-users mailing list
> Msys2-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/msys2-users



-- 
Regards,
Ameya Vikram Singh


_______________________________________________
Msys2-users mailing list
Msys2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/msys2-users

Reply via email to