2013-05-12 1 views
2

내 서버가 여러 클라이언트를 지원해야하는 시점에 2 클라이언트로 작업하고 있다고 가정 해 봅시다. 다음 서버 모두 클라이언트가 자신의 PID (이것은 아버지와 아들의 관계 아니다! 그 두 개의 별도의 프로세스입니다) 통과하는 데 필요한,서버에서 보낸 두 개의 서로 다른 SIGUSR2 신호를 구분하는 방법은 무엇입니까?

#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; 

    } 

문제이며, 다음은

는 서버의 그가 첫 번째로 선택된 첫 번째 프로세스에 SIGUSR2의 신호를 보내고, 그렇다면이 프로세스는 X 유형의 문자로 작동합니다.

두 번째 프로세스 인 경우 Y 문자로 작업하십시오.

여기 클라이언트 : (1 또는 2 중 하나)

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 

    } 

내가 클라이언트에 정적 INT 변수를 사용하려고합니다 (flagger)이 SIGUSR2의 신호를 구별 할 수 있지만,이 때문에 도움이되지 않습니다, 각 클라이언트에 대해 은 0으로 시작하고 1에 도달하는 새로운 변수입니다.

내가 프로세스가 SIGUSR2을받은 1 시간과 다른 프로세스가 SIGUSR2을받은 하는 두 번째 시간 사이에 어떻게 구별 할 수 있습니까?

+1

Thee는 실수 방지 방식이 아니므로 신호가 아닌 FIFO 또는 파이프와 통신하는 것이 좋습니다. –

답변

4

데이터를 전달해야하는 경우 신호가 적절한 메커니즘이 아닙니다. 명명 된 파이프와 같은 다른 IPC 방법을 사용해보십시오.

관련 문제