Defining Custom Signal


 
Thread Tools Search this Thread
Top Forums Programming Defining Custom Signal
# 1  
Old 07-27-2009
Defining Custom Signal

Is it possible to send a custom signal to a process?

e.g. Send signal 9999 to my process, which handles it with some custom handler.

How would one do this?
# 2  
Old 07-27-2009
SIGUSR1 and SIGUSR2 and "custom" signals. Nothing else but your code will raise or send those signals to your app.

SIGHUP is traditionally used to cause a daemon to close the logfiles it has open, and reread the initfile, if there is one.
# 3  
Old 07-27-2009
Is it possible to have more than 2 user-defined signals?
# 4  
Old 07-28-2009
You might check out the "real time" signals, whose numbers are defined as a range from SIGRTMIN to SIGRTMAX. Not all systems support them however. POSIX compliant ones should, but OSX for one does not last time I checked... Also, the first three realtime signals (SIGRTMIN+0, ..., SIGRTMIN+2) are reserved on some versions of Linux.

Last edited by Corona688; 07-28-2009 at 11:56 AM..
# 5  
Old 07-29-2009
OK, I have decided to just send SIGUSR1 and read instructions from a file.

However, once receiving SIGUSR1, I can no longer send SIGINT to stop the process. It breaks out of my sleep or pause call, but it doesn't stop execution. I have to create a special handler for SIGINT in order for it to work. However, SIGSEGV stops the process as usual. But this worries me - am I doing something wrong, and are there any other signals which will need special attention?
# 6  
Old 07-29-2009
We can't read your code from here unless you post it. I wonder if there are problems with your signal mask.
# 7  
Old 07-29-2009
Here is the code:

Code:
#include <Python.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#define MAX_STR_LEN 255

void catch_int(int sig_num);
void catch_usr1(int sig_num);
void write_run_file();
void run_action(char *line);
void run_script_function(char *fname, char *func);

int main(int argc, char *argv[]) {

  signal(SIGUSR1, catch_usr1);
  /*signal(SIGINT, catch_int);*/

  write_run_file();

  Py_SetProgramName(argv[0]);
  Py_Initialize();

  while(1) {
    printf("Still Alive!\n");
    sleep(1);
  }

  Py_Finalize();

  return 0;

}

void write_run_file() {

  FILE *fh;

  fh = fopen("test.RUN", "w");
  if(fh == NULL) {
    printf("Could not open .RUN file.\n");
    exit(1);
  }

  fprintf(fh, "%i", getpid());

  fclose(fh);

}

void catch_int(int sig_num) {

  exit(1);

}

void catch_usr1(int sig_num) {

  FILE *fh;
  char line[MAX_STR_LEN];

  sigset_t mask_set, old_set;

  /* reset for next time */
  signal(SIGUSR1, catch_usr1);

  /* mask any further signals */
  sigfillset(&mask_set);
  sigprocmask(SIG_SETMASK, &mask_set, &old_set);

  printf("Caught SIGUSR1\n");

  fh = fopen("action", "r");
  if(fh == NULL) {
    printf("Could not open file.\n");
    return;
  }
  
  while(fgets(line, MAX_STR_LEN, fh)) {
    printf("Running %s\n", line);
    run_action(line);
    printf("Finished action\n");
    fflush(stdout);
    fflush(stderr);
  }

  fclose(fh);

  /* restore old signal mask */
  sigprocmask(SIG_SETMASK, &old_set, NULL);

}

void run_action(char *line) {

  char *action, *fname, *func;
  char py_fun[] = "PYFUN";

  action = strtok(line, " ");

  if(strlen(action) == 0) {
    return;
  }

  if(strcmp(action, py_fun) == 0) {
    fname = strtok(NULL, " ");
    func = strtok(NULL, " ");
    run_script_function(fname, func);
  }

}

