I don't think it's a good idea to call swapcontext() by main() even indirectly, main is special in many ways that most threads aren't. What happens when you call swapcontext() from a third thread instead?
You are blowing the stack somewhere, I don't see context referencing either.
Here is the standard POSIX example for this -- note the use of static functions and some other context setups you do not seem to have:
Note corona688's comment - the only swpcontext call in main above is one to set context to a function, rather than calling it. main is not swapping context repeatedly.
Sorry for the late reaction, I wasn't online during the holidays.
@jim mcnamara: How can I detect whether I am overflowing the signal stack or not? And what do you mean with context referencing? I've changed the stack size from 65365 to 4096. But it doesn't have any effects.
Btw. I found a code sample which is similar to my problem. But this example seems to work. The code is in the listing below (source: CIS381 - Fall 2008). The important functions are scheduler() and timer_interrupt().
But I stumbled upon the swapcontext in the signal handler. I thought it isn't a good idea to use it in the signal handling method. That's why it is also hard to say, what happens after swapping back into the signal handler? Is anyone able to explain this code snippet?
Concerning my concrete problem I found out that the segmentation fault occurs while calling swapcontext() in the while-loop of scheduler_function(). I also tried to run the program without using an alternate stack with no effects to the behaviour.
Another interesting fact is, that the segmenation fault does not occur, when I set the size of the stacks, which will be used in the contexts, to 65536. But than, the counting does not work in every case, too.
Last edited by XComp; 12-02-2008 at 07:29 PM..
Reason: additional information
hi friends i have a problem in signal handling ...
let me explain my problem clearly..
i have four process ..
main process forks two child process and each child process again forks another new process respectively...
the problem is whenever i kill the child process it is reforking and the... (2 Replies)
i wrote handler for sigsegv such that i can allocate memory for a variable to which
sigsegv generated for illlegal acces of memory.
my code is
#include <signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *j;
void segv_handler(int dummy)
{
j=(char *)malloc(10);
... (4 Replies)
I am trying to write a small program where I can send signals and then ask for an action to be triggered if that signal is received. For example, here is an example where I am trying to write a programme that will say you pressed ctrl*c when someone presses ctrl+c. My questions are what you would... (1 Reply)
Hello all,
I am starting to learn signal handling in Linux and have been trying out some simple codes to deal with SIGALRM. The code shown below sets a timer to count down. When the timer is finished a SIGALRM is produced. The handler for the signal just increments a variable called count. This... (7 Replies)
#1: does anyone know how to detect how many times (and/or the time length) a given thread has been context switched out of the CPU?
#2: are there any tchniques that minimize/eliminate your thread getting context switched?
I would be happy to know the answers to these questions for ANY... (2 Replies)
Guys,
I'm doing signal handling in Perl. I'm trying to catch ^C signal inside the script.
There two scripts : one shell script and one perl script.
The shell script calls the perl script.
For e.g. shell script a.sh and perl scipt sig.pl.
Shell script a.sh looks something like this :... (6 Replies)
Hi folks
I'm trying to write a signal handler (in c on HPUX) that will catch the child process launched by execl when it's finished so that I can check a compliance file.
The signal handler appears to catch the child process terminating however when the signal handler completes the parent... (3 Replies)
HI,
I need to handle SIGUSR2 signal in my application to change the state of the application dynamically. I have implemented the signal handler. However the application is able to catch only one SIGUSR2 signal. The second SIGUSR2 signal causes the application to crash. This is happning only with... (3 Replies)