Operating System

OS:

graph LR;
A[User]
B[Applications<br>Chrome<br>Word Pocessor]
C[Operating System<br>Mac<br>Linux<br>Windows]
D[Hardware<br>CPU<br>Memory<br>HardDisk]

Systems without OS

Services

graph TD;
subgraph Based on Functionalities
end
A[OS]-->B[Single Tasking]
A-->C[Multi Tasking / Multi Programming]
A-->D[Multi Threading]
A-->E[Multi Processing]
graph TD;
subgraph Based on Functionalities
end
A[OS]-->B[Single Tasking<br>Only one task at a time in CPU<br> ex: MS-DOC<br>Only One Process other than CPU <br> can exist a time<br>Very Inefficient Systems]
A-->C[Multi Tasking / Multi Programming]
A-->D[Multi Threading]
A-->E[Multi Processing]

Single Tasking

graph LR;
A[Single Tasking]-->B[CPU]
C[Computation]
D[I/O]
E[Computation]
subgraph Functioning
F[Computation]-->G[I/O]
G-->H[Computation]
X[First Computation then I/O<br> during I/O CPU will wait for <br>I/O completion after I/O Completion<br>Another Computation will start by CPU<br> So this is the disadvantage<br>Our Costly CPU is Waiting for I/O]
end

Batch Processing

graph LR;
A[User 1]-->B[Job 1]
C[User 2]-->D[Job 2]
E[User 3]-->F[Job 3]
G[User 4]-->H[Job 4]
I[User 5]-->J[Job 5]
K[User 6]-->L[Job 6]
M[User 7]-->N[Job 7]
O[User 8]-->P[Job 8]
Q(OPERATOR)
B-->Q
D-->Q
F-->Q
H-->Q
J-->Q
L-->Q
N-->Q
P-->Q
Q-->R[BATCH 1<br/>Job 1,Job 2,Job 3,Job 4]
R-->S[BATCH 2<br/>Job 5,Job 6,Job 7,Job 8]
S-->T[CPU<br/>I/0]

Multi Programming

graph LR;
A[Multi Programming]-->B[CPU]
C[Computation]
D[I/O]
E[Computation]
subgraph Functioning
F[Computation]-->G[I/O]
G-->H[Computation]
X[During  Computation I/O <br>will be unassigned from CPU<br>and Another Computation sent to CPU so<br> here processes are running concurrently.<br>Also multiple processes in memory is there]
end

Multi Tasking

graph LR;
A[Multi Tasking]-->B[CPU]
C[Computation]
D[I/O]
E[Computation]
subgraph Functioning
F[Computation]-->G[I/O]
G-->H[Computation]
X[During  Computation I/O <br>will be unassigned from CPU<br>and Another Computation sent to CPU so<br> here processes are running concurrently.<br>Also multiple processes in memory is there]
end
AA[Here only we run the<br>Processes in time slot manner<br> More Responsiveness]

Multi Threading

graph BT;
A[Program]-->B[Process]
A-->C[Process]
A-->D[Process]
A-->E[Process]
A-->F[Process]
A-->G[Process]
A-->H[Process]
B-->BA[Thread]
B-->BB[Thread]
B-->BC[Thread]
B-->BD[Thread]
B-->BE[Thread]
B-->BF[Thread]
B-->BG[Thread]
B-->BH[Thread]
C-->CA[Thread]
C-->CB[Thread]
C-->CC[Thread]
C-->CD[Thread]
C-->CE[Thread]
C-->CF[Thread]
C-->CG[Thread]
C-->CH[Thread]
D-->DA[Thread]
D-->DB[Thread]
D-->DC[Thread]
graph TB;
A[Program]-->B[Process]
A-->C[Process]
A-->D[Process]
A-->E[Process]
A-->F[Process]
A-->G[Process]
A-->H[Process]
graph TB;
A[Process]-->B[Thread]
A-->C[Thread]
A-->D[Thread]
A-->E[Thread]
A-->F[Thread]
A-->G[Thread]
A-->H[Thread]

POSIX Threads

