Stack Pointers...

Status
You're currently viewing only Ajar's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Ajar

Ars Tribunus Angusticlavius
9,702
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?
 

Ajar

Ars Tribunus Angusticlavius
9,702
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,702
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?
 

Ajar

Ars Tribunus Angusticlavius
9,702
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
You're currently viewing only Ajar's posts. Click here to go back to viewing the entire thread.
Not open for further replies.