unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Alex Vong <alexvong1995@gmail.com>
To: guile-user@gnu.org
Subject: Re: C-c in guile
Date: Tue, 16 May 2017 01:46:09 +0800	[thread overview]
Message-ID: <87fug66mji.fsf@gmail.com> (raw)
In-Reply-To: <877f5ovd85.fsf@gmail.com> (Alex Vong's message of "Sat, 21 Jan 2017 19:55:54 +0800")


[-- Attachment #1.1: Type: text/plain, Size: 1218 bytes --]

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.



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: system_star_star.c --]
[-- Type: text/x-csrc, Size: 1871 bytes --]

/* Copyright (C) 2017 Alex Vong <alexvong1995@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);
}

[-- Attachment #1.3: Type: text/plain, Size: 478 bytes --]



Cheers,
Alex


Alex Vong <alexvong1995@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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

      parent reply	other threads:[~2017-05-15 17:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-21 11:55 C-c in guile Alex Vong
2017-01-21 14:53 ` Matt Wette
2017-01-22  6:54   ` Alex Vong
2017-01-23 16:05     ` Vladimir Zhbanov
2017-02-27 19:49 ` Andy Wingo
2017-03-01 16:03   ` Alex Vong
2017-05-15 17:46 ` Alex Vong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fug66mji.fsf@gmail.com \
    --to=alexvong1995@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).