void run_script_function(char *fname, char *func) {

  FILE *fh;
  char call[MAX_STR_LEN];

  fh = fopen(fname, "r");
  if(fh == NULL) {
    printf("Could not open file %s\n", fname);
    exit(1);
  }

  PyRun_SimpleFile(fh, fname);

  fclose(fh);

  strcpy(call, func);
  strcat(call, "()");

  PyRun_SimpleString(call);

}

It actually happens both before and after receiving the first SIGUSR1. It also happens both with and without the mask.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Defining variable problem

Hi I'd say I'm having this weird problem where my script isn't taking the value off a variable or printing it. My code is like this: set count_C= `grep -c C mols` set count_H= `grep -c H mols` set count_O= `grep -c O mols` sed -i '7,7 s/$/ $count_C $count_O $count_H/g' input It... (8 Replies)
Discussion started by: saleheen
8 Replies

2. Programming

Problem defining a struct

I have the following code and getting the compilation errors baseLib/DynBaseObj.h:80: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:89: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:101:... (0 Replies)
Discussion started by: kristinu
0 Replies

3. UNIX for Dummies Questions & Answers

Help with defining PATH

Hi All, I have a trivial question but I dont know how to solve it. So basically I'm working on a USB key and I have a directory with some scripts which I use to work on files present in other directories within the USB or sometimes on the main harddisk too. The problem is every time I have to... (3 Replies)
Discussion started by: pawannoel
3 Replies

4. UNIX for Dummies Questions & Answers

Defining an alias FreeBSD

I have defined this alias as quick way to find out which mount point to use for a USB drive after inserting it: # alias da='dmesg | grep da | grep MB' However, when invoking it, it states the following: # da da: Command not found. Can someone explain what is the issue here and how it can be... (3 Replies)
Discussion started by: figaro
3 Replies

5. UNIX for Dummies Questions & Answers

defining variable in .profile

In root dir i have created a .profile file and added variable and assigned a path to it: a = '/dir/dir' export a but when i echo (echo $a) the path or use this variable the value or path not getting displayed. i tried executing the .profile and logging out and logging in, didnt workout. am... (1 Reply)
Discussion started by: abhi_n123
1 Replies

6. Programming

Defining the inputStream object

Question regarding extend issue What is the reason behind defining the inputStream object in this way... InputStream inputStream = new FileInputStream("c:\\input.txt"); I know that FileInputStream extends InputStream, is there anything else ? I mean we could have define it like ... (0 Replies)
Discussion started by: yahyaaa
0 Replies

7. UNIX and Linux Applications

Problems with defining triggers

I am running the example from the following webpage: MySQL :: MySQL 5.0 Reference Manual :: 12.1.11 CREATE TRIGGER Syntax and the problem is that triggers cannot be defined for some reason: CREATE DATABASE IF NOT EXISTS triggertest; USE triggertest; CREATE TABLE test1(a1 INT); CREATE TABLE... (3 Replies)
Discussion started by: figaro
3 Replies

8. Shell Programming and Scripting

defining variables

Hey all, I was wondering if someone would take a look at this script I'm working on. I don't know if i have the syntax correct for my variables and if the for loop is written correctly. any assistance would be greatly appreciated. #!/usr/bin/bash ###########################################... (12 Replies)
Discussion started by: em23
12 Replies

9. AIX

defining a printer in qconfig

I've got a modified samba script (named it winprint) that I can use to print out to a to a shared Win Network printer from an AIX machine. This is a modification of the samba provided smbprint script changed to work under AIX as the backend for a queue. It does not read a config file I can print... (0 Replies)
Discussion started by: matheeq
0 Replies

10. UNIX for Dummies Questions & Answers

Defining Variables

I'm trying to define a variable named sin I already have a variable named cos, which has the value "hello" I want sin to have the value of "hellothere", so sin would be something like sin = $cos & "there" but I'm not sure that I know the syntax. Can anyone help? :confused: (4 Replies)
Discussion started by: sailorliones
4 Replies
Login or Register to Ask a Question