Linux: Synchronous Signal Delivery

Submitted by nimrod
on February 13, 2003 - 11:12pm

Linus Torvalds announced his Synchronous Signal Delivery mechanism - originally designed as a possible solution for select() and signal races - which uses a regular file descriptor to deliver pending signals. All the normal operations can be performed on it: you can pass it around, duplicate it, read it, etc.

This mechanism introduces the sigfd system call, which "creates a file descriptor that is associated with the particular thread that created it, and the particular signal mask that the user was interested in."

So, synchronous signal delivery allows a process to get signals on-demand, as opposed to it getting pelted by them. Linus pointed out one useful scenario: "So you can have a process that does a sigfd(), forks, and the child can then listen in on the specified signals of the parent."


From:     Linus Torvalds
To:       linux-kernel
Subject:  Synchronous signal delivery..
Date:     2003-02-13 19:46:10

Ok, 
 I talked about select() and signal races in Australia at IBM (forget who
exactly was involved so I just cc'd the usual suspects), and here's a
prototype example of what I suggested as a potential solution.

[ Warning: this goes on top of the current BK tree with all the signal 
  fixes and the generalized "dequeue_signal()" stuff. 

  Also note that it is a prototype, ie the packet returned to "read()" is 
  obviously not the full info, and there may be other issues with this. ]

It's a generic "synchronous signal delivery" method, and it uses a
perfectly regular file descriptor to deliver an arbitrary set of signals
that are pending.

It adds one new system call:

	fd = sigfd(sigset_t * mask, unsigned long flags);

which creates a file descriptor that is associated with the particular
thread that created it, and the particular signal mask that the user was
interested in. That file descriptor can be passed around all the normal
ways: it can be dup()'ed, given to somebody else with a AF_UNIX socket,
and obviously read() and select()/poll()'ed upon.

So you can have a process that does a sigfd(), forks, and the child can 
then listen in on the specified signals of the parent, for example.

NOTE! For it to be useful, the signals that you want to wait on and read()
using the file descriptor obviously have to be blocked, otherwise they'll 
just be delivered the old-fashioned way.

Here's a trivial example program using the new system call:

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

	#define __NR_syscall (259)

	int sigfd(sigset_t *mask, unsigned long flags)
	{
		void *vsyscall = (void *) 0xffffe000;
		int ret;
		asm volatile("call *%1"
			:"=a" (ret)
			:"m" (vsyscall),
			 "0" (__NR_syscall),
			 "b" (mask),
			 "c" (flags));
		return ret;
	}

	int main(void)
	{
		sigset_t mask;
		int fd, len;
		char buffer[1024];

		sigfillset(&mask);
		sigprocmask(SIG_BLOCK, &mask, NULL);

		fd = sigfd(&mask, 0);
		printf("sigfd returns %d\n", fd);
	
		len = read(fd, buffer, sizeof(buffer));
		printf("read returns %d (signal %d)\n", len, *(int *)buffer);
		return 0;
	}

Just run it, and send the process signals to see what happens.

The above example program is obviously totally useless, and any real use
would have to expand the implementation with addign the full siginfo to
the packet read (which is trivial apart from deciding on what format to
use - it would be good to not have it be architecture-dependent and in
particular it would be horrible to have different formats for different
compatibility layers).

Any real use would also probably be a select() or poll() loop.

			Linus

[full message, including the appended patch]


From: Davide Libenzi Subject: Re: Synchronous signal delivery.. Date: 2003-02-13 20:22:06 On Thu, 13 Feb 2003, Linus Torvalds wrote: > It's a generic "synchronous signal delivery" method, and it uses a > perfectly regular file descriptor to deliver an arbitrary set of signals > that are pending. > > It adds one new system call: > > fd = sigfd(sigset_t * mask, unsigned long flags); > > which creates a file descriptor that is associated with the particular > thread that created it, and the particular signal mask that the user was > interested in. That file descriptor can be passed around all the normal > ways: it can be dup()'ed, given to somebody else with a AF_UNIX socket, > and obviously read() and select()/poll()'ed upon. That's really nice, I like file-based interfaces. No plan to have a way to change the sig-mask ? Close and reopen ? What do you think about having timers through a file interface ? > Any real use would also probably be a select() or poll() loop. And sice it supports ->poll(), epoll. - Davide
From: Linus Torvalds Subject: Re: Synchronous signal delivery.. Date: 2003-02-13 20:25:26 On Thu, 13 Feb 2003, Davide Libenzi wrote: > > That's really nice, I like file-based interfaces. No plan to have a way to > change the sig-mask ? Close and reopen ? You can have multiple fd's open at the same time, which is a lot more convenient. I will _not_ add a fcntl-like or ioctl interface to this. They are ugly _and_ there are security issues (ie if you pass on the fd to somebody else, they must not be able to change the signal mask _you_ specified). > What do you think about having timers through a file interface ? One of the reasons for the "flags" field (which is not unused) was because I thought it might have extensions for things like alarms etc. Linus

Benefits?

Anonymous
on
February 14, 2003 - 1:28pm

What real-life benefits would this give to me, average Linux desktop-user? I'm not asking because I think it's useless, I'm asking because the technical jargon goes right over my head.

As an average desktop user?

Nick
on
February 14, 2003 - 1:48pm

Probably none.

Re: Benefits?

nimrod
on
February 14, 2003 - 1:53pm
    What real-life benefits would this give to me, average Linux desktop-user? I'm not asking because I think it's useless, I'm asking because the technical jargon goes right over my head.

This probably won't have visible changes for end-users. The ones that will benefit from this are programmers.

Signals are a way for the kernel to "signal" stuff to processes. They're modeled after interrupts. You use signals every day without knowing it.

A simple case is when you press Ctrl-C while running a program. Pressing Ctrl-C sends the SIGINT (the interrupt signal. its default action is to terminate the process) signal. If the programmer hasn't coded a signal handler for SIGINT into the program, the program will be terminated. If the program does have a signal handler coded in, then it can do some stuff when it gets that particular signal (in this case, SIGINT). For example, bash (when in interactive mode) handles SIGINT by just displaying a new prompt line, instead of quitting.

So signals are a way for the system to say to the program "Hey, a certain thing has happened, if you don't know how to handle this situation, then i will take the default course of action". You can take a look at the different signals on your system by looking at the signal manpage. Just run: man 7 signal (running plain "man signal" will probably display the manpage for the signal() system call, instead of the list of signals)

So this new system potentially allows _programmers_ to simplify signal handling in some situations; Users probably won't see visible changes.

users and processes can send

Anonymous
on
February 14, 2003 - 1:55pm

users and processes can send signals too, not only the kernel. see kill(1) and kill(2).
other than that i'm really impressed by this... now select() and poll() can work on signals.. i like this uniformity.

Ooops

nimrod
on
February 14, 2003 - 2:09pm

Yes, user process can send signals too. Sorry for that. My excuse is that I wrote that piece at about 06:30 (EST [not EDT, mind you]), and sleepy (being awake for more than 24 hours [no caffeine, mind you] makes you sleepy.)

My other excuse is that i was trying to simplify things by omitting that part.

My other excuse is that i was still half right, when a user process sends a signal, it needs to call kill(2), which is a system call. So the victim^Wreceiving process still gets the signal from the kernel.

except for performance, i don

Anonymous
on
February 14, 2003 - 1:57pm

except for performance, i don't see what more can the linux kernel give you. you shouldn't expect much more, all has been done (in the kernel area) :O)

