void printToScreen(){
write(1, boardString, strlen(boardString)) == -1 ? writeError() : 1;
write(1, "\n", 1) == -1 ? writeError() : 1;
int i = 0;
char * pch;
pch = strtok (boardString, ",");
int len = strlen(pch);
int count=0;
// insert the number and pads it with zeros, '|' and spaces
while (pch != NULL){
printf("\niteration %d\n", count++);
switch(len){
case 1:
if(*pch == '0'){
write(1, "| ", 7) == -1 ? writeError() : 1;
break;
}
write(1, "| 000", 5) == -1 ? writeError() : 1;
write(1, pch, 1) == -1 ? writeError() : 1;
write(1, " ", 1) == -1 ? writeError() : 1;
break;
case 2:
write(1, "| 00", 4) == -1 ? writeError() : 1;
write(1, pch, 2) == -1 ? writeError() : 1;
write(1, " ", 1) == -1 ? writeError() : 1;
break;
case 3:
write(1, "| 0", 3) == -1 ? writeError() : 1;
write(1, pch, 3) == -1 ? writeError() : 1;
write(1, " ", 1) == -1 ? writeError() : 1;
break;
case 4:
write(1, "| ", 2) == -1 ? writeError() : 1;
write(1, pch, 3) == -1 ? writeError() : 1;
write(1, " ", 1) == -1 ? writeError() : 1;
break;
default:
break;
}
// update pch, len
pch = strtok (NULL, ",");
len = strlen(pch);
i++;
// move to next line
if (i % 4 == 0)
write(1, "|\n", 2) == -1 ? writeError() : 1;
}
}
void sig_hand(int sig){
printf("got signal\n");
read(STDIN, boardString, STRING_SIZE) == -1 ? readError() : 1;
printToScreen();
}
我有这个处理SIGUSR1信号的信号处理程序 .
boardString是一个16字节的数组,字符串格式,','作为分隔符 .
printToScreen()只是以4X4矩阵格式打印
我有第二个进程,在boardString中的每次更新后将SIGUSR1发送到此进程 .
我的问题是我的程序终止时printToScreen函数没有't end. it prints the first iteration just fine and then I get this msg 2651326 and that' .
我无法理解为什么它会终止,以及"User signal 1"的意思 .
2 回答
从描述中可以看出,您将信号处理程序设置为一次性处理程序(因此在第一次接收到信号时调用它,然后将其重置为默认操作,即终止进程;然后,shell通常会打印“用户信号1”消息 .
你是如何设置信号处理程序的?如果您使用的是
signal
且正在使用SysV派生的unix变体,则默认为单次使用 . 请改用sigaction
,并确保未设置SA_RESETHAND
标志 .