gpt4 book ai didi

c - 如何区分Server发出的两个不同的SIGUSR2信号?

转载 作者:太空狗 更新时间:2023-10-29 11:45:50 24 4
gpt4 key购买 nike

我的服务器需要支持多个客户端,现在让我们假设我们是与 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 向first process 他是第一个被选中的人,如果是,那么该过程将处理 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

最佳答案

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

关于c - 如何区分Server发出的两个不同的SIGUSR2信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com