LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2004, 05:13 PM   #1
utumno
LQ Newbie
 
Registered: Jun 2004
Distribution: Debian
Posts: 6

Rep: Reputation: 0
functions from ucontext.h


Hello Linux gurus,

I am writing a program that is supposed to be an emulation of a Unix shell. It has its own concept of a process and its own scheduler. I use setcontext() and swapcontext() to swap my processes.

In order for the processes to be swapped periodically, I have arranged SIGALRM to be delivered to my program every 10 ms ( isn't that too short amount of time? ) , and the scheduler is the signal handler which messes up with process queues and finally calls swapcontext to swap the old context out and run a new one. In order for the scheduler not to be interrupted by itself, I mask SIGALRM during its execution.

This setup basically works, sometimes I am able to spawn as much as a million processes.
The problem is that sometimes - very rarely - I experence a segfault. I've been trying to isolate the problem for past 2 weeks now. Finally I decided to RTFM. Here is what I read in /usr/doc/glibc-doc/html/chapters_23.html

"It is not allowed to do the context switching from the signal handler directly since neither setcontext nor swapcontext are functions which can be called from a signal handler."

Damn! The question: how in the world am I supposed to write my scheduler then? I know I could use sigsetjmp() and siglongjmp(), but I'd very much rather stick to ucontext.h functions.

Could anyone give me a hint?

Leszek Koltunski

You can take a look at the code at www dot 3miasto dot net/~leszek/progs/cam/camel.html
 
Old 06-23-2004, 06:12 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
What if all that your SIGALRM handler did was set a global variable called something like alarm_signaled to 1? Then your shell could poll that variable and when it discovers it's set to 1 then it knows SIGALRM has been raised. Then it can do its scheduling tasks and reset alarm_signaled to 0.
 
Old 06-23-2004, 06:54 PM   #3
utumno
LQ Newbie
 
Registered: Jun 2004
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
Alas, that will not do, since my program is a bit more than a shell emulation. Actually I strive to write a simple kernel.

So, it should be possible to swap out any process that is spawned by my progam ( again, 'process' is not a Linux process but my own notion of it ) in a way transparent to it. The code for userspace processes - like my implementations of shell commands 'echo' , 'ls' and so on, shouldn't know when and if it is interrupted.

Thus, it is not possible to just set a global var in my signal handler and then check it in the code, since I'd have to check it in the userspace code too, which is kinda against design of the program...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
g++ functions apreda07 Programming 4 07-22-2004 12:40 AM
overload functions in C? jpbarto Programming 14 05-20-2004 02:26 PM
help with functions starla827 Programming 1 04-19-2004 02:57 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration