thread_sigsend Subroutine

Purpose

Sends a signal to the specified thread.

Library

Standard C library (libc.a)

Syntax

#include <sys/thread.h>

int thread_sigsend (tid, signal)
tid_t tid;
int signal;

Description

The thread_sigsend subroutine allows a thread in one process to send a signal to a specific thread in the same or another process. If a value of -1 is passed in the tid parameter field, the signal will be delivered to the calling thread.

The thread to receive the signal is identified by the kernel thread ID which has global scope. This can be obtained by the application using the thread_self system call. Only 1:1 thread mode is supported. The result for M:N thread mode is undefined.

Sending a signal number of 0 will cause only error checking to be performed. No signal be delivered to the target thread.

The effect of a signal will be same as in the case of kill() or pthread_kill() system calls, as explained in the sigaction section available at the IBM® Power Systems servers and AIX® Information Center.

To send a signal to a thread in another process, the real or the effective user ID of the sending process must match the real or effective user ID of the receiving process. Alternatively, if the sending process has root user authority or the ACT_P_SIGPRIV privilege, the sending process may send a signal to any thread. In case of insufficient privileges, an EPERM is returned in the global errno variable.

Parameters

tid
Identifier of thread that will receive the signal. A value of -1 will cause the signal to be delivered to the calling thread.
signal
The effect of a signal will be same as in the case of kill() or pthread_kill() system calls, as explained in the sigaction section available at the IBM Power Systems servers and AIX Information Center.

Return Values

0
The thread_sigsend was successful.
-1
The thread_sigsend was unsuccessful. Global variable errno is set to indicate the error.

Error Codes

EPERM
The thread issuing the signal does not have sufficient privileges to send the signal to the target thread.
ESRCH
The target thread could not be found.
EINVAL
Invalid signal number.

Example

mykill.c :

#include <sys/thread.h>
#include <sys/signal.h>
int main(int argc, char *argv[])
{
    int rc, sig;
    tid_t tid;
    if (argc < 3) {
       printf("Syntax: %s <tid> <signo>\n", argv[0]);
       exit(0);
}
    tid = atoi(argv[1]);
    sig = atoi(argv[2]);
    if (thread_sigsend(tid, signo) == -1)
       perror("thread_sigsend returned error");
       printf("Sent signal %d to thread %d\n",sig,tid);
}
mythread.c :
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
void *thread_func(void *);
void sighand(int signo)
{
      printf("-- Received signal %d in thread %d\n",
      signo, thread_self());
}
int main(int argc, char *argv[])
{
    int rc,i,signo;
    pthread_t *ptid;
    struct sigaction actions;
    int numthreads;
    if (argc < 3) {
       printf("Syntax: %s <numthreads> <signo>\n", argv[0]);
       exit(0);
    }
    numthreads = atoi(argv[1]);
        if (numthreads < 1)
        numthreads = 1;
    signo = atoi(argv[2]);
    ptid = (pthread_t *)calloc(1,
    numthreads*sizeof(pthread_t));
    pthread_init();
    memset(&actions, 0, sizeof(actions));
    sigemptyset(&actions.sa_mask);
    actions.sa_flags = 0;
    actions.sa_handler = sighand;
    rc = sigaction(signo,&actions,NULL);
    for (i=0; i<numthreads; i++) {
        rc = pthread_create(&ptid[i],NULL,thread_func, NULL);
           if (rc != 0) {
               printf("pthread_create func1 failed. rc =
                  %d\n",rc);
                  exit(-1);
               }
        }
    for (i=0; i<numthreads; i++)
       pthread_join(ptid[i],NULL);
    free(ptid);
}
void *thread_func(void *p)
{
    int rc;
    tid_t tid = thread_self();
    printf("Thread %d started\n", tid);
    rc = sleep(20);
    if (rc != 0) {
       printf("tid %d woken up with rc %d, errno %d\n",
          tid, rc, errno);
       eturn NULL;
    }
       printf("tid %d completed sleep\n", tid);
       pthread_exit(NULL);
}
Output:
# ./mythread 3 30 &
[1] 192734
Thread 684281 started
Thread 786593 started
Thread 1101959 started
# ./mykill 786593
Sent signal 30 to 786593
-- Received signal 30 in thread 786593
tid 786593 woken up with rc 15, errno 0
# ./mykill 684281
Sent signal 30 to 684281
-- Received signal 30 in thread 684281
tid 684281 woken up with rc 9, errno 0
# tid 1101959 completed sleep