Visit The New, Modern Unix Linux Community

Trying to block signal

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trying to block signal
# 1  
Trying to block signal

I have this code that doesnt do what it is suppose to do. It should block signal that I send while process is running. I press control+z while this process is running and it should be blocked but it isnt. When i press control+z it gives me this....
[1]+ Stopped
When I change SIGTSP into SIGINT then it works as it should. I press control+c and it ignores it and continues executing my program.
Can anyone tell me why this is happening.

  sigset_t s;
  int j = 0;

  for (i = 0 ; i < 10 ; i++){
     if ( sigprocmask(SIG_BLOCK,&first,NULL)==-1)

     fprintf("print something here\n");


# 2  
Moderator's Comments:
Mod Comment Unix/Linux Man Pages are found here, on our site. Please use them in these forums.

KILL(1) 		      Linux User's Manual		       KILL(1)

       kill - send a signal to a process

       kill [ -signal | -s signal ] pid ...
       kill [ -L | -V, --version ]
       kill -l	[ signal ]

       The  default  signal  for  kill is TERM. Use -l or -L to list available
       signals.  Particularly useful signals include  HUP,  INT,  KILL,  STOP,
       CONT,  and  0.	Alternate  signals  may be specified in three ways: -9
       -SIGKILL -KILL.	Negative PID values may be used to choose  whole  pro-
       cess  groups;  see the PGID column in ps command output. A PID of -1 is
       special; it indicates all processes except the kill process itself  and

       The  signals  listed  below  may  be available for use with kill.  When
       known constant, numbers and default behavior are shown.

       Name	Num   Action	Description
       ()									   ()

       0	  0   n/a	exit code indicates if a signal may be sent
       ALRM	 14   exit
       HUP	  1   exit
       INT	  2   exit
       KILL	  9   exit	this signal may not be blocked
       PIPE	 13   exit
       POLL	      exit
       PROF	      exit
       TERM	 15   exit
       USR1	      exit
       USR2	      exit
       VTALRM	      exit
       STKFLT	      exit	may not be implemented
       PWR	      ignore	may exit on some systems
       WINCH	      ignore
       CHLD	      ignore
       URG	      ignore
       TSTP	      stop	may interact with the shell
       TTIN	      stop	may interact with the shell
       TTOU	      stop	may interact with the shell
       STOP	      stop	this signal may not be blocked

# 3  
I have no idea what the relevence of DGPickett's post is.

The ctrl/z is being picked up by shell before it gets to your program. In foreground ctrl/z is commonly mapped to SUSP (suspend). The program can be brought to foreground with the "fg" Shell command.

To see the mapping of the various control keys in your session:
stty -a

Each and every one of these can (and often will) be disabled or changed to suit individual terminals or applications. See "man stty" for your O/S.

Many people re-map INTR to ctrl/c for consistency with MSDOS but on many terminals it is not mapped to anything by default.
This User Gave Thanks to methyl For This Post:
# 4  
One of the stop signals cannot be caught, something the kill not signal man page tells you. My very educational buddy truss/tusc tells us:
tusc -fap ksh -i

(p is a named pipe, so typing 'wc p' hangs blocked on open, and the cntrl-z stops it) that ^Z stty susp is SIGTSTP, which can be caught. You were blocking that signal, not SIGINT = cntrl-c usually. Maybe you need to catch it but do little? Else, Google 'UNIX signal tutorial' like I do at such times. Smilie
Moderator's Comments:
Mod Comment Unix/Linux Man Pages are found here, on our site. Please use them in these forums.

[532] read(0, 0x400059b0, 1024) .......................... [sleeping]
wc p
[532] read(0, "w c   p \n", 1024) ........................ = 5
[532] fork() ............................................. = 536
[536] fork() .......... (returning as child ...) ......... = 532
[536] execve(0x40056b32, 0x400564ac, 0x4005677c) ......... [entry]
                              argv[0] @ 0x4005647d: "wc"
                              argv[1] @ 0x4005649d: "p"
[536] execve("/usr/bin/wc", 0x400564ac, 0x4005677c) ...... = 0 [32-bit]
[536] open(0x7b0f0003, O_RDONLY|O_LARGEFILE, 0666) ....... [sleeping]
[532] waitpid(-1, WIFSTOPPED(SIGTSTP), WUNTRACED) ........ = 536
[532] sigvec(SIGCLD, 0x7b118258, 0x7b118268) ............. = 0
[532] sigvec(SIGCLD, 0x7b118258, 0x7b118268) ............. = 0
[532] sigvec(SIGCLD, 0x7b118198, 0x7b1181a8) ............. = 0
[532] ioctl(2, TIOCGPGRP, 0x7b1181d0) .................... = 0
[532] ioctl(2, TIOCSPGRP, 0x7b118198) .................... = 0
[532] ioctl(2, TCSETATTRF, 0x40004e80) ................... = 0
[532] sigprocmask(SIG_BLOCK, 0x7b1180f0, 0x7b1180d0) ..... = 0
[532] sigprocmask(SIG_SETMASK, 0x7b1180d0, NULL) ......... = 0
[532] sigvec(SIGCLD, 0x7b117d98, 0x7b117da8) ............. = 0
[532] sigvec(SIGCLD, 0x7b117d98, 0x7b117da8) ............. = 0
[532] lseek(2047, 864312, SEEK_SET) ...................... = 864312
[532] sigprocmask(SIG_BLOCK, 0x7b117f30, 0x7b117f10) ..... = 0
[532] read(2047, "w c   p \n\0", 1024) ................... = 6
[532] sigprocmask(SIG_SETMASK, 0x7b117f10, NULL) ......... = 0
[532] time(0x7b117c50) ................................... = 1287760639
[532] lseek(2047, 864318, SEEK_SET) ...................... = 864318
[532] sigprocmask(SIG_BLOCK, 0x7b117db0, 0x7b117d90) ..... = 0
[532] read(2047, 0x40055c98, 1024) ....................... = 0
[532] sigprocmask(SIG_SETMASK, 0x7b117d90, NULL) ......... = 0
[532] ioctl(0, TIOCGWINSZ, 0x7b117d10) ................... = 0
[1] + Stopped                  wc p
$ [532] write(2, "[ 1 ]   +   S t o p p e d       ".., 38) . = 38
[532] sigvec(SIGCLD, 0x7b117ed8, 0x7b117ee8) ............. = 0
[532] sigvec(SIGCLD, 0x7b117ed8, 0x7b117ee8) ............. = 0
[532] sigprocmask(SIG_BLOCK, 0x7b117db0, 0x7b117d90) ..... = 0
[532] read(0, 0x400059b0, 1024) .......................... [sleeping]

$ stty -a
speed 9600 baud; line = 0; 
rows = 25; columns = 80
min = 4; time = 0; 
intr = ^C; quit = ^\; erase = ^H; kill = ^U
eof = ^D; eol = ^@; eol2 <undef>; swtch = ^@
stop = ^S; start = ^Q; susp = ^Z; dsusp <undef>
werase <undef>; lnext <undef>
-parenb -parodd cs8 -cstopb hupcl cread -clocal -loblk -crts 
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc 
ixon ixany ixoff -imaxbel -rtsxoff -ctsxon -ienqak 
isig icanon -iexten -xcase echo echoe echok -echonl -noflsh 
-echoctl -echoprt -echoke -flusho -pendin 
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -tostop 

---------- Post updated at 11:53 AM ---------- Previous update was at 11:49 AM ----------


Introduction To Unix Signals Programming

Note: Older systems do not support the sigprocmask() system call. Instead, one should use the sigmask() and sigsetmask() system calls. If you have such an operating system handy, please read the manual pages for these system calls. They are simpler to use than sigprocmask, so it shouldn't be too hard understanding them once you've read this section.
These 2 Users Gave Thanks to DGPickett For This Post:
# 5  
Note: Unix and Linux Man Pages are found on our site, these forums, please use them when you post here. Ours are better anyway!

Moderator's Comments:
Mod Comment Unix/Linux Man Pages are found here, on our site. Please use them in these forums.
# 6  
What is the output from this command on your system?
stty -a


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #194
Difficulty: Easy
Perl was ranked in the top 10 according to the TIOBE Index for October 2019.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Signal function

Hello I want to know how can i use signal function in c for keyboard interrupt handling. what i exactly want is : my program is processing and if i press any key while processing , the program should call the interrupt and displays/prints that key and now goes back to processing. I added the... (5 Replies)
Discussion started by: Jahanzeb
5 Replies

2. Programming

queue a signal

A program have to receive signals and work agreed with it, but the process have to receive more than one signal when it is attending other. Those have to be queued to be attended later recived. how can i do that? thanks. (2 Replies)
Discussion started by: marmaster
2 Replies

3. Shell Programming and Scripting

Killed by signal 15.

Hi all I have Master script, Main script ,and 4 Child script. #!/bin/bash /export/home/user/ #!/bin/bash /export/home/user/ & /export/home/user/ & /export/home/user/ & /export/home/user/ &I run only script... (1 Reply)
Discussion started by: almanto
1 Replies

4. Programming

Signal catching

Hi! I want to catch all signals that my program receives print their name and then execute the default handler. Can you help me on that? I've tried the following code: #include <stdio.h> #include <unistd.h> #include <signal.h> void (*hnd)(int i); char signals = { "SIGHUP",... (7 Replies)
Discussion started by: dark_knight
7 Replies

5. Shell Programming and Scripting

signal script?

I have a script which invoke a java program, because the program requires file as input, hence the script would sleep a X seconds then check for file existence, if the file exists then program is invoker else, keep waiting until the time is up. My problem is that if there is a way to find out if my... (1 Reply)
Discussion started by: mpang_
1 Replies

6. Shell Programming and Scripting

Signal question

Who can explain the meaning of the &2 &1 or @, #, etc in the script? Is there any document which can explain the usage of these words in details? for example: ls /etc/sysconfig/network > /dev/null 2>&1 #@ bash, ksh and sh. Thanks in advance for ur advice. (1 Reply)
Discussion started by: GCTEII
1 Replies

7. UNIX for Advanced & Expert Users

Kill Signal

Hello, I'm doing a project of OS simulation (Process Scheduling, to be very specific). Can anyone, please, explain what exactly happens in the background when we see "Sending all processes the KILL signal...........". How is it sent to each process? Is it that something like a boolean is stored... (3 Replies)
Discussion started by: ameya
3 Replies

8. UNIX for Advanced & Expert Users

kill signal

Hello e'bdy, We have WebSphere MQ running on AIX 5.1 Every weekend MQ receives a kill -30 signal from some process or user and offloads a big error file. There is no way in MQ through which that process can be tracked. Is there something which i can do on UNIX level to trap the process? Best... (3 Replies)
Discussion started by: jhaavinash
3 Replies

9. Programming

Signal Names

Hi everyone, Is there a variable or built in function in the Unix env. for me to obtain the name of a signal that is caught? As far as I understand only a numeric value of the signal is returned to the handler. For example: void handler (int signum) { ... (2 Replies)
Discussion started by: laila63
2 Replies

10. UNIX for Dummies Questions & Answers

Alarm signal

Hi, when I execute a script on unix AIX, I've got an error message: "Execution: 85328 Signal d'alarme". If I edit this file with "vi", I ve got the same error after a while (about 1 minute). If I try with another user I still have the problem. But if I rename this file, no problem. My... (5 Replies)
Discussion started by: cgsteph
5 Replies

Featured Tech Videos