NO

Author Topic: Problem with spawn a process  (Read 5089 times)

Jens

  • Guest
Problem with spawn a process
« on: March 28, 2017, 03:00:57 PM »
Compile and link this example with 32bit, all is ok.
But with 64bit i must close the open logfile before call _spawn or execution crashes.


Code: [Select]
// ***********************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <process.h>

// ***************************** Code Start *********************************

int main(int argc, char **argv)
{
FILE *log=NULL;
long long notep_pid=0;
int rc=0;
char *user_file="C:\\Temp\\T\\T.txt\0", *log_file="C:\\Temp\\T\\T.log\0";

log=fopen(log_file, "w+");
if(log) fprintf(log, "\n    Log File is open");

//fclose(log);

notep_pid=_cwait (&rc, _spawnl(_P_NOWAIT, "C:\\Windows\\notepad.exe\0", "notepad.exe\0", user_file, 0), 0);

//log=fopen(log_file, "a");

fprintf(stdout, "\n    STATUS %d, SPID %lld", rc, notep_pid);
fprintf(stdout, "\n");
fclose(log);

return rc;
}

// **************************** Code End *****************************
« Last Edit: March 30, 2017, 10:35:39 PM by Jens »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2107
Re: Problem with spawn a process
« Reply #1 on: March 28, 2017, 04:52:57 PM »
optimization problem with const?
« Last Edit: March 28, 2017, 05:06:25 PM by TimoVJL »
May the source be with you

Jens

  • Guest
Re: Problem with spawn a process
« Reply #2 on: March 28, 2017, 10:00:06 PM »
ok,

that's the reason.
I compile without the -OT option and everything is fine.

THANKS Jens :) :)
« Last Edit: March 28, 2017, 10:02:29 PM by Jens »

Jens

  • Guest
Re: Problem with spawn a process
« Reply #3 on: March 30, 2017, 09:25:25 PM »
Hi again,

sorry, but my problem isn't solved.

First Example (see above) was running well.
But because it wasn't that what I want, I do some other tests
and the result was always the same. Program chrashed at end of execution.

If you test the code an uncomment "#define TEST" the prog crashed

example code:

Code: [Select]
// *************************************************************************

//#define TEST

// *************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <process.h>

// *************************************************************************

FILE *log=NULL;

// ***************************** Code Start *********************************

int TestSpawn(const char *ufile)
{
long long notepad_id=0;
int rc=0;

notepad_id=_cwait (&rc, _spawnl(_P_NOWAIT, "C:\\Windows\\notepad.exe\0", "notepad.exe\0", ufile, 0), 0);
fprintf(log, "\n    STATUS %d, NPAD_ID %lld\n", rc, notepad_id);

return rc;
}

// *************************************************************************

int main(int argc, char **argv)
{
FILE *test=NULL;
int rc=0;
const char *log_file="D:\\Temp\\T\\T.log\0", *user_file="D:\\Temp\\T\\T1.txt\0", *test_file="D:\\Temp\\T\\T2.txt\0";
log=fopen(log_file, "w+");
if(!log)
{
perror("\n  No Log File ");
exit(EXIT_FAILURE);
}
fprintf(log, "\n    Log File is open\n");

rc=TestSpawn(user_file);

#ifdef TEST

test=fopen(test_file, "w");
if(!test)
{
perror("\n  No Test File ");
exit(EXIT_FAILURE);
}
if(test) fprintf(test, "\n    Test File is open");
fprintf(test, "\n    STATUS %d\n", rc);
fprintf(test, "\n    Test File closed\n\n    END\n");
fflush(test);
fclose(test);

#endif

fprintf(log, "\n    STATUS %d\n", rc);
fprintf(log, "\n    Log File closed\n\n    END\n");
fflush(log);
fclose(log);
fprintf(stdout, "\n    END STATUS %d\n", rc);
fprintf(stdout, "    END\n");

return EXIT_SUCCESS;
}

// **************************** Code End *****************************

compiler flags: -std:C11 -Tx64-coff -Zi -Ob0 -fp:precise -W1 -Gr -J

linker flags: -subsystem:console -machine:x64 -LIBPATH:C:\PellesC\Lib\Win64 kernel32.lib user32.lib advapi32.lib delayimp64.lib

error message:
CRT: unhandled exception (main) -- terminating
*** Process returned 255 ***
Press any key to continue...

debug: Access violation --->>> fclose(file)

Tested with 3 PCs
Win7- intel i3, Win8.1- intel i5, Win10-intel i7

If this is not the right sub-forum, feel free to move it.
regards and thanks

Jens
« Last Edit: March 31, 2017, 01:13:32 PM by Jens »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Problem with spawn a process
« Reply #4 on: March 31, 2017, 03:36:27 PM »
The fault is generated in the fclose function due to malloced memory header corruption.
Maybe there is a bug in the spawn functions in 64bits compilation.
I'll check better as soon I have enough time.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2107
Re: Problem with spawn a process
« Reply #5 on: March 31, 2017, 03:40:44 PM »
This one doesn't crash:
Code: [Select]
int TestSpawn1(const char *ufile)
{
long long notepad_id=0;
int rc=0;
const char *sargv[] = {"notepad.exe",ufile,0};

notepad_id=_cwait (&rc, _spawnv(_P_NOWAIT, "C:\\Windows\\notepad.exe", (char* const*)sargv), 0);
if (log) fprintf(log, "\n    STATUS %d, NPAD_ID %lld\n", rc, notepad_id);

return rc;
}
_spawnl calls _spawnve and that crash too.
« Last Edit: March 31, 2017, 10:23:54 PM by TimoVJL »
May the source be with you

Jens

  • Guest
Re: Problem with spawn a process
« Reply #6 on: March 31, 2017, 08:56:13 PM »
Thanks frankie and Timo,

@Timo
i import your TestSpawn1 function in my example, but no success.
Crashes at the first call to fclose() function.

I think frankie is on the right way

My workaround is to close all open files before calling spawn function
and open then after returning in append mode

nice and sunny  8) weekend
greetings from Kiel
Jens  ;)
« Last Edit: March 31, 2017, 10:11:47 PM by Jens »