Skip to content

Commit 5f743d6

Browse files
author
Rik
committed
Add new function _Exit().
* NEWS.11.md: Announce addition. * bootstrap.conf: Add "_Exit" to list of gnulib modules to import. * _Exit-wrapper.h (octave__Exit_wrapper), _Exit-wrapper.c: New files for wrapper function which maps to gnulib _Exit. * liboctave/wrappers/module.mk: Add new files to build system. * syscalls.cc (F_Exit): New function which calls octave wrapper for gnulib _Exit. * syscalls.cc (Ffork): Improve documentation and explain use of _Exit(). Add seealso link to _Exit. * octave-popen2.c: Change to use _Exit rather than _exit for consistency. * system.txi: Add DOCSTRING to manual.
1 parent f98d6c6 commit 5f743d6

File tree

8 files changed

+131
-6
lines changed

8 files changed

+131
-6
lines changed

bootstrap.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
# gnulib modules used by this package.
2929
gnulib_modules="
30+
_Exit
3031
areadlink
3132
base64
3233
bison

doc/interpreter/system.txi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ they can be used, look at the definition of the function @code{popen2}.
367367

368368
@DOCSTRING(fork)
369369

370+
@DOCSTRING(_Exit)
371+
370372
@DOCSTRING(exec)
371373

372374
@DOCSTRING(pipe)

etc/NEWS.11.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Summary of important user-visible changes for version 11 (yyyy-mm-dd):
2929
or permutation of the inputs for performance. Now Octave automatically picks
3030
the fastest calculation order irrespective of the input orientation.
3131

32+
- New function `_Exit` has been added which makes it possible to use
33+
`fork`/`_Exit` sequence to perform work in parallel child processes for
34+
potential performance gains.
35+
3236
- The `fzero` function is now more accurate (1-2 eps when TolX is eps).
3337

3438
- The `roots` function now accepts only a numeric argument. Convert any
@@ -111,6 +115,7 @@ Summary of important user-visible changes for version 11 (yyyy-mm-dd):
111115

112116
### Alphabetical list of new functions added in Octave 11
113117

118+
* `_Exit`
114119
* `corrcov`
115120

116121
### Deprecated functions, properties, and operators

libinterp/corefcn/syscalls.cc

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
#endif
3434

3535
#include <cstdio>
36+
#include <cstdlib>
3637
#include <cstring>
3738

39+
#include "_Exit-wrapper.h"
3840
#include "cmd-hist.h"
3941
#include "fcntl-wrappers.h"
4042
#include "file-ops.h"
@@ -496,17 +498,19 @@ Fork can return one of the following values:
496498
@table @asis
497499
@item > 0
498500
You are in the parent process. The value returned from @code{fork} is the
499-
process id of the child process. You should probably arrange to wait for
500-
any child processes to exit.
501+
process id of the child process. You should probably arrange to wait for any
502+
child processes to exit by using @code{waitpid}.
501503
502504
@item 0
503505
You are in the child process. You can call @code{exec} to start another
504-
process. If that fails, you should probably call @code{exit}.
506+
process. If that fails, you should probably call @code{_Exit} to terminate the
507+
child.
505508
506509
@item < 0
507-
The call to @code{fork} failed for some reason. You must take evasive
508-
action. A system dependent error message will be waiting in @var{msg}.
510+
The call to @code{fork} failed for some reason. You must take evasive action.
511+
A system-dependent error message will be waiting in @var{msg}.
509512
@end table
513+
@seealso{exec, _Exit}
510514
@end deftypefn */)
511515
{
512516
if (args.length () != 0)
@@ -522,6 +526,35 @@ action. A system dependent error message will be waiting in @var{msg}.
522526
return ovl (pid, msg);
523527
}
524528

