C – 用信号()终止进程

我有一个服务器和一个客户端。 客户端可以select终止服务器。

对于即时通讯使用:

void killServer() { int server_p_id; FILE *file; file = fopen(SERVER_INFO, "r"); if(file == NULL) { fprintf(stderr, "\nErro ao abrir %s", SERVER_INFO); exit(EXIT_FAILURE); } fscanf(file, "%d", &server_p_id); fclose(file); kill -9 -server_p_id; unlink(SERVER_FIFO); } /* some code */ if (!strcasecmp("fim",buffer)) { signal(SIGUSR1, killServer); break; } 

问题是这个过程没有被终止。 这是正确的做法吗?

编辑:当前的代码

 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #include <signal.h> #include "dict.h" #define MAX 256 void killServer() { int server_p_id; FILE *file; file = fopen(SERVER_INFO, "r"); if(file == NULL) { fprintf(stderr, "\nErro ao abrir %s", SERVER_INFO); exit(EXIT_FAILURE); } fscanf(file, "%d", &server_p_id); fclose(file); kill(server_p_id, SIGKILL); unlink(SERVER_FIFO); } int main() { int s_fifo_fd; /* identificador do FIFO do servidor */ int c_fifo_fd; /* identificador do FIFO deste cliente */ pergunta_t perg; /* mensagem do "tipo" pergunta */ resposta_t resp; /* mensagem do "tipo" resposta */ char buffer[80]; /* para a leitura da palavra a traduzir */ char c_fifo_fname[25]; /* nome do FIFO deste cliente */ long fflags; int read_res; CLEAR; //Abrir fifo do servidor s_fifo_fd = open(SERVER_FIFO, O_WRONLY); /* bloqueante */ if (s_fifo_fd == -1) { fprintf(stderr, "\nO servidor não está a correr\n"); exit(EXIT_FAILURE); } //Cria fifo para receber resposta do servidor perg.pid_cliente = getpid(); sprintf(c_fifo_fname, CLIENT_FIFO, perg.pid_cliente); if (mkfifo(c_fifo_fname, 0777) == -1) { fprintf(stderr, "\nErro no FIFO para a resposta (1)"); close(s_fifo_fd); exit(EXIT_FAILURE); } //Abre o fifo para receber a resposta do servidor c_fifo_fd = open(c_fifo_fname, O_RDONLY | O_NONBLOCK); if (c_fifo_fd == -1) { fprintf(stderr, "\nErro no FIFO para a resposta (2)\n"); close(s_fifo_fd); unlink(c_fifo_fname); exit(EXIT_FAILURE); } fflags = fcntl(c_fifo_fd, F_GETFL); fflags ^= O_NONBLOCK; /* inverte a semântica de bloqueio */ fcntl(c_fifo_fd, F_SETFL, fflags); /* bloqueante = on */ perg.palavra[TAM_MAX-1] = '\0'; printf("\tIntroduza os comandos pretendidos\n\n"); /* Ciclo Principal */ while (1) /* caso escreva "fim" o programa termina */ { /* ---- a) OBTEM PERGUNTA ---- */ printf("[ADMIN] "); scanf("%s",buffer); if (!strcasecmp("fim",buffer)) { signal(SIGUSR1, killServer); break; } strncpy(perg.palavra,buffer,TAM_MAX-1); //copia a palavra lida do "buffer" para a "perg.palavra" /* ---- b) ENVIA A PERGUNTA ---- */ write(s_fifo_fd, & perg, sizeof(perg)); /* ---- c) OBTEM A RESPOSTA ---- */ read_res = read(c_fifo_fd, & resp, sizeof(resp)); if (read_res == sizeof(resp)) printf("[SERVER] %s\n", resp.palavra); else printf("Sem resposta ou resposta incompreensivel[%d]\n",read_res); } close(c_fifo_fd); close(s_fifo_fd); unlink(c_fifo_fname); printf("Programa Terminou com Exito!"); return 1; } 

kill是一个函数指针。 标准C不允许在函数指针上进行指针运算,尽管许多编译器将其作为(相当无意义的)扩展提供。

总之,你的代码是无效的C.

但是,完全有效的C代码kill(server_p_id, SIGKILL); 可以做你想做的事情。