graph TD;
A[Thread T1]
B[Fork Point]
A-->B
B--Instruction 1<br> Print T1----->C['']
B--Thread T2 Created-->E[Instruction 2<br> Print T2]
E-->D[Thread T2]
subgraph Execution Details
X[Here it is<br> undeterminable which will <br>executed first, in 99% T1 1st. <br>Even this situation is so called<br> Thread Race Condition]
end
// This is a simple program to demonstrate how to create a thread

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> /* For working with POSIX threads*/
#include <unistd.h>  /* For pause() and sleep() */
#include <errno.h>	 /* For using Global variable errno */

/* A thread callback fn must have following prototypes 
 * void *(*thread_fn)(void *)
 * */
static void *
thread_fn_callback(void *arg) {

	char *input = (char *)arg;

	while(1) {
		printf("input string = %s\n", input);
		sleep(1);
	}
}

void
thread1_create() {

	/* opaque object, dont bother about its internal
	 * members */
	pthread_t pthread1;

	/* Take some argument to be passed to the thread fn,
 	 * Look after that you always paas the persistent memory
 	 * as an argument to the thread, do not pass caller's 
 	 * local variables Or stack Memory*/
	static char *thread_input1 = "I am thread no 1";

	/* Return 0 on success, otherwise returns errorcode, all
 	 * pthread functions return -ve error code on failure, they
 	 * do not set global 'errno' variable */
	int rc = pthread_create(&pthread1, 
				   NULL, 
				   thread_fn_callback,
				   (void *)thread_input1);
	if(rc != 0) {

		printf("Error occurred, thread could not be created, errno = %d\n", rc);
		exit(0);
	}
}

int
main(int argc, char **argv){

	thread1_create();
	printf("main fn paused\n");
	pause(); // pause() is a system call, it will not return until a signal is received 
    //thread will create child threads and and wait for them to complete
	return 0;
}

main fn paused
input string = I am thread no 1
input string = I am thread no 1
input string = I am thread no 1
input string = I am thread no 1
input string = I am thread no 1
and so on .....

Multi Processing

Multi User

Multi Threading vs Multi Tasking

Threads

			graph BT;
			subgraph Thread 1
            A[Stack1
--

--
Heap1
Data1
Code1] end subgraph Thread 2 B[Stack2
--

--
Heap1
Data1
Code1] end subgraph Thread 3 C[Stack3
--

--
Heap1
Data1
Code1] end subgraph All are sharing Heap Data and Code end

Advantages of Threads

Types of Threads

graph TB;
A[Threads]
B[User Managed Threads]
C[Kernel Managed Threads]
A-->B
A-->C
User Managed Threads vs Kernel Managed Threads
User Managed Threads Kernel Managed Threads
Manageement in User Space Management in Kernel Space
Context Switching is faster Context Switching is slower
Blocking: One thread might block the other threads Blocking: A thread blocks it self Only
MultiCore or MultiProcessor : Can not take advantage of MultiCore or MultiProcessor Only Concurrent execution on single processor MultiCore or MultiProcessor : Take full advantage of Multi core System
Creation/Termination :Fast Creation/Termination :Slow
Context Switching :Fast Context Switching :Slow
Mapping of User Managed Threads to Kernel Managed Threads
graph TB;
subgraph One to One
subgraph User Space
subgraph Word Processor
A[ut1]
B[ut2]
C[ut3]
end
end
subgraph Kernel Space
AA[kt1]
BB[kt2]
CC[kt3]
A-->AA
B-->BB
C-->CC
end
end
subgraph Many to One
subgraph User Space
subgraph Word Processor
AX[ut1]
BX[ut2]
CX[ut3]
end
subgraph Music Player
AXX[ut1]
BXX[ut2]
CXX[ut3]
end
end
subgraph Kernel Space
AAA[kt1]
BBB[kt2]
AX-->AAA
BX-->AAA
CX-->AAA
AXX-->BBB
BXX-->BBB
CXX-->BBB
end
end
subgraph Many To Many
subgraph User Space
subgraph Word Processor
AXXXX[ut1]
BXXXX[ut2]
CXXXX[ut3]
end
subgraph Music Player
AXXXXX[ut1]
BXXXXX[ut2]
CXXXXX[ut3]
end
subgraph Kernel Space
AY[kt1]
BY[kt2]
CY[kt3]
AXXXX-->AY
AXXXX-->BY
AXXXX-->CY
AXXXXX-->AY
AXXXXX-->BY
AXXXXX-->CY
end
end
end