Difference between revisions of "Prove scritte 2020"

From Sistemi Operativi
Jump to navigation Jump to search
 
Line 111: Line 111:
  
  
=== Soluzione proposta 1 (da controllare)===
+
=== Soluzione proposta 1 ===
  
 
[[User:Gio|Gio]] ([[User talk:Gio|talk]]) 14:43, 16 January 2023 (CET)
 
[[User:Gio|Gio]] ([[User talk:Gio|talk]]) 14:43, 16 January 2023 (CET)
 +
* Modificato [[User:Flecart|Flecart]] ([[User talk:Flecart|talk]]) 15:36, 16 January 2023 (CET)
  
 
<syntaxhighlight lang="C">
 
<syntaxhighlight lang="C">
  
 
void multisend(pid_t destination,T msg,int times){
 
void multisend(pid_t destination,T msg,int times){
   for(int i=0;i<n;i++){
+
   for(int i=0; i<n; i++) {
     asend(<n-i,getpid(),msg>,destination);
+
     asend(<n-i, getpid(), msg>, destination);
     ack=arecv(destination)
+
     ack = arecv(destination);
 
   }
 
   }
 
}
 
}
T multirecv(pid_t sender){
+
 
   int i;
+
T multirecv(pid_t sender_first) {
  if(sender==ANY){
+
   <i, sender, msg> = arecv(sender_first);
    //assegna il pid preso da a recv a sender
+
   asend(ack, sender);
    <i, sender, msg>=arecv(ANY);
+
    
  }else{
+
   while(i > 0) {
    <i, sender, msg>=arecv(sender);
+
     <j, _, msg>=arecv(sender);
  }
+
     asend(ack, sender);
   asend(ack,sender);
 
   i--;
 
   while(i>0)
 
     <j, sender, msg>=arecv(sender);
 
     asend(ack,sender);
 
 
     i--;
 
     i--;
 
     assert(i==j);
 
     assert(i==j);
Line 141: Line 137:
 
   return msg;
 
   return msg;
 
}
 
}
 +
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Latest revision as of 15:36, 16 January 2023

Prova 2020/01/15

esercizio c1

Soluzione proposta 1

class TS{

  int v=0;
  queue<(unsigned int,cond)> pq;
  usinged int timer;


  void V(){
    if(pq.empty()){
      v++;
    }else{
      pq.pop().signal();
    }
  }
  bool P(unsigned int timeout){
    if(v == 0){
      cond c = new condition();
      pq.push((timeout+timer,));
      c.wait();
      free(c);
      if timer== timeout+timer{
        return true;
      }
    }else{
      v--;
    }
    return false;
  }

  void tick(){
    timer++;
    for [i,c] in pq.iter(){
      if( i <= timer) {
        c.signal();
        // la nostra stuttura dati supporta la rimozione durante lo scorrimento
        // per esempio può essere una lista
        pq.remove(c);
      }
    }
  }
}


Soluzione proposta 2

int value;
boolean ret_val;
int time;
min_heap<int, condition> heap;  // ordinato sul tempo minore.
vector<condition> queue; // vector utilizzato come queue, con rimozione anche nel mezzo.


monitor {
	value = 0;
	time = 0;
}

void V(void) {
	if (heap.size() > 0) {
		condition c = queue.pop_back(); // remove and return
		heap.remove(c);  // rimuovi l'unico elemento con c.
		ret_val = true;
		c.signal();
	} else {
		value++;
	}
}


boolean P(unsigned int timeout) {	
	if (value > 0) {
		value--;
		return true;
	} else {
		int next_time = timeout + time;
		condition c = new condition();
		heap.insert(<next_time, c>);
		queue.push_back(c);
		c.wait();
		free(c);
		
		return ret_val;	
	}	
}


void tick(void) {
	ret_val = false;
	time++;
	while (heap.size() > 0 && heap.top().0 <= time) {
		condition c = heap.top().1;
		heap.deleteTop();
		queue.remove(c);
		c.signal();
	}
}

esercizio c2

Soluzione proposta 1

Gio (talk) 14:43, 16 January 2023 (CET)

void multisend(pid_t destination,T msg,int times){
  for(int i=0; i<n; i++) {
    asend(<n-i, getpid(), msg>, destination);
    ack = arecv(destination);
  }
}

T multirecv(pid_t sender_first) {
  <i, sender, msg> = arecv(sender_first);
  asend(ack, sender);
  
  while(i > 0) {
    <j, _, msg>=arecv(sender);
    asend(ack, sender);
    i--;
    assert(i==j);
  }
  return msg;
}

Prova 2020/02/20

esercizio c2

Sol : x alla seconda