What about I/O?

ready queue -> cpu
I/O <- I/O queue <- I/O request time slice expired, fork a child, wait for an interrupt Scheduler Responsibility - maintaining the ready queue - decision on when to context switch P1(web server) P2(Database) mm, cpu Inter Process communication IPC mechanisms -transfer data/info between address spaces -maintain protection and isolation Threads to the rescue! threads -> multithreaded process -> core1, core2, core3 …

parallelization => speed up
specialization => hot cache!
efficiency => lower mm requirement & cheaper IPC

Are threads useful on a signle CPU?
or when (#of Threads) > (# of CPUs)?
t_ctx_switch -> t_idle -> t_ctx_switch

A process

State of execution
-program counter, stack
Parts & temporary holding area
May require special hardware
-I/O devices

What is a process?
OS manages hardware on behalf of applications
application == program of disk, flash memory(static entity)

process == state of a program when executing loaded in memory(active entity)

What does a process look like?
stack, heap, data, text

address space == “in memory”
representation of a process
physical addresses: locations in physical memory

How does the OS know what a process is doing?
-program counter
-cpu registers
-stack pointer

Process Control Block(PCB)
registers
memory limits
list of open files
priority
signal mask
CPU scheduling info

-PCB created when process is created
-Certain fields are updated when process state changes
-Other fields change too frequently

CPU process
running, ready state

mechanisms for process creation
-fork == copies the parent PCB into new child PCB
child continues execution at instruction after fork
-exec == replace child image, load new program and start from first instr.

unix-based os, the parent of all process: init
the parent of all app processes: zygote

OS must
-preempt
-schedule
-dispatch

System call

User process, kernel

to make a system call an application must
– write argoments
– save relevant data at well-defined location
– make system call

User/Kernel Transitions
・hardware supported
e.g. traps on illegal instructions or memory accesses requiring special privilege
・involves a number of insturctions
e.g. -50-100ns on a 2GHz machine running Linux
・switches locality
-> affects hardware cache

・process management
・file management
・device management

kill
setgid
mount
sysctl

monolithic OS
+everthing included, inlining, compile time optimizations
-customization, portability, manageability, momory footprint, performance

module OS
-operating system, module(Interface)

Microkernel
operating system, address space, threads

Linux architecture
Hardware(cpu, memory, disks, terminal), Linux operating system(process management, memory management, the file system;I/O, etc), standard library(open, close, read, write, fork, etc), standards utility programs(shell, editor, compilers, etc), users

Abstraction or Arbitration

arbitration(R): distributing memory between multiple processes
abstruction(B): supporting different types of supeakers, interchangeable access of hard disk or SSD

Desktop, Embedded
microsoft windows, unix-based(mac os x(BSD), linux)
android, ios, Symbian

Abstractions
-process, thread, file, socket, memory page
Mechanism
-create, schedule

OS Element: memory management example
Abstractions: memory page
mechanism: allocate, map to a process
Policies: least recently used -LRU

Design princples
separation of mechanism 2 policy
-implement flexible mechanisms to support many polices
-e.g, LRU, LFU, random
Optimize for common case
– where will the os be used?
– what will the user want to execute on that machine?
– what are the workload requirements?

OS Protection Boundary
user/kernel protection boundary
privileged mode, kernel-level

user-kernel switch is supported by hardware
-trap instructions
-system call
open(file), send(socket), malloc(memory)
-signals

Operating System

What are operating systems?
Why are operating systems needed?
How are operating systems designed and implemented?

OS abstractions, mechanisms, and policies for:
-processes and process management
-threads and concurrency
-resource management: scheduling, memory management
-OS services for communication and I/O
-systems software for data center and cloud environments

Theory + Practice
sequence of programming projects
– threads, concurrency, and synchronization
– single-node Os mechanisms
inter-process communication, scheduling …
multi-node os mechanisms
– remote procedure calls(RPC),…
experimented design and evaluation
-> programming in C in Linux

Simple OS definition
– a special piece of software that…
abstract and arbitrates
the use of a computer system.

Direct operational resources
-control use of CPU, memory, peripheral devices…
Enforce working policies
Mitigate difficulty of complex tasks

Operating System(send/recive, socket, network, read/write file, storage)
CPU, MainMemory, Ethernet/Wifi Card, GPU, disk, usb
-hide hardware complexity
-resource management
-provide isolation < protection file system, device driver, scheduler

Working from the Command Line

Concepts to know
– read man page(man)
– navigate directories(cd, ls, pwd)
– move and copy files(mv, cp)
– adjust permissions or groups(chown, chmod)
– run executables(gcc, Cexecutables, or other tools)

# navigate to home dir
$ cd ~

# create main.c file
$ touch main.c

# edit main.c (write program)
$ nano main.c

# compile (and link) program
$ gcc main.c -o helloWorld

# run program
$ ./helloWorld

Makefiles concepts
– Targets and dependencies
– Comments(always useful)
– Variables(compiler, flags, etc.)
– Calling make from the command line

# specify the compiler
CC=gcc

# specify options for the compiler
CFLAGS=-c -Wall

all: hello

hello: main.o hello.o
	$(CC) main.o hello.o -o hello

main.o: main.cpp
	$(CC) $(CFLAGS) main.cpp

hello.o: hello.cpp
	$(CC) $(CFLAGS) hello.cpp

clean:
	rm -rf *o hello

UNIX Time or POSIX Time

#include <stdio.h>
#include <time.h>
int main()
{
	time_t sec;
	sec = time(NULL);

	printf("Number of hours since January 1, 1970 is %dId /n", sec/3600);
	return 0;
}

The following example shows the usage of rand() function.

#include 
#include 

int main()
{
	int i, n;
	time_t t;

	n = 5;

	/* Initializes random number generator */
	srand((unsigned) time(&t));

	/* Print 5 random numbers from 0 to 49 */
	for( i = 0; i < n; i++)
	{
		printf("%d\n", rand() % 50);
	}
	return(0);
}

The most important string functions are as follows:
strlen() Get length of a string.
strcat() Link together (concatenate) two strings.
strcmp() Compare two strings.
strchr() Find character in string.
strstr() Find string in string.
strlwr() Convert string to lowercase.
strupr() Convert string to uppercase.

#include <stdio.h>
#include <stdlib.h>

int main()
{
	char *t = "XXX";
	printf( "Length of <%s> is %d.\n", t, strlen( t ));
}

C programming

Concepts to know
-Structs, arrays, pointers, and reference types
-File I/O
-Use of command line parameters
-Pass-by-reference and pass-by-value
-Dynamic memory allocation using malloc()
-Use of C libraries

Helpful Skills related C
-Debugging programs
-Reading documentation
-Iterative design
-Good coding standards

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct test_struct
{
	int val;
	struct test_struct *next;
}

struct test_struct *head = NULL;
struct test_struct *curr = NULL;

struct test_struct* create_list(int val)
{
	printf("\n creating list with headnode as [%d]\n", val);
	struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
	if(NULL == ptr)
	{
		printf("\n Node creation failed \n");
		return NULL;
	}
	ptr->val = val;
	ptr->next = NULL;

	head = curr = ptr;
	return ptr;
}

struct test_struct* add_to_list(int val, bool add_to_end)
{
	if(NULL == head)
	{
		return (create_list(val));
	}

	if(add_to_end)
		printf("\n Adding node to end of list with value [%d]\n",val);
	else
		printf("\n Adding node to beginning of list with value [%d]\n", val);

	struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
	if(NULL == ptr)
	{
		printf("\n Node creation failed \n");
		return NULL;
	}
	ptr->val = val;
	ptr->next = NULL;

	if(add_to_end)
	{
		curr->next = ptr;
		curr = ptr;
	}
	else
	{
		ptr->next = head;
		head = ptr;
	}
	return ptr;
}

struct test_struct* search_in_list(int val, struct test_struct **prev)
{
	struct test_struct *ptr = head;
	struct test_struct *tmp = NULL;
	bool found = false;

	printf("\n Searching the list for value [%d] \n", val);

	while(ptr !=NULL)
	{
		if(ptr->val == val)
		{
			found = true;
			break;
		}
		else
		{
			tmp = ptr;
			ptr = ptr->next;
		}	
	}

	if(true == found)
	{
		if(prev)
			*prev = tmp;
		return ptr;
	}
	else
	{
		return NULL;
	}
}

int delete_from_list(int val)
{
	struct test_struct *prev = NULL;
	struct test_struct *del = NULL;

	printf("\n Deleting value [%d] from list\n",val);

	del = search_in_list(val,&prev);
	if(del == NULL)
	{
		return -1;
	}
	else
	{
		if(prev != NULL)
			prev->next = del->next;

		if(del == curr)
		{
			curr = prev;
		}
		else if(del == head)
		{
			head = del->next;
		}
	}
	free(del);
	del = NULL;

	return 0;
}	
void print_list(void)
{
	struct test_struct *ptr = head;

	printf("\n ----Printing list Start---- \n");
	while(ptr != NULL)
	{
		printf("\n [%d] \n",ptr->val);
		ptr = ptr->next;
	}
	printf("\n -------Printing list End ------ \n");
	return;
}

int main(void)
{
	int i = 0, ret = 0;
	struct test_struct *ptr = NULL;

	print_list();

	for(i = 5; i<10; i++)
			add_to_list(i,true);

	print_list();
	for(i =4; i>0; i--)
		add_to_list(i,false);

	print_list();

	for(i = 1; i<10; i += 4)
	{
		ptr = search_in_list(i, NULL);
		if(NULL == ptr)
		{
			printf("\n Search &#91;val = %d&#93; failed, no such element found\n",i);
		}
		else
		{
			printf("\n Search passed &#91;val = %d&#93;\n", ptr->val);
		}

		print_list();
		ret = delete_from_list(i);
		if(ret != 0)
		{
			printf("\n delete [val = %d] failed, no such element found\n",i);
		}
		else
		{
			printf("\n delete [val = %d] passed \n", i);
		}
		print_list();
	}
	return 0;
}

LEAP/SLERP

Vector3.Lerp Documentation:
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Quaternion.SLERP Documentation:
https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html

Methods and Debugging

Debugging

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	public void GoToScene(){
		Debug.log("My method was called!")	
	}
}

Scene Changing

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class NewBehaviourScript : MonoBehaviour {
	public void GoToScene(){
		Debug.log("My method was called!");
		SceneManager.LoadScene("00-FallingCoconut");
	}
}

The Variables

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class NewBehaviourScript : MonoBehaviour {
	public void GoToScene(string sceneName){
		Debug.log("My method was called!");
		SceneManager.LoadScene("sceneName");
	}
}