如何区分从服务器发送的两个不同的SIGUSR2信号?

时间:2013-05-12 07:09:48

标签: c linux process signals

我的服务器需要支持多个客户端,暂时让我们假设我们是 与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信号(第一个或第二个),但它对每个客户端都没有帮助static flagger是一个以0开头且到达1的新变量。

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

1 个答案:

答案 0 :(得分:4)

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