Open In App

Special Machine Instructions in Operating System

Machine instructions are machine code programs or commands. In other words, commands written in the machine code of a computer that it can recognize and subsequently execute.

Machine code or machine language refers to a computer programming language consisting of a string of ones and zeros, i.e., binary code. Computers can respond to machine code directly, i.e., without any direction or conversion.



Special Machine Instructions :

The following techniques are used for hardware to the critical solution problem :



1. Test and Set Instruction :

Set a memory location to 1 and return the previous value of the location. If a return value is 1 that means lock acquired by someone else. If the return value is 0 then lock is free and it will set to 1. Test and modify the content of a word automatically

int test_and_set (int &target) {
int tmp ;
tmp = target ;
target =1 ;     /* True */
return ( tmp )
}

Implementing mutual exclusion with test and set :

external bool lock (false) ;
do {
while ( test_and_set (lock)) ;      /* Wait for Lock  */
critical_section ( )  ;          
Lock =false ;                 /* release the Lock  */
remainder_section ( ) ;
} while (1) ;

2. Swap Instruction :

void Swap ( boolean &a , boolean &b ) {
boolean temp = a ;
a= b ;
b = temp ;
}

Implementation :

Shared Data :  boolean lock = false ;
Process Pi :
do {
key = true ;
while ( key == true ) 
        Swap ( lock , key ) ;
< critical section >
lock = false ;
< remainder section >
}

3. Compare and Swap Instructions :

Compare the value of a memory location with an old value that is passed to the instruction. If the two values match then write the new value in the memory location. With this instruction, we set a memory location to a specific value provided that did not charge the contents since we read them. If they did, then our new value will not be set.

int compare_and_swap (int *x , int old , int new ) {
int save = *x ;           /* current contents of the memory */
if (save == old)     *x = new ;
return save ;          /* tell the caller what was read */
}

Implementation :

while ( compare_and_swap ( &locked , 0 , 1 ) != 0 ) ;    /* spin until locked == 0  */
 /* if we got here lock got set to 1 and we have it  */
<Critical Section>
locked = 0 ;      /* release the lock  */
<remainder section>

4. Fetch and Increment Instruction :

This instruction increments the memory location and returns the previous value of the location. We grab a ticket (fetch-and-increment) and wait until its our turn. When done we increment turn so that the thread that grabbed the next ticket gets to go.

int fetch_and_increment ( int *x ) {
last_value = *x ;
*x = *x +1 ;
return last_value ;
}

the return value corresponds to number of our ticket while the machine is now ready to generate the next higher number. 

Implementation :

ticket = 0 ;     turn = 0  ;
. . .
myturn = fetch_and_increment ( & ticket ) ;
while ( turn !=myturn )    /* Busy wait  */
<Criticcal Section>
fetch_and_increment ( &turn ) ; 

Variable ticket represents ticket number in machine and variable myturn represents our ticket number, turn represents the number currently being served. Each time somebody takes a ticket via fetch_and_increment the value of ticket is incremented. Each time somebody is done with their critical section and is ready for the next thread to serve, they increase the turn.

Article Tags :