529+
DEFUNX ("_Exit", F_Exit, args, ,
530+
doc: /* -*- texinfo -*-
531+
@deftypefn {} {} _Exit ()
532+
@deftypefnx {} {} _Exit (@var{status})
533+
Exit the currently running process with exit code @var{status}.
534+
535+
If called with no arguments, exit with status @code{0} indicating success.
536+
537+
The optional integer argument @var{status} specifies the exit code.
538+
539+
Programming Note: This function maps to the C++ function @code{quick_exit}.
540+
The calling process is stopped and any open file descriptors are closed. This
541+
is the correct function to call to end a child process started from Octave.
542+
The ordinary C library function `exit` will not work.
543+
@seealso{fork}
544+
@end deftypefn */)
545+
{
546+
int nargin = args.length ();
547+
if (nargin > 1)
548+
print_usage ();
549+
550+
int status = EXIT_SUCCESS;
551+
552+
if (nargin == 1)
553+
status = args(0).xint_value ("_Exit: STATUS must be an integer");
554+
555+
octave__Exit_wrapper (status);
556+
}
557+
525558
DEFUNX ("getpgrp", Fgetpgrp, args, ,
526559
doc: /* -*- texinfo -*-
527560
@deftypefn {} {pgid =} getpgrp ()

liboctave/wrappers/_Exit-wrapper.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (C) 2025 The Octave Project Developers
4+
//
5+
// See the file COPYRIGHT.md in the top-level directory of this
6+
// distribution or <https://octave.org/copyright/>.
7+
//
8+
// This file is part of Octave.
9+
//
10+
// Octave is free software: you can redistribute it and/or modify it
11+
// under the terms of the GNU General Public License as published by
12+
// the Free Software Foundation, either version 3 of the License, or
13+
// (at your option) any later version.
14+
//
15+
// Octave is distributed in the hope that it will be useful, but
16+
// WITHOUT ANY WARRANTY; without even the implied warranty of
17+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
// GNU General Public License for more details.
19+
//
20+
// You should have received a copy of the GNU General Public License
21+
// along with Octave; see the file COPYING. If not, see
22+
// <https://www.gnu.org/licenses/>.
23+
//
24+
////////////////////////////////////////////////////////////////////////
25+
26+
// _Exit is provided by gnulib. We don't include gnulib headers
27+
// directly in Octave's C++ source files to avoid problems that may be
28+
// caused by the way that gnulib overrides standard library functions.
29+
30+
#if defined (HAVE_CONFIG_H)
31+
# include "config.h"
32+
#endif
33+
34+
#include "stdlib.h"
35+
36+
#include "_Exit-wrapper.h"
37+
38+
void
39+
octave__Exit_wrapper (int status)
40+
{
41+
_Exit (status);
42+
}

liboctave/wrappers/_Exit-wrapper.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright (C) 2025 The Octave Project Developers
4+
//
5+
// See the file COPYRIGHT.md in the top-level directory of this
6+
// distribution or <https://octave.org/copyright/>.
7+
//
8+
// This file is part of Octave.
9+
//
10+
// Octave is free software: you can redistribute it and/or modify it
11+
// under the terms of the GNU General Public License as published by
12+
// the Free Software Foundation, either version 3 of the License, or
13+
// (at your option) any later version.
14+
//
15+
// Octave is distributed in the hope that it will be useful, but
16+
// WITHOUT ANY WARRANTY; without even the implied warranty of
17+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
// GNU General Public License for more details.
19+
//
20+
// You should have received a copy of the GNU General Public License
21+
// along with Octave; see the file COPYING. If not, see
22+
// <https://www.gnu.org/licenses/>.
23+
//
24+
////////////////////////////////////////////////////////////////////////
25+
26+
#if ! defined (octave__Exit_wrapper_h)
27+
#define octave__Exit_wrapper_h 1
28+
29+
#if defined (__cplusplus)
30+
extern "C" {
31+
#endif
32+
33+
extern OCTAVE_API void
34+
octave__Exit_wrapper (int status);
35+
36+
#if defined (__cplusplus)
37+
}
38+
#endif
39+
40+
#endif

liboctave/wrappers/module.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
NOINSTALL_WRAPPERS_INC = \
2+
%reldir%/_Exit-wrapper.h \
23
%reldir%/areadlink-wrapper.h \
34
%reldir%/async-system-wrapper.h \
45
%reldir%/base64-wrappers.h \
@@ -44,6 +45,7 @@ NOINSTALL_WRAPPERS_INC = \
4445
%reldir%/wait-wrappers.h
4546

4647
WRAPPERS_SRC = \
48+
%reldir%/_Exit-wrapper.c \
4749
%reldir%/areadlink-wrapper.c \
4850
%reldir%/async-system-wrapper.c \
4951
%reldir%/base64-wrappers.c \

liboctave/wrappers/octave-popen2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ octave_popen2 (const char *cmd, char *const *args, bool sync_mode,
232232
else
233233
perror ("error: popen2 (child)");
234234

235-
_exit (OCTAVE_CHILD_FAILURE);
235+
_Exit (OCTAVE_CHILD_FAILURE);
236236
}
237237
else if (pid > 0)
238238
{

0 commit comments

Comments
 (0)