|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2004
Città: Rovereto (tn)
Messaggi: 2187
|
[C] Signal (ctrl+z) problemi
Codice:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
void ciao(){
printf("ciao\n");
sleep(10);
}
int main(){
pid_t pid1,pid2;
signal (SIGTSTP,ciao);
pid1=fork();
if (!pid1){
printf("gimp\n");
execlp("gimp","gimp",0);
}
else {
pid2=fork();
if (!pid2){
printf("xmms\n");
execlp("xmms","xmms",0);
}else{
printf("stop\n");
sleep(60);
}
}
}
praticamente, se si da CTRL+Z dovrebbe stampare solo "ciao" e non bloccare niente.. invece stampa "ciao" e blocca entrambi i processi.. dove sta l'inghippo? |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
Modificherei il codice così: Codice:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
pid_t pid1,pid2; /* globali */
void ciao(){
printf("ciao\n");
kill (pid1, SIGCONT); /* riavvia figlio 1 */
kill (pid2, SIGCONT); /* riavvia figlio 2 */
sleep(10);
}
int main(){
signal (SIGTSTP,ciao);
pid1=fork();
if (!pid1){
printf("gimp\n");
execlp("gimp","gimp",0);
}
else {
pid2=fork();
if (!pid2){
printf("xmms\n");
execlp("xmms","xmms",0);
}else{
printf("stop\n");
sleep(60);
}
}
}
EDIT: c'è una documentazione dei segnali legati ai job qui.
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet Ultima modifica di andbin : 17-05-2006 alle 11:06. |
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Oct 2004
Città: Rovereto (tn)
Messaggi: 2187
|
Quote:
|
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
Basta mettere all'inizio del programma (nota, includere <termios.h>): Codice:
struct termios tios;
if (tcgetattr (1, &tios) == 0)
{
tios.c_cc[VSUSP] = _POSIX_VDISABLE;
if (tcsetattr (1, TCSANOW, &tios) != 0)
printf ("tcsetattr error\n");
}
else
printf ("tcgetattr error\n");
Non so se ti va bene, ovviamente.
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Apr 2004
Messaggi: 130
|
Codice:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void ciao(int unused)
{
write(STDOUT_FILENO, "ciao\n", 5);
}
int main(void)
{
signal(SIGTSTP, ciao);
if (! fork()) {
puts("gimp");
signal(SIGTSTP, SIG_IGN);
execlp("gimp", "gimp", NULL);
_exit(EXIT_FAILURE);
}
if (! fork()) {
puts("xmms");
signal(SIGTSTP, SIG_IGN);
execlp("xmms", "xmms", NULL);
_exit(EXIT_FAILURE);
}
puts("stop");
sleep(60);
puts("done");
wait(NULL);
wait(NULL);
return 0;
}
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Mar 2004
Messaggi: 1455
|
La signanl handler dovrebbe avere il seguente prototipo
void func(int signum). Hai provato a fargli scrivere solo ciao senza fare la sleep? Che cosa ottieni? Cmq se le tue intenzioni sono quelle di rendere il processo immune dal segnale SIGSTOP puoi fare molto velocemente con signal(SIGSTP,SIG_IGN);
__________________
Ciao ~ZeRO sTrEsS~ |
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Oct 2004
Città: Rovereto (tn)
Messaggi: 2187
|
Quote:
Codice:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
pid_t f1,f2;
void ciao(int unused)
{
kill(f1,SIGTSTP);
printf("ciao %d\n",f1);
}
int main(void)
{
signal(SIGTSTP, ciao);
if (! (f1=fork())) {
puts("gimp");#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void ciao(int unused)
{
write(STDOUT_FILENO, "ciao\n", 5);
}
int main(void)
{
signal(SIGTSTP, ciao);
if (! fork()) {
puts("gimp");
signal(SIGTSTP, SIG_IGN);
execlp("gimp", "gimp", NULL);
_exit(EXIT_FAILURE);
}
if (! fork()) {
puts("xmms");
signal(SIGTSTP, SIG_IGN);
execlp("xmms", "xmms", NULL);
_exit(EXIT_FAILURE);
}
puts("stop");
sleep(60);
puts("done");
wait(NULL);
wait(NULL);
return 0;
}
signal(SIGTSTP, SIG_IGN);
execlp("gimp", "gimp", NULL);
_exit(EXIT_FAILURE);
}
if (! (f2=fork()) ) {
puts("xmms");
signal(SIGTSTP, SIG_IGN);
execlp("xmms", "xmms", NULL);
_exit(EXIT_FAILURE);
}
puts("stop");
sleep(60);
puts("done");
wait(NULL);
wait(NULL);
return 0;
}
come posso fare? |
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Come può una _funzione_ impostata con "signal" essere ereditata dal processo chiamato in exec? Quando chiami la exec, il nuovo processo _sostituisce_ l'immagine in memoria del processo chiamante, comprese le funzioni che gestiscono i segnali. L'unica cosa che puoi ereditare tramite la exec è la _maschera_ dei segnali; ad esempio puoi impostare SIGTSTP come "mascherato" prima di effettuare le exec: Codice:
if (! fork()) {
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGTSTP);
sigprocmask(SIG_BLOCK, &ss, NULL);
execlp(<...>);
_exit(EXIT_FAILURE);
}
Codice:
if (! fork()) {
setsid();
execlp(<...>);
_exit(EXIT_FAILURE);
}
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
|
#9 | |
|
Senior Member
Iscritto dal: Oct 2004
Città: Rovereto (tn)
Messaggi: 2187
|
Quote:
a dire il vero se do SIGSTOP invece che SIGNSTP funziona tutto come dovrebbe il SIGIGN funziona.. prova
|
|
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Oct 2004
Città: Rovereto (tn)
Messaggi: 2187
|
così funzione (apre 2 gimp, blocca solo il primo, per 10 secondi)
Codice:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
pid_t f1,f2;
void ciao()
{
kill(f1,SIGSTOP);
printf("stoppo figlio 1 %d\n");
sleep(10);
kill(f1,SIGCONT);
}
int main(void)
{
signal(SIGTSTP, ciao);
if (! (f1=fork())) {
puts("gimp");
signal(SIGTSTP, SIG_IGN);
execlp("gimp", "gimp", NULL);
_exit(EXIT_FAILURE);
}
else
if (! (f2=fork()) ) {
puts("gimp2");
signal(SIGTSTP, SIG_IGN);
execlp("gimp", "gimp", NULL);
_exit(EXIT_FAILURE);
}
puts("stop");
sleep(60);
puts("done");
wait(NULL);
wait(NULL);
return 0;
}
|
|
|
|
|
|
#11 | |
|
Member
Iscritto dal: Apr 2004
Messaggi: 130
|
Quote:
Codice:
goku@big:~$ info libc Processes 'Executing a File' 2>/dev/null | > sed -ne '136,138p' | cut -d'.' -f1 && echo Signals that are set to be ignored in the existing process image are also set to be ignored in the new process image goku@big:~$ |
|
|
|
|
|
|
#12 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:40.



















