This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sleep ignore sigchld


Here's a sample source and sample run on HP-UX.
I COULD easily be doing something wrong.
I don't have a Linux box right here to run it, but I believe if you run it on RedHat 5.1 (2.0.34, I think ?) you'll get something like "Parent woke up after 3" instead of "Parent woke up after 10".

I appologize for not having a better example, if you'd like I'll put something together when I get home tonight.

----------------------------------------------------------------------------------------
*/

#include <stdio.h>
#include <signal.h>

void usage() {
  fputs("usage: trash childs sleep_parent sleep_child\n", stderr);
  exit(1);
}

void main(int argc, char * argv[]) {
  int idx01;
  int i_rc;
  int i_sleep_p;
  int i_sleep_c;
  int i_child;
  int i_slept;

  if(argc != 4) usage();

  i_child = atoi(argv[1]);
  printf("children: %d\n", i_child);

  i_sleep_p = atoi(argv[2]);
  printf("parent sleep: %d\n", i_sleep_p);

  i_sleep_c = atoi(argv[3]);
  printf("child sleep: %d\n", i_sleep_c);

  signal(SIGCHLD, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);

  for(idx01 = i_child; idx01 > 0; idx01--) {
    i_rc = fork();
    if(i_rc == 0) {
      printf("Child %d sleeping...\n", idx01);
      sleep(i_sleep_c);
      _exit(0);
    }
    if(i_rc < 0) {
      perror("fork");
      exit(1);
    }
    printf("Child pid: %d\n", i_rc);
    // sleep(1);
  }
  while(1) {
    printf("Parent sleeping %d\n", i_sleep_p);
    i_slept = sleep(i_sleep_p);
    printf("Parent woke up after %d\n", i_sleep_p - i_slept);
  }
} /* main() */

/*
-----------------------------------------------------------------------------------------------

Sample run on HP-UX:

children: 3
parent sleep: 10
child sleep: 3
Child pid: 28380
Child 3 sleeping...
Child 2 sleeping...
Child pid: 28381
Child pid: 28382
Parent sleeping 10
Child 1 sleeping...
Parent woke up after 10
Parent sleeping 10
Parent woke up after 10
Parent sleeping 10
Parent woke up after 10
Parent sleeping 10

>>> "H. J. Lu" <hjl@freya.yggdrasil.com> 11/06 1:40 PM >>>
In article <71tf3s$qbo$1@nnrp1.dejanews.com> you wrote:
: RedHat 5.1

: sleep() seems to return when the process receives SIGCHLD
: even though I'm ignoring SIGCHLD ( signal(SIGCHLD, SIG_IGN); ).

: On other Unixes, IGNORED signals don't interrupt sleep.
: Why is Linux different ?

Send me a small testcase. I will fix it.

H.J.
*/
-----
Mon Nov 16 21:16:27 PST8  H.J. Lu  <hjl@gnu.org>

	* sysdeps/unix/sysv/linux/syscalls.list (nanosleep): Change it
	to __syscall_nanosleep.

	* sysdeps/unix/sysv/linux/nanosleep.c: New file.

	* sysdeps/unix/sysv/linux/Makefile (sysdep_routines): Add
	s_nanosleep for misc.

Index: sysdeps/unix/sysv/linux/syscalls.list
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/unix/sysv/linux/syscalls.list,v
retrieving revision 1.1.1.23
diff -u -r1.1.1.23 syscalls.list
--- sysdeps/unix/sysv/linux/syscalls.list	1998/10/22 01:14:58	1.1.1.23
+++ sysdeps/unix/sysv/linux/syscalls.list	1998/11/17 06:05:11
@@ -28,7 +28,7 @@
 mremap		EXTRA	mremap		4	__mremap	mremap
 munlock		EXTRA	munlock		2	__munlock	munlock
 munlockall	EXTRA	munlockall	0	__munlockall	munlockall
-nanosleep	-	nanosleep	2	__libc_nanosleep	__nanosleep nanosleep
+s_nanosleep	-	nanosleep	2	__syscall_nanosleep	__libc_nanosleep
 nfsservctl	EXTRA	nfsservctl	3	nfsservctl
 pause		-	pause		0	__libc_pause	pause
 personality	init-first personality	1	__personality	personality
Index: sysdeps/unix/sysv/linux/Makefile
===================================================================
RCS file: /home/work/cvs/gnu/glibc/sysdeps/unix/sysv/linux/Makefile,v
retrieving revision 1.1.1.41
diff -u -r1.1.1.41 Makefile
--- sysdeps/unix/sysv/linux/Makefile	1998/11/13 23:47:31	1.1.1.41
+++ sysdeps/unix/sysv/linux/Makefile	1998/11/17 05:07:35
@@ -9,7 +9,8 @@
 endif
 
 ifeq ($(subdir),misc)
-sysdep_routines += sysctl clone llseek getresuid getresgid umount umount2
+sysdep_routines += sysctl clone llseek getresuid getresgid umount \
+		   umount2 s_nanosleep
 
 sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h sys/mtio.h \
 		  sys/io.h sys/klog.h sys/kdaemon.h \
--- /dev/null	Tue May  5 13:32:27 1998
+++ sysdeps/unix/sysv/linux/nanosleep.c	Tue Nov 17 08:13:45 1998
@@ -0,0 +1,79 @@
+/* Copyright (C) 1998 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+
+extern int __syscall_nanosleep (const struct timespec *req,
+				struct timespec *rem);
+
+int
+__nanosleep(const struct timespec *req, struct timespec *rem)
+{
+  sigset_t set, oset;
+  struct sigaction oact;
+  int save_errno = errno;
+  int ignored;
+  int ret;
+
+  /* Linux will wake up the system call, nanosleep, when SIGCHLD
+     arrives even if SIGCHLD is ignored. We have to deal with it
+     in libc. We block SIGCHLD first.  */
+  if (sigemptyset (&set) < 0 ||
+      sigaddset (&set, SIGCHLD) < 0 ||
+      sigprocmask (SIG_BLOCK, &set, &oset))
+    {
+      __set_errno (save_errno);
+      return -1;
+    }
+
+  /* We get the signal handler for SIGCHLD. */
+  if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
+    {
+      /* Restore the original signal mask.  */
+      (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
+      __set_errno (save_errno);
+      return -1;
+    }
+
+  /* Should we really block SIGCHLD?  */ 
+  ignored = (oact.sa_handler == SIG_IGN);
+
+  if (!ignored)
+    {
+      /* Restore the original signal mask.  */
+      (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
+      __set_errno (save_errno);
+    }
+
+  ret = __syscall_nanosleep (req, rem);
+
+  if (ignored)
+    {
+      save_errno = errno;
+      /* Restore the original signal mask.  */
+      (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
+      __set_errno (save_errno);
+    }
+
+  return ret;
+}
+
+weak_alias (__nanosleep, nanosleep)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]