help!

cout << __func__ << " end is now: "<< temp -> pid << endl;
why is temp -> pid in my device_start function stopping the program

delete code
Last edited on
First fix the constructor for your device_queue class:
1
2
3
4
device_queue :: device_queue():ready_queue(){
	   begin = NULL;
	   end = NULL;
}


Next fix your move_top function and observe what it is doing:
1
2
3
4
5
6
7
8
9
PCB* ready_queue :: move_top() {
    if (head != NULL){
        PCB*temp = head;
        head = head->next;
        temp->next = NULL;
	return temp;	 
    }
    return NULL;
}


Code in bold is added by me. Can you now see why temp -> pid causes a segmentation fault?
Line 53: temp is an invalid point here. Accessing temp->pid is an illegal operation.
device_queue::device_start() is completely wrong. Do it over.
helios, how is my device_start() function completely wrong. Please explain! I'm so confused! So I'm trying to delete end (a.k.a head) of the device queue.

And the head of my device queue is the temp that is returned in move_top() function.
Last edited on
I still don't see why it's creating segmentation fault.
What does ready_queue::move_top() return if head is NULL?
Last edited on
In the ready-queue::move_top() function the head is never supposed to be NULL because there will be nothing to be moved then?
if head is NULL, then tail will be temp?
In the ready-queue::move_top() function the head is never supposed to be NULL because there will be nothing to be moved then?

Still, it 's a really bad idea to have a path through your code that can return garbage to the caller. Add return NULL; before line 64 and print the value returned at line 115. I think you'll find that temp is NULL.
Also, you need to update tail at lines 49 & 60 if the list becomes empty.
ok so if my move-top() function is like this;
delete code

and if my device_start function is like this:
delete code

I still get segmentation fault.
Last edited on
ohhh so if I just cout<<temp; in line 5 of my device_start() function it prints 0.
this is my move_top function
1
2
3
4
5
6
7
8
9
10
11
PCB* ready_queue :: move_top(){
	    if (head != NULL){
	 	   PCB*temp = head;
                  head = head->next;
                  temp->next = NULL;
                  cout << __func__ << " temp.pid is now: "<< temp -> pid << endl;
		  return temp;	 
            }
    cout << __func__ << " returns NULL." << endl;
    return NULL;
}


this is my device_start function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void device_queue :: device_start(){
   PCB* temp = move_top();
    if (temp != NULL){
	  if (end == NULL){
		 end = temp;
          }
          else{
         	begin->next=temp;
          }
	        begin=temp;
     }
    else{
	   	  cout << "There are no devices";
    }
}



The device_start function should be going to this:
cout << __func__ << " temp.pid is now: "<< temp -> pid << endl;
but it instead goes to: cout << __func__ << " returns NULL." << endl;
Last edited on
So you missed the point that Smac89 was making in the second post. By the way, I missed reading it when I made my comment in the sixth post. Then a version of the same point is made by dhayden made in the ninth post.

The device_start function should be going to this:
cout << __func__ << " temp.pid is now: "<< temp -> pid << endl;
but it instead goes to: cout << __func__ << " returns NULL." << endl;
Now you have proved to yourself that ready_queue :: move_top() is being called when head==NULL.

So are you seeing "There are no devices"?

Please select an indentation style and use it consistently. If you want help from others, make your code readable.
Last edited on
in my void device_queue :: device_arrival(PCB* temp)
move_top() is not being called when head==NULL?

yes, I am seeing "There are no devices"
Last edited on
As shown in the original post of this thread, In your void device_queue :: device_arrival(PCB* temp), move_top is not being called at all.
So now the problem is:
say I enter c1: So, process with PID 1 enters device.
say I enter c1: So, process with PID 2 enters device.
say I enter C1: So, done with process with PID 2 in device.

but should be done with process with PID 1 in device, and then done with PID 2 in device, not just last one. How to fix this?

ok so this is in my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
case 'c':
	     cin >>num;
             if (num > c || num < 1){
     	       cout << "this device doesn't exist" <<endl<<endl;
             } 
            else
               cdrws[num].device_start(rq.move_top());
	       cdrws[num].device_arrival();
               break;
               
case 'C':
              	cdrws[num].device_completion();
              	break;


this is my header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void device_queue :: device_start (PCB *temp){
    if (temp != NULL){
		 end = temp;
    }
}

void device_queue :: device_arrival(){
	string filename;
        char readorwrite;
	int location, filesize;
	if (end == NULL){
	   		cout << "Cannot make system call to device- CPU, and therfore Ready Queue, are empty!"<<endl<<endl;
	   }
	else if ( end != NULL){
		        cout <<"File Name: ";
                        cin >> filename; 
                        cout << "Starting location in memory: "; 
                        cin >> location;
                       cout <<"Read or write <r or w ONLY>: ";
                       cin >> readorwrite; 
                       while (readorwrite != 'r' && readorwrite != 'w'){
        	           cout << "only r or w please!" << endl;
                       }
		       if (readorwrite == 'w'){
			   cout <<"File Size: ";
			   cin >> filesize;	    	  
		       } 
			cout << "Process has entered device" << endl << endl;
	   }
	   
}

void device_queue :: device_completion(){
       if (end == NULL){
          cout<<"Cannot terminate, and therefore Device Queue are empty!"<<endl<<endl;     
	   }
       else if (end != NULL){
	    PCB *temp = end;
            end = end->next;
            temp -> next = end;
            delete end;
            cout <<"Done with process with PID " << temp -> pid << " in device!"<<endl;
	}
} 
Last edited on
Topic archived. No new replies allowed.