Skip to content

Commit 5a1bd54

Browse files
committed
fix sleep
1 parent 53b03b0 commit 5a1bd54

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

example/system/example_process_2.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ program run_async
55

66
type(process_type) :: p
77

8-
! Run an asynchronous process to sleep for 5 seconds
9-
p = run("sleep 3", wait=.false.)
8+
! Run an asynchronous process to sleep for 1 second
9+
p = run("sleep 1", wait=.false.)
1010

1111
! Check if the process is running
1212
if (is_running(p)) then

example/system/example_process_4.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ program example_process_kill
3030

3131
! Verify the process is no longer running
3232
running = is_running(process)
33-
print *, "Process running after kill:", running
33+
print *, "Process running after kill:", running,' runtime=',elapsed(process)
3434

3535
stop 0
3636

src/stdlib_system_subprocess.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ end function process_has_win32
7777
module subroutine sleep(millisec)
7878
integer, intent(in) :: millisec
7979

80-
call process_wait(real(0.001*millisec,c_float))
80+
call process_wait(real(0.001*real(max(0,millisec),c_float),c_float))
8181

8282
end subroutine sleep
8383

src/stdlib_system_subprocess.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ void process_create_windows(const char* cmd, const char* stdin_stream,
109109
snprintf(full_cmd, full_cmd_len, "%s", cmd);
110110
}
111111

112-
// Free the allocated memory
113-
free(full_cmd);
114-
115112
// Create the process
116113
BOOL success = CreateProcess(
117114
NULL, // Application name
@@ -126,8 +123,11 @@ void process_create_windows(const char* cmd, const char* stdin_stream,
126123
&pi // PROCESS_INFORMATION
127124
);
128125

126+
// Free the allocated memory
127+
free(full_cmd);
128+
129129
if (!success) {
130-
fprintf(stderr, "CreateProcess failed (%lud).\n", GetLastError());
130+
fprintf(stderr, "CreateProcess failed (%lu).\n", GetLastError());
131131
return;
132132
}
133133

@@ -333,15 +333,18 @@ bool process_kill(stdlib_pid pid)
333333
void process_wait(float seconds)
334334
{
335335
#ifdef _WIN32
336-
DWORD dwMilliseconds = 1000*seconds;
336+
DWORD dwMilliseconds = (DWORD) (seconds * 1000);
337337
Sleep(dwMilliseconds);
338338
#else
339339
int ierr;
340340

341-
struct timespec ts_remaining;
342-
ts_remaining.tv_sec = seconds;
343-
ts_remaining.tv_nsec = seconds * 1000000000L;
344-
341+
unsigned int ms = (unsigned int) (seconds * 1000);
342+
struct timespec ts_remaining =
343+
{
344+
ms / 1000,
345+
(ms % 1000) * 1000000L
346+
};
347+
345348
do
346349
{
347350
struct timespec ts_sleep = ts_remaining;
@@ -358,7 +361,7 @@ void process_wait(float seconds)
358361
fprintf(stderr, "nanosleep() bad milliseconds value\n");
359362
exit(EINVAL);
360363
case EFAULT:
361-
fprintf(stderr, "nanosleep() bad milliseconds value\n");
364+
fprintf(stderr, "nanosleep() problem copying information to user space\n");
362365
exit(EFAULT);
363366
case ENOSYS:
364367
fprintf(stderr, "nanosleep() not supported on this system\n");

0 commit comments

Comments
 (0)