Difference between revisions of "Prove scritte 2020"

From Sistemi Operativi
Jump to navigation Jump to search
 
(4 intermediate revisions by one other user not shown)
Line 109: Line 109:
  
 
== esercizio c2 ==
 
== esercizio c2 ==
 +
 +
 +
=== Soluzione proposta 1 ===
 +
 +
[[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">
 +
 +
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;
 +
}
 +
 +
</syntaxhighlight>
 +
 +
= Prova 2020/02/20 =
 +
 +
 +
== esercizio c2 ==
 +
 +
Sol : x alla seconda

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