【问题标题】:How to distinguish between two different SIGUSR2 signals sent from the Server?如何区分从服务器发送的两个不同的 SIGUSR2 信号?
【发布时间】:2013-05-12 07:09:47
【问题描述】:

我的服务器需要支持多个客户端,暂时让我们假设我们是 与2 客户合作。

这是服务器:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/stat.h>

#define FIFONAME "fifo_clientTOserver"
#define SHM_SIZE 1024  /* make it a 1K shared memory segment */
#define ROWS 10
#define COLS 10



void error(char* str)
{
    perror(str);
    exit(1);
}



int main(int argc, char *argv[])
{

    unlink(FIFONAME);               // remove any previous fifo pipes

    // create a FIFO named pipe - only if it's not already exists
    if(mkfifo(FIFONAME , 0666) < 0)
        error("mkfifo");

    /**
     * process 1
     */


    // open the fifo for reading

    int server_to_client = open(FIFONAME, O_RDONLY);
    int reading;

    while (1)
    {
        if (read(server_to_client, &reading ,sizeof(int)) < 0)
            perror("read");
        else
            break;
    }

    printf("Reading from the fifo : %d\n" , reading);
    if (close(server_to_client) < 0)
        error("close");

    // casting into pid_t
    pid_t pid = (pid_t)reading;

    // signal to the process that he's the first
    kill(pid, SIGUSR2);



    /**
     *  process 2
     */

    printf("Now waiting for process 2...\n");

    // doing it again - this time for the second process

    // remove any previous fifo pipes
    unlink(FIFONAME);

    // create a FIFO named pipe - only if it's not already exists
    if(mkfifo(FIFONAME , 0666) < 0)
        error("mkfifo");

    printf("Server tester1\n");

    server_to_client = open(FIFONAME, O_RDONLY);

    // grab the PID of process 2
    while (1)
    {
        if (read(server_to_client, &reading ,sizeof(int)) > 0)
            break;  // got the data
    }

    printf("Server tester2\n");

    printf("Reading from the fifo : %d\n" , reading);
    if (close(server_to_client) < 0)
        error("close");

    // casting into pid_t
    pid = (pid_t)reading;

    // signal to the process that he's the first
    kill(pid, SIGUSR2);




    return 0;

    }

问题是,两个客户端都需要传递他们的 PID(这不是父子关系!!!它们是两个独立的进程),然后服务器用SIGUSR2 向第一个进程发出信号,表明他是第一个选择,如果是,则该过程使用X 类型的字符。

另一方面,如果您是第二个进程,您将使用Y 类型的字符。

这是客户:

int static flagger = 0;
char process_char = 'a';

/**
 *  handler for SIGUSR2
 */
void my_handler(int signum)
{

    printf("foo bar\n");

    if (signum == SIGUSR2)
    {
        printf("Received SIGUSR2!\n");
        flagger++;
    }

    printf("flagger is :%d\n" , flagger);

    if (flagger == 1)
    {
        // then process works with "X"
            process_char = 'x';
            printf("I'm process 1, working with X char\n");
            // exit(1);
    }

    else if (flagger == 2)
    {
        process_char = 'Y';
        printf("I'm process 2 , working with Y char\n");
        // exit(1);
    }

}




void error(char* str)
{
    perror(str);
    exit(1);
}




int main(int argc, char *argv[])
{

    pid_t pid;

    /* get the process id */
    if ((pid = getpid()) < 0)
    {
        perror("unable to get pid");
    }
    else
    {
        printf("The process id is %d\n", pid);
    }

    int pidInt = (int)pid;      // convert the pid to int

    // write pid into the fifo

    int fd = open("fifo_clientTOserver",O_WRONLY);  // open the fifo for writing
    if(fd < 0)
    {
         perror("open");
         exit(1);
    }

    signal(SIGUSR2, my_handler);

    printf("Tester1\n");


    // writing the PID of the client into the pipe
    write(fd, &pidInt ,sizeof(int));

    close(fd);      // closing the pipe

    printf("Tester2\n");

    while(1)
    {
        printf("Waiting for the signal...\n");
        sleep(1);
    }


        // more code 

    }

我尝试在客户端(flagger)中使用静态 int 变量来区分 SIGUSR2 信号(第一个或第二个),但它没有帮助,因为对于每个客户端来说,静态 flagger是一个以 0 开头并到达 1 的新变量。

如何区分一个进程第一次收到SIGUSR2 和第二次另一个 进程收到SIGUSR2

【问题讨论】:

  • 这不是万无一失的方法,我建议使用 FIFO 或管道(而不是信号)进行通信。

标签: c linux process signals


【解决方案1】:

如果您需要传递数据,那么信号不是合适的机制。考虑使用不同的 IPC 方法,例如命名管道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多