Stack Pointers...

Status
Not open for further replies.

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
I'm not having any trouble setting the jump (with jmp_buf), but whenever I try to longjmp back, I get segmentation errors...

Here are the relevant code fragments:

code:<HR>
Code:
int main(){<P>jmp_buf Env1;<P>//create first child with a fork
if(CurrentID = fork() == 0){
	
	//child1 saves its state and then yields
	setjmp(Env1);
	pause();<P>	cout << "Child1: I'm back! Entering loop...\n";<P>	//Child1 process loop
	while(1){
        cout << "Hi.\n";
		yield();
        }
    }<P>
//parent loop                
while(1){
    
	sleep(2);<P>	longjmp(Env1, SIGWAKE);<P>	//our work here is done
	break;
    }<P>//exit stage left
return 0;
}

...the setjmp works fine, but when the parent executes the longjmp statement, I get the aforementioned segmentation fault.

(As an aside, I can't figure out the if condition for a conditional longjmp... but I'd like to get the basic one working first...)

Any suggestions?
 

-Triple-

Ars Tribunus Angusticlavius
8,554
I'm rusty on my inline assembler, but isn't the external stack pointer stored in the esp register? That would make sense ;) Take a look at the following code (and remember I may be wrong):

code:<HR>
Code:
#include <iostream>
using namespace std;<P>int main()
{
    int x;
    __asm mov x,esp  ;move value stored in esp into x
    cout << "Stack pointer: " << x;
    return 0;
}
 

-Triple-

Ars Tribunus Angusticlavius
8,554
Gack! if you're running on Solaris, the only coding I've done on a SPARC box is Java... and I've never ever heard of a standard method of accessing the stack pointer (besides, of course, the *jmp functions). You probably have to use Solaris-specific assembly outside the program.

Edit: more.

[This message was edited by -Triple- on December 01, 2001 at 19:06.]
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
Yeah, I figured that out yesterday... my theory is that we're supposed to store the jump buffers in a shared memory region with shmget() and related functions... but I gave up on fork() and switched to threads. The professor never mentioned threads in class, but he discussed fork and jumps at some length; whenever anyone asked him about context switching and child processes it was always discussed in the context of fork/jmp. However, he noted that we could do the implementation as we saw fit and, if it worked, he wouldn't dock us.

Ultimately, the assignment is to write a small realtime executive that runs three tasks that we also have to program and takes a small amount of user input, but while the rest of my group works on the data structures and functions, I'm learning UNIX programming and trying to create multiple processes or threads and have them communicate and switch between them (it doesn't need to be preemptive).

I finally gave up last night, though, and bit the bullet and learned the basics of the pthread library. It was a million times easier than trying to beat fork(), longjmp(), set_sp(), and shmget() into submission all at once... I'm trying to tweak things now so I can control switching between threads. Inter-thread communication seems to be working well, though I'm not sure how to prevent, for example, a thread printing some output in the middle of the parent's output. For example, if the parent has

pthread_cancel(Thread's ID);
cout << "Parent";

in its code, and a thread has

cout << "Thread";

sometimes, because I don't have much control over thread scheduling, it prints

ParThreadent

Which really isn't any good. I don't think it will be a major problem, but if I can do something about it I'd like to :)
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
While we're on the subject of this assignment...

I can't use conio functionality under Solaris, which means that I can't use functions like kbhit() and getche()... is there any way for me to get this kind of functionality when it comes time to integrate the basic CLI my other group members have written with my Solaris-friendly code?
 

panto

Wise, Aged Ars Veteran
115
Well by using pthreads you already have the threads.

I'm afraid that the prof wants you to do it
within the context of only one process and
use longjmp/setjmp to switch around the tasks.
It is doable if you do cooperative multitasking.
Do a search about "cthreads", they work in the same way.

Regarding the getch() function and other stuff, what
you want to use is ncurses. Try man ncurses.

And another thing about the mangled output.
You will only get this problem when you're using
pthreads; you must protect the output functions you use with a mutex.
In a C++ context I think that you must overload the << operator
on streams to do that (but it will be slow). Not worth the bother IMO.
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
Yeah, the prof was looking for something non-preemptive (i.e. cooperative). That's precisely it. But he claims that he won't mind if we do "more," which is why after many hours of frustration I decided to make the thread switch.

man ncurses in the Solaris console returns a "no entry," but I'll see if I can link to it.

I was afraid you'd say semaphores :) yet another thing I'll have to learn, I suppose. :p
 
Status
Not open for further replies.