Regarding performance (FreeBSD vs. Linux)

Anonymous
on
February 14, 2003 - 5:04pm

I'm not a FreeBSD-user, but from whatI have heard, it's smoother than Linux on the desktop. If the desktop (KDE for example) and X is the same, why is FreeBSD snappier than Linux? Two identical machines, running same software. Other is Linux, other is FreeBSD. FreeBSD is smoother and snappier. Why is that?

Re: Regarding performance (FreeBSD vs. Linux)

Hiryu
on
February 14, 2003 - 10:15pm

I have FreeBSD and Linux on my machine, and let me tell you, Linux (since linux 2.2) has been a lot faster and snappier with X on this system.

So much so that KDE 3.0.5a (I now have 3.1 which is faster than 3.0.5a in my experience) on linux is snappier than window maker on FreeBSD (let alone freebsd running any kde 3 release).

I currently have like FreeBSD 4.6 or so.

Re: linux kernel

Anonymous
on
February 14, 2003 - 10:14pm

There are still plenty of neat things out there, and eventually the improved infrastructure will enable people to work on other things. Personally, I think it'd be great when there is a simple, efficient and standard way to 'transport' processes to other systems over the network transparently.

Re: linux kernel

Anonymous
on
February 15, 2003 - 5:36am

www.openmosix.org is what you want

Re: www.openmosix.org

Anonymous
on
February 16, 2003 - 12:35pm

keyword: standard. openmosix isn't.

The most likely benefits will

Anonymous
on
February 14, 2003 - 5:54pm

The most likely benefits will be hidden from desktop users. Mostly, multithreaded programs which suffer from weird glitches due to signal delivery now have a more reliable mechanism.

An aside:

"There are currently 7 users and 255 guests online." Wow, two (2^N)-1 numbers there. How neat.

programs can get a lot simpler using this system call

Anonymous
on
February 14, 2003 - 7:35pm

So most likely the programs will become a little bit faster and the developers will squeeze a little bit more functionality in instead of sorting out signals.

umm... no

Anonymous
on
February 15, 2003 - 12:25am

programs won't get faster or have more functionality.

However, some "linux" programs will have a "feature" that makes porting to other platforms a pain.

Nice troll

Anonymous
on
February 15, 2003 - 1:12am

Actually, this feature is most likely going to be of use inside various libraries, such as threading libraries, and so will be "invisible" to the higher levels of the application. (Other than, of course, that the app works correctly and efficiently.)

porting

Anonymous
on
February 16, 2003 - 7:34am

I don't know the details of this new system call, but it seems like it could be implemented in user code (via a signal handler that writes to a pipe). So porting shouldn't be much of a problem.

There must be something I'm missing though; I don't see why this needs to be in the kernel at all.

porting - Linux to Solaris

Anonymous
on
March 23, 2004 - 11:08pm

is there any tool used to port applications from Linux to Solaris ?? anybody can help me to get this done ??

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.