Estou trabalhando em um projeto universitário que precisa ser escrito no GNU C e executado no servidor Ubuntu. Meu sistema operacional primário em casa é o Ubuntu 14.04 no meu PC. O projeto é sobre programação multi thread e eu sempre prendi quando precisei enviar um sinal do processo filho para o pai.
Eu usei o strace para investigar o envio do sinal e descobri que ou os sinais não foram enviados ou não chegaram ao manipulador. Finalmente eu escrevi o aplicativo de manipulador singal mais simples para testar o motivo.
Aqui está o código
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h> // DateTime kezeles
#include <fcntl.h> // Rendszerhivasok file
#include <unistd.h> // fork
#include <sys/types.h> // pid_t típus
#include <sys/wait.h> // wait, waitpid
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
void HandlerParent(int signalNumber){
printf("Parent received signal: %d\n", signalNumber);
}
int main (void)
{
signal(SIGUSR1, HandlerParent);
signal(SIGUSR2, HandlerParent);
kill(getpid(), SIGUSR1);
sleep(3);
return 0;
}
Então, novamente, este não é o código do projeto. Eu chutei para fora no meu PC e acabou sem chegar ao manipulador. Eu fiz o upload para o servidor da universidade e funcionando bem. Deixe-me anexar os dois stacktraces:
Resultado no meu PC
user@userPC:~/fold/fold1/test$ ./ownkill
Nenhum texto de resultado ... strace:
user@userPC:~/fold/fold1/test$ strace -Ff -tt ./ownkill
20:22:46.939854 execve("./ownkill", ["./ownkill"], [/* 66 vars */]) = 0
20:22:46.940705 brk(0) = 0xe2e000
20:22:46.941014 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.941220 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba7b000
20:22:46.941499 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:22:46.941622 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
20:22:46.941921 fstat(3, {st_mode=S_IFREG|0644, st_size=145965, ...}) = 0
20:22:46.942198 mmap(NULL, 145965, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa16ba57000
20:22:46.942324 close(3) = 0
20:22:46.942397 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.942515 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
20:22:46.942664 read(3, "7ELFUSER@SERVER:~/fold/fold1> ./ownkill
Parent received signal: 10
USER@SERVER:~/fold/fold1> strace -Ff -tt ./ownkill
20:13:21.027730 execve("./ownkill", ["./ownkill"], [/* 64 vars */]) = 0
20:13:21.031375 brk(0) = 0x602000
20:13:21.033631 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d50000
20:13:21.035918 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:13:21.038207 open("/etc/ld.so.cache", O_RDONLY) = 3
20:13:21.040400 fstat(3, {st_mode=S_IFREG|0644, st_size=200673, ...}) = 0
20:13:21.042700 mmap(NULL, 200673, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7feec2d1f000
20:13:21.044877 close(3) = 0
20:13:21.047067 open("/lib64/libc.so.6", O_RDONLY) = 3
20:13:21.049276 read(3, "7ELF#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h> // DateTime kezeles
#include <fcntl.h> // Rendszerhivasok file
#include <unistd.h> // fork
#include <sys/types.h> // pid_t típus
#include <sys/wait.h> // wait, waitpid
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
void HandlerParent(int signalNumber){
printf("Parent received signal: %d\n", signalNumber);
}
int main (void)
{
signal(SIGUSR1, HandlerParent);
signal(SIGUSR2, HandlerParent);
kill(getpid(), SIGUSR1);
sleep(3);
return 0;
}
user@userPC:~/fold/fold1/test$ ./ownkill
user@userPC:~/fold/fold1/test$ strace -Ff -tt ./ownkill
20:22:46.939854 execve("./ownkill", ["./ownkill"], [/* 66 vars */]) = 0
20:22:46.940705 brk(0) = 0xe2e000
20:22:46.941014 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.941220 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba7b000
20:22:46.941499 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:22:46.941622 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
20:22:46.941921 fstat(3, {st_mode=S_IFREG|0644, st_size=145965, ...}) = 0
20:22:46.942198 mmap(NULL, 145965, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa16ba57000
20:22:46.942324 close(3) = 0
20:22:46.942397 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
20:22:46.942515 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
20:22:46.942664 read(3, "7ELFUSER@SERVER:~/fold/fold1> ./ownkill
Parent received signal: 10
USER@SERVER:~/fold/fold1> strace -Ff -tt ./ownkill
20:13:21.027730 execve("./ownkill", ["./ownkill"], [/* 64 vars */]) = 0
20:13:21.031375 brk(0) = 0x602000
20:13:21.033631 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d50000
20:13:21.035918 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
20:13:21.038207 open("/etc/ld.so.cache", O_RDONLY) = 3
20:13:21.040400 fstat(3, {st_mode=S_IFREG|0644, st_size=200673, ...}) = 0
20:13:21.042700 mmap(NULL, 200673, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7feec2d1f000
20:13:21.044877 close(3) = 0
20:13:21.047067 open("/lib64/libc.so.6", O_RDONLY) = 3
20:13:21.049276 read(3, "7ELF%pre%%pre%%pre%%pre%%pre%%pre%%pre%%pre%%pre%%pre%>%pre%%pre%%pre%%pre%04%pre%%pre%%pre%%pre%%pre%"..., 832) = 832
20:13:21.051473 fstat(3, {st_mode=S_IFREG|0755, st_size=1661454, ...}) = 0
20:13:21.053668 mmap(NULL, 3528776, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7feec27d5000
20:13:21.055848 fadvise64(3, 0, 3528776, POSIX_FADV_WILLNEED) = 0
20:13:21.058040 mprotect(0x7feec2929000, 2097152, PROT_NONE) = 0
20:13:21.060232 mmap(0x7feec2b29000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x154000) = 0x7feec2b29000
20:13:21.062417 mmap(0x7feec2b2e000, 18504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7feec2b2e000
20:13:21.064619 close(3) = 0
20:13:21.066834 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1e000
20:13:21.069021 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1d000
20:13:21.071213 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1c000
20:13:21.073461 arch_prctl(ARCH_SET_FS, 0x7feec2d1d700) = 0
20:13:21.075774 mprotect(0x7feec2b29000, 16384, PROT_READ) = 0
20:13:21.077954 mprotect(0x600000, 4096, PROT_READ) = 0
20:13:21.080143 mprotect(0x7feec2d51000, 4096, PROT_READ) = 0
20:13:21.082318 munmap(0x7feec2d1f000, 200673) = 0
20:13:21.084716 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.086917 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.089118 getpid() = 16382
20:13:21.091313 kill(16382, SIGUSR1) = 0
20:13:21.092463 --- SIGUSR1 (User defined signal 1) @ 0 (0) ---
20:13:21.094575 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
20:13:21.096769 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d4f000
20:13:21.098964 write(1, "Parent received signal: 10\n", 27Parent received signal: 10) = 27
20:13:21.101292 rt_sigreturn(0x1) = 0
20:13:21.103473 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
20:13:21.105654 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:13:21.107841 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
20:13:21.110018 nanosleep({3, 0}, {3, 0}) = 0
20:13:24.112493 exit_group(0) = ?
%pre%%pre%%pre%%pre%%pre%%pre%%pre%%pre%>%pre%%pre%%pre%%pre%0%pre%%pre%%pre%%pre%%pre%"..., 832) = 832
20:22:46.942749 fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
20:22:46.942818 mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa16b495000
20:22:46.942883 mprotect(0x7fa16b650000, 2097152, PROT_NONE) = 0
20:22:46.942948 mmap(0x7fa16b850000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7fa16b850000
20:22:46.943025 mmap(0x7fa16b856000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa16b856000
20:22:46.943097 close(3) = 0
20:22:46.943167 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba56000
20:22:46.943234 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba54000
20:22:46.943303 arch_prctl(ARCH_SET_FS, 0x7fa16ba54740) = 0
20:22:46.943509 mprotect(0x7fa16b850000, 16384, PROT_READ) = 0
20:22:46.943582 mprotect(0x600000, 4096, PROT_READ) = 0
20:22:46.943654 mprotect(0x7fa16ba7d000, 4096, PROT_READ) = 0
20:22:46.943715 munmap(0x7fa16ba57000, 145965) = 0
20:22:46.943816 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943895 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943966 getpid() = 15023
20:22:46.944022 kill(15023, SIGUSR1) = 0
20:22:46.944082 rt_sigprocmask(SIG_BLOCK, [CHLD], [USR1], 8) = 0
20:22:46.944148 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:22:46.944208 rt_sigprocmask(SIG_SETMASK, [USR1], NULL, 8) = 0
20:22:46.944267 nanosleep({3, 0}, 0x7fff71e310f0) = 0
20:22:49.944652 exit_group(0) = ?
20:22:49.944888 +++ exited with 0 +++
%pre%%pre%%pre%%pre%%pre%%pre%%pre%>%pre%%pre%%pre%%pre%04%pre%%pre%%pre%%pre%%pre%"..., 832) = 832
20:13:21.051473 fstat(3, {st_mode=S_IFREG|0755, st_size=1661454, ...}) = 0
20:13:21.053668 mmap(NULL, 3528776, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7feec27d5000
20:13:21.055848 fadvise64(3, 0, 3528776, POSIX_FADV_WILLNEED) = 0
20:13:21.058040 mprotect(0x7feec2929000, 2097152, PROT_NONE) = 0
20:13:21.060232 mmap(0x7feec2b29000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x154000) = 0x7feec2b29000
20:13:21.062417 mmap(0x7feec2b2e000, 18504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7feec2b2e000
20:13:21.064619 close(3) = 0
20:13:21.066834 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1e000
20:13:21.069021 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1d000
20:13:21.071213 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d1c000
20:13:21.073461 arch_prctl(ARCH_SET_FS, 0x7feec2d1d700) = 0
20:13:21.075774 mprotect(0x7feec2b29000, 16384, PROT_READ) = 0
20:13:21.077954 mprotect(0x600000, 4096, PROT_READ) = 0
20:13:21.080143 mprotect(0x7feec2d51000, 4096, PROT_READ) = 0
20:13:21.082318 munmap(0x7feec2d1f000, 200673) = 0
20:13:21.084716 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.086917 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7feec28079e0}, {SIG_DFL, [], 0}, 8) = 0
20:13:21.089118 getpid() = 16382
20:13:21.091313 kill(16382, SIGUSR1) = 0
20:13:21.092463 --- SIGUSR1 (User defined signal 1) @ 0 (0) ---
20:13:21.094575 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
20:13:21.096769 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7feec2d4f000
20:13:21.098964 write(1, "Parent received signal: 10\n", 27Parent received signal: 10) = 27
20:13:21.101292 rt_sigreturn(0x1) = 0
20:13:21.103473 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
20:13:21.105654 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:13:21.107841 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
20:13:21.110018 nanosleep({3, 0}, {3, 0}) = 0
20:13:24.112493 exit_group(0) = ?
%pre%%pre%%pre%%pre%%pre%%pre%%pre%%pre%>%pre%%pre%%pre%%pre%0%pre%%pre%%pre%%pre%%pre%"..., 832) = 832
20:22:46.942749 fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
20:22:46.942818 mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa16b495000
20:22:46.942883 mprotect(0x7fa16b650000, 2097152, PROT_NONE) = 0
20:22:46.942948 mmap(0x7fa16b850000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7fa16b850000
20:22:46.943025 mmap(0x7fa16b856000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa16b856000
20:22:46.943097 close(3) = 0
20:22:46.943167 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba56000
20:22:46.943234 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa16ba54000
20:22:46.943303 arch_prctl(ARCH_SET_FS, 0x7fa16ba54740) = 0
20:22:46.943509 mprotect(0x7fa16b850000, 16384, PROT_READ) = 0
20:22:46.943582 mprotect(0x600000, 4096, PROT_READ) = 0
20:22:46.943654 mprotect(0x7fa16ba7d000, 4096, PROT_READ) = 0
20:22:46.943715 munmap(0x7fa16ba57000, 145965) = 0
20:22:46.943816 rt_sigaction(SIGUSR1, {0x40064d, [USR1], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943895 rt_sigaction(SIGUSR2, {0x40064d, [USR2], SA_RESTORER|SA_RESTART, 0x7fa16b4cbc30}, {SIG_DFL, [], 0}, 8) = 0
20:22:46.943966 getpid() = 15023
20:22:46.944022 kill(15023, SIGUSR1) = 0
20:22:46.944082 rt_sigprocmask(SIG_BLOCK, [CHLD], [USR1], 8) = 0
20:22:46.944148 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
20:22:46.944208 rt_sigprocmask(SIG_SETMASK, [USR1], NULL, 8) = 0
20:22:46.944267 nanosleep({3, 0}, 0x7fff71e310f0) = 0
20:22:49.944652 exit_group(0) = ?
20:22:49.944888 +++ exited with 0 +++
Resultado no servidor
%pre%
O sinal funcionou bem ... strace:
%pre%
Informações adicionais
Eu também testei a função sigaction (), e isso também não está funcionando! : (
Por favor me ajude, o que pode causar esse problema. Não sei se isso pode ser causado por um aplicativo instalado ou biblioteca. Também pedi a alguns dos meus amigos que usam o Ubuntu 14.04 para testá-lo e funcionaram bem.
Como posso iniciar a investigação da causa raiz? O projeto é tão grande para desenvolvê-lo no servidor. Então eu preciso usar meu PC em vez do servidor. Eu uso o Geany para desenvolvimento.
Por favor, levante sua dúvida ou preocupação nos comentários, e eu tento respondê-la o mais rápido possível.
Muito obrigado antecipadamente!