Hello,
I manage to find a solution: define a new procedure 'system**' which is implemented in C using fork-exec-waitpid. The code is in the attchment. To compile the C extension, run: $ gcc -o libguile-system_star_star.so -shared -fPIC \ `pkg-config --cflags guile-2.2` -O2 -Wall -Wextra system_star_star.c To test the C extension, try running something like: (use-modules (ice-9 threads)) (load-extension "./libguile-system_star_star" "init_system_star_star") (define n 10000) (define s (make-string n #\s)) (define l (make-list n s)) (define main (lambda _ (parallel (system** "echo" "foo" "bar") (apply system** l) (system** "echo" "foo" "bar") (apply system** l)))) C-c should terminate the program. My first solution was to use 'primitive-fork'. But it didn't work, because guile warned me that 'primitive-fork' shouldn't be called in a multithreaded program. I want to find out why. So I read the man-page and it says after forking in a multithread program, it is only safe to call async-safe functions. So I re-implement the whole thing in C and exec right after forking. This is how I come up with the current solution.
/* Copyright (C) 2017 Alex Vong <alexvong1...@gmail.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <libguile.h> static SCM system_star_star(SCM prog, SCM args) { SCM prog_args = scm_cons(prog, args); size_t len = scm_to_size_t(scm_length(prog_args)); char **argv = scm_gc_malloc((len + 1) * sizeof(char *), "argv"); for (size_t k = 0; k < len; ++k) { SCM arg = SCM_CAR(prog_args); size_t len = scm_c_string_length(arg); argv[k] = scm_gc_malloc_pointerless(len + 1, "argv[k]"); scm_to_locale_stringbuf(arg, argv[k], len); argv[k][len] = '\0'; prog_args = SCM_CDR(prog_args); } argv[len] = NULL; int status = 0; pid_t pid = fork(); if (pid == -1) scm_syserror("system**"); if (pid == 0) /* child */ { execvp(argv[0], argv); _exit(127); } else /* parent */ { if (waitpid(pid, &status, 0) == -1) scm_syserror("system**"); } return scm_from_int(status); } void init_system_star_star(void) { scm_c_define_gsubr("system**", 1, 0, 1, system_star_star); }
Cheers, Alex Alex Vong <alexvong1...@gmail.com> writes: > Hello, > > When running the external program "yes" in shell, > > $ yes > > We can terminate the process by pressing C-c. > > However, when running the external program "yes" in guile, > > $ guile -c '(system* "yes")' > > We cannot terminate the process by pressing C-c, > but we can suspend it by pressing C-z. > > Why is that? Is there any way I can terminate the process by pressing > C-c? > > Thanks, > Alex
signature.asc
Description: PGP signature