简体   繁体   中英

sigsuspend does not return when being called by a golang program via cgo

I am working on a project with golang. The project calls C API of LSF (A job scheduler https://en.wikipedia.org/wiki/Platform_LSF ). Some of the APIs call sigsuspend() when communicating with LSF system.

During test, I found those APIs never return. But they work fine when being called by a C program. Therefore I write a simple test program as follows (based on Go: Get signal origin :-))

The test program confirmed my finding. It prints "before sigsuspend" and "Sent by xxxx", but never prints "after sigsuspend".

Can anyone throw light on it? How to make those APIs work in a golang program just like being called by a C program?

package main

/*
#include <stdio.h>
#include <signal.h>
#include <string.h>

struct sigaction old_action;
void handler(int signum, siginfo_t *info, void *context) {
    printf("Sent by %d\n", info->si_pid);
}

void test() {
    sigset_t pauseMask;
    struct sigaction action;

    sigaction(SIGUSR1, NULL, &action);
    memset(&action, 0, sizeof action);
    sigfillset(&action.sa_mask);
    action.sa_sigaction = handler;
    action.sa_flags = SA_SIGINFO | SA_ONSTACK;
    sigaction(SIGUSR1, &action, &old_action);
    sigemptyset(&pauseMask);
    printf("before sigsuspend \n");
    sigsuspend(&pauseMask);
    printf("after sigsuspend \n");
}
*/
import "C"

import (
    "os"
    "syscall"
    "time"
)

/* https://stackoverflow.com/questions/30219751/golang-get-signal-origin */
func main() {
    go C.test()
    pid := os.Getpid()
    for {
        syscall.Kill(pid, syscall.SIGUSR1)
        time.Sleep(time.Second * 5)
    }
}

Thanks

Yong

The root cause is that in a multiple-thread program, to allow sigsuspend return, we have to block all the other threads with specified signal. However in golang, we cannot configure the sigmask of management threads of golang.

Please refer to following link for detail info

https://groups.google.com/forum/#!topic/golang-nuts/sYo0iGfkVRg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM