Sponsored Content
Full Discussion: Defining Custom Signal
Top Forums Programming Defining Custom Signal Post 302339049 by joha on Wednesday 29th of July 2009 03:02:51 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PTHREAD_KILL(3) 					     Linux Programmer's Manual						   PTHREAD_KILL(3)

NAME
pthread_kill - send a signal to a thread SYNOPSIS
#include <signal.h> int pthread_kill(pthread_t thread, int sig); Compile and link with -pthread. DESCRIPTION
The pthread_kill() function sends the signal sig to thread, a thread in the same process as the caller. The signal is asynchronously directed to thread. If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a thread ID. RETURN VALUE
On success, pthread_kill() returns 0; on error, it returns an error number, and no signal is sent. ERRORS
EINVAL An invalid signal was specified. ESRCH No thread with the ID thread could be found. CONFORMING TO
POSIX.1-2001. NOTES
Signal dispositions are process-wide: if a signal handler is installed, the handler will be invoked in the thread thread, but if the dispo- sition of the signal is "stop", "continue", or "terminate", this action will affect the whole process. SEE ALSO
kill(2), sigaction(2), sigpending(2), pthread_self(3), pthread_sigmask(3), raise(3), pthreads(7), signal(7) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2012-08-19 PTHREAD_KILL(3)
All times are GMT -4. The time now is 01:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy