Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何区分从服务器发送的两个不同的SIGUSR2信号?_C_Linux_Process_Signals - Fatal编程技术网

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

C 如何区分从服务器发送的两个不同的SIGUSR2信号?,c,linux,process,signals,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> #i

我的服务器需要支持多个客户端,现在让我们假设 与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;

    }
我尝试在客户端标记器中使用一个静态int变量来区分SIGUSR2信号1或2,但没有帮助,因为对于每个客户端,静态标记器都是一个新变量,从0开始,到达1


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

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

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

您不是一种无故障的方式,我建议与FIFOS或管道通信,而不是用信号。
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 

    }