Process Sync


smart person learn knowledge by network data structure but gard to use and administratenormal person learn knowldedge by tree data structure, so cannot imagine creative thinking which connect far distance in tree but close distance in network
**so i need to make network document site myself**
> critical section: where racing can be ocuurred

## issues
Mutual Exclusion (상호배제): no two process execution in critical section
Progress: no indefinite postpone while selection of process enter to critical section
Bounded Waiting: no starvation

# Solution


### 1. Locks
- spinlocks
- disabling inturrupts
> for two-process case code


### Spinlock (software-only)
1. turn onlyping-pong like one-to-one problem
2. flag onlysymultaniously raising flag problem
3. **Peterson's algorithm**use flag and turn variable
> ```typerepeat 	flag[i] := true; 	turn := j; 	while (flag[j] and turn = j) //cation to and 		//busy waiting = spinning 	critical section 	flag[i] := false; 	//else code until false;```

### spinlock (hardware atomic)
by hardware support atomic operation
> atomic operation: cannot be disturbed


### disabling interrupts
prevent context switches
**acquire()/lock()**: before critical section**release()/unlock()**: after critical section

# Differences


> ### Semaphore
> Semaphore prevent multi-process access to data which are File type integer counter 
> Semaphore - unlock by other
> supervise many resources
> ![](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fde77dfc5-3c2a-4128-989c-7443486332d9%2Ftemp.jpg?table=block&id=71108d45-8e2a-4fe9-87dc-47519b809e9b&cache=v2)
> 

> ### Mutex
> Mutex(mutual exclusion) is object(variable) in process that prevent multi-thread access to data - use locking and unlocking
> Mutex - self unlock
> mutex is for two process - semaphore is for multi process use critical section
> supervise only one resource
> ![](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F9ed7cd04-daba-4d0a-9a45-b75b72d1e370%2Ftemp.jpg?table=block&id=04dc4d93-4edb-40a4-bab1-868919034830&cache=v2)

> All Mutex is Semaphore, Semaphore can be mutex, not all mutexes are Semaphore





