From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alex Vong Newsgroups: gmane.lisp.guile.user Subject: Re: C-c in guile Date: Tue, 16 May 2017 01:46:09 +0800 Message-ID: <87fug66mji.fsf@gmail.com> References: <877f5ovd85.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Trace: blaine.gmane.org 1494870470 22184 195.159.176.226 (15 May 2017 17:47:50 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 15 May 2017 17:47:50 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon May 15 19:47:46 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dAK5x-0005c1-K8 for guile-user@m.gmane.org; Mon, 15 May 2017 19:47:45 +0200 Original-Received: from localhost ([::1]:37991 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAK61-0003CS-CW for guile-user@m.gmane.org; Mon, 15 May 2017 13:47:49 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50237) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAK5U-00039B-2H for guile-user@gnu.org; Mon, 15 May 2017 13:47:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAK5Q-0003OS-3p for guile-user@gnu.org; Mon, 15 May 2017 13:47:16 -0400 Original-Received: from mail-pf0-x230.google.com ([2607:f8b0:400e:c00::230]:33464) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dAK5P-0003OK-Qr for guile-user@gnu.org; Mon, 15 May 2017 13:47:12 -0400 Original-Received: by mail-pf0-x230.google.com with SMTP id e193so66629829pfh.0 for ; Mon, 15 May 2017 10:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:references:date:in-reply-to:message-id:user-agent :mime-version; bh=GgdaT+w3VWiKiadXUm1Hyr6QOcXWlvYyImm7UUYhcX8=; b=p/I/YIA2MHgxQFtG4uR2vDrJShzlb4l+nF8JHqmWVi/rRs3rbO8gNY/JGrB6HOitzo blnfzNuPF4axHlxxXX5kphSx3k+gEvwMciagVMAT+MrCv8s5SgpwSg4W/03howe1r+GJ /GaPfdvusEdP3uMJuelWmvcwkEGFZD4mhgtHt/fdwcsPMHILVQVUnZ0HdgRpGAfLc0dg hTYQ+VZMyQmdTQywWR0s0jHlL8ynS2BiQfksJMpT6j3z/YnWf6NH0Rd3y0Pm0T2S/Yii PZIHbEUgEI3klbXzWF/Awx1kJ2R+Z2dphxZj5prUK7TZyOwdl/ZPnzY48/3rLRTXJtHl EH/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=GgdaT+w3VWiKiadXUm1Hyr6QOcXWlvYyImm7UUYhcX8=; b=X3yA8C8RP8MneLClYvznVJn74snGJMV/frbttMuLOwwttzevFedXiCDuQDphHIA0oP 4vf1IIkOub1U0IwvDegpMYW8wZg5jxKDeCZOHY8RJR0cnFBv5doEAEetkdBV/j/wcfJQ +Z58GOXLfx+tWBW2OLnBZS5bk3svy0LhvaKixnekigDYlGUzdYFjW+E1Z2rSzzQzIld8 fpYZVuHbQ2vKVDyjm2FkXPTPKepRldE8fv1MWvIFUZK+2Mm4/APNgIeEQ0Wxqd2ED9RW L+o0UXcZJucRhn9jgy5DATcCzcp+PYrnQiI5P/qRriR1nV4CM+xzE5JLFGACKVVCu3Bp aw6w== X-Gm-Message-State: AODbwcAPL18LSmCbX0qABUPNnmCPnUlAb3RtOGeLCfyAxuqUsnt8c6b8 82ai3bZpRPq5Sw== X-Received: by 10.98.13.17 with SMTP id v17mr7451641pfi.89.1494870430284; Mon, 15 May 2017 10:47:10 -0700 (PDT) Original-Received: from debian (1-64-82-148.static.netvigator.com. [1.64.82.148]) by smtp.gmail.com with ESMTPSA id k79sm24317250pfj.6.2017.05.15.10.47.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 May 2017 10:47:09 -0700 (PDT) In-Reply-To: <877f5ovd85.fsf@gmail.com> (Alex Vong's message of "Sat, 21 Jan 2017 19:55:54 +0800") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::230 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:13716 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-csrc Content-Disposition: inline; filename=system_star_star.c /* Copyright (C) 2017 Alex Vong * * 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 #include #include #include #include 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); } --=-=-= Content-Type: text/plain Cheers, Alex Alex Vong 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 --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEdZDkzSn0Cycogr9IxYq4eRf1Ea4FAlkZ6WIACgkQxYq4eRf1 Ea6Bog/7BFzCZrrap37DvGfMvt/mFYuLScKT6fQAbGpD8iN/R+8XlPR/y33nuadf HwkuyRs6JwKfYJfMjeQn2R+rngash3gToMd413Sil1vZ9FoFEWnihXKWY3+UPm/L FJTJc0q657QZ1yUFjo8Qtxbz0JTtYHn2+NpGXN5yh/qvrUtBHfGs+c24D1T8hxN+ XkIJzB9LCX/LuLs1Mj+mzpuGHGVU+BcTGneOykshQk3JWxUKkbSCpWK68fbpQ/h4 TKh/0swpSO03VFk7GKzUbt16NQGSEDbpowtPwPGLo7TAILLAxg+Dl6Znif+3o1xq 1M1URiosl/PM4kMJDYC5FQD6c0DwAZaoyCDSdlbsQyNvRi5bgRBq3Nf3O1TOjrTw wvffz4kLELJIr8M0Re9EFMO3b88SP5bymtsJDZR+OLmPYm0DIrY1ZdRFGrgQQ+je QKUAdOnW7IeacjvNmbyUzLY3T3X2LOXhmVsHvqRYeuWvEXHmXhtb5D5jdiaiAZUb xowMfvMXvyHDbDYkhthYCUaYQixw/H+l3g2iY0vYiTB+q2DttGwbwzZS83yIvzsb J9WoqwofCOLGgqeLVlSCNdcifDclopcvW3hzrMG41CPxp+WrS3qfulXO/MXe4pgL 0zVKQ66/BE77sekOVdsaZHpQNNP8CHvA/ppCsr8sIEAN+fjrX/U= =Y9mW -----END PGP SIGNATURE----- --==-=-=--