[英]C - Terminating a process with signal()
I have a server and a client. 我有一个服务器和一个客户端。 The client has the option to terminate the server. 客户端可以选择终止服务器。
For that im using: 为此我使用:
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;
}
The problem is that the process is not being terminated. 问题是该进程没有终止。 Is this the right way to do it ? 这是正确的方法吗?
EDIT: Current code 编辑:当前代码
#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
is a function pointer. kill
是一个函数指针。 Standard C does not allow pointer arithmetic on function pointers, though many compilers offer it as a (fairly meaningless) extension. 标准C不允许对函数指针进行指针算术运算,尽管许多编译器将其作为(相当无意义的)扩展名提供。
In short, your code isn't valid C. 简而言之,您的代码不是有效的C。
However, the perfectly valid C code kill(server_p_id, SIGKILL);
但是,完全有效的C代码kill(server_p_id, SIGKILL);
may well do what you want. 可能会做你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.