Thread: spawnl function under windows

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    7

    Question spawnl function under windows

    trying to get pid of child process from the parent process

    the return of _spawn function should give the created process id,

    but according to below code,
    child pid=13328
    and return of _spawnl function is 216

    explanation is appreciated

    thanks
    Code:
    #include<stdio.h>
    #include<process.h>
    
    
    int main(void)
    {
      printf("Spawning child with spawnl\n");
      long pid;
      pid = _spawnl( P_NOWAIT, "child.exe", "child.exe", "process_1",  "50000", NULL );
      printf("parent= %ld child=%ld ", getpid(), pid);
      return 0;
    }
    output:
    Spawning child with spawnl
    parent= 216 child=196
    Process returned 0 (0x0) execution time : 0.034 s
    Press any key to continue.
    process_1 start with 50000 delay pid=13328

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is this within MinGW or Cygwin?

    What's the source code for child.exe look like?

    _spawnl, _wspawnl | Microsoft Docs
    The return value from an asynchronous _spawnl or _wspawnl (_P_NOWAIT or _P_NOWAITO specified for mode) is the process handle.
    A process handle is not the same as a process ID.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    Quote Originally Posted by Salem View Post
    Is this within MinGW or Cygwin?

    What's the source code for child.exe look like?

    _spawnl, _wspawnl | Microsoft Docs

    A process handle is not the same as a process ID.
    ok .. how to get the pid of child

    the child code
    Code:
    // child process program
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h> // needed for atoi
    
    
    void delay(int milliseconds);
    
    
    int main(int argc, char *argv[] )
    {
    
    
      printf("%s start with %d delay pid=%d\n",argv[1], atoi(argv[2]), _getpid());
      delay(atoi(argv[2]));
      printf("%s end\n",argv[1]);
      return 0;
    }
    
    
    void delay(int milliseconds)
    {
        long pause;
        clock_t now,then;
    
    
        pause = milliseconds*(CLOCKS_PER_SEC/1000);
        now = then = clock();
        while( (now-then) < pause )
            now = clock();
    }
    using codeblocks with MinGW

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    The description of that function says it returns the "process handle" (type HANDLE). This is not the same as the process id (type DWORD).
    Code:
    // In the parent, get the pid from the handle:
    DWORD pid = GetProcessId(handle);
     
    // In the child, print it's handle:
    HANDLE handle = GetCurrentProcess();
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    mingw-w64/getpid.c at master * Alexpux/mingw-w64 * GitHub
    Code:
    int getpid(void)
    {
        return GetCurrentProcessId();
    }
    GetProcessId function | Microsoft Docs
    As I said above, spawn returns a process handle, not a process ID when using NOWAIT
    Use GetProcessId() to turn your handle into an ID.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    what header to include in my program to use GetProcessId function

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by vlopaz View Post
    what header to include in my program to use GetProcessId function
    It's at the end of the link I posted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    Thanks, I saw that but I could not find processthreadsapi.h in my codeblocks+ MinGW installation

  9. #9
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    used #include <windows.h>
    but still GetProcessId not defined

  10. #10
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    what i am trying to do is:


    parent program tocreate ~50 calls to external program, but do not want to make 50concurrent spwanl calls, I want make ~ 5 calls at a time. then,parent program check terminated processes to make extra calls.


    I need to find away, so parent program can polls created spawns, once any iscompleted, parent will make extra spawnl calls.


    my hope that, I willfind a function to check the stats of a process based on it ID. onceI know a process is completed will make more spawn calls.


    I think, toaccomplish this, I need
    1. function gives mecreated process ID
    2. function checksthe status of process based on its ID




    do not know how todo (1) and (2) ?


    help please.
    Thanks

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How old is your MinGW?

    I just cloned GitHub - Alexpux/mingw-w64 and it's right there.
    Code:
    $ grep -C2 GetProcessId mingw-w64/mingw-w64-headers/include/processthreadsapi.h
      WINBASEAPI DWORD WINAPI GetPriorityClass (HANDLE hProcess);
      WINBASEAPI WINBOOL WINAPI ProcessIdToSessionId (DWORD dwProcessId, DWORD *pSessionId);
      WINBASEAPI DWORD WINAPI GetProcessId (HANDLE Process);
      WINBASEAPI DWORD WINAPI GetThreadId (HANDLE Thread);
      WINBASEAPI HANDLE WINAPI CreateRemoteThreadEx (HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList, LPDWORD lpThreadId);
    --
    #define PROC_THREAD_ATTRIBUTE_REPLACE_VALUE 0x00000001
    
      WINBASEAPI DWORD WINAPI GetProcessIdOfThread (HANDLE Thread);
      WINBASEAPI WINBOOL WINAPI InitializeProcThreadAttributeList (LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList, DWORD dwAttributeCount, DWORD dwFlags, PSIZE_T lpSize);
      WINBASEAPI VOID WINAPI DeleteProcThreadAttributeList (LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2019
    Posts
    7
    I have the latest codeblocks codeblocks-17.12mingw-setup.exe from their website and not sure what is the MinGW version

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vlopaz View Post
    I have the latest codeblocks codeblocks-17.12mingw-setup.exe from their website and not sure what is the MinGW version
    It is not a MinGW64 build of GCC; If I recall correctly it is version 5.1.0

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. spawnl()
    By Drew53385 in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 08:00 PM
  2. using spawnl for a dos program
    By ronin in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:34 PM
  3. spawnl !!!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 06:00 PM
  4. about spawnl
    By xacoolboy in forum C Programming
    Replies: 1
    Last Post: 05-13-2002, 09:01 AM

Tags for this Thread