├── VirtualMemManagement ├── tlb.gif ├── Agenda.pdf ├── TLB.c └── address.txt ├── Make GNU ├── Writing a Makefile.pdf └── 3-2 Project Management Using GNU Make.pdf ├── IPC ├── ExchangeOfInfo │ ├── Pipe │ │ ├── Images │ │ │ ├── FD.png │ │ │ ├── Pipe-Intro.gif │ │ │ ├── Pipe1-Desc.jpg │ │ │ └── Pipe2-Desc.jpg │ │ ├── Pipe1.c │ │ ├── Pipe2.c │ │ └── Pipe3.c │ └── Shared-Memory │ │ ├── Client │ │ ├── Server │ │ ├── shm.png.pagespeed.ic.__XqIf0Vd2.webp │ │ ├── Client.c │ │ └── Server.c └── Synchronization │ └── Mutual Exclusion │ └── Producer-Consumer │ └── semaphore.c ├── Disk Scheduling Algorithms ├── disk-structure.png ├── FCFS │ ├── main2.c │ ├── main.c │ ├── main3.c │ └── main4.c └── LCFS │ └── main.c ├── Process Creation ├── exec.c ├── 1.c ├── 2.c ├── 4.c ├── 3.c ├── execCaller.c └── wait.c ├── Thread ├── C# Examples │ ├── Shared │ │ ├── App.config │ │ ├── SharedInfo.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ └── Shared.csproj │ ├── Tasks │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ └── Tasks.csproj │ ├── Threads │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ └── Threads.csproj │ ├── ParallelLinq │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ └── ParallelLinq.csproj │ └── Parallel.sln ├── Pthread_Join.c ├── Sum_Single_Thread.c ├── Sum_Multiple_Threads.c └── Pthread_Creation.c ├── CPU Scheduling Algorithms ├── FCFS │ ├── main-naive.c │ └── main.c ├── SJF │ └── main.c └── Round-Robin │ ├── RoundRobin.c │ └── Python │ └── RoundRobin.py ├── README.md ├── .gitattributes └── .gitignore /VirtualMemManagement/tlb.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/VirtualMemManagement/tlb.gif -------------------------------------------------------------------------------- /Make GNU/Writing a Makefile.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/Make GNU/Writing a Makefile.pdf -------------------------------------------------------------------------------- /VirtualMemManagement/Agenda.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/VirtualMemManagement/Agenda.pdf -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Images/FD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Pipe/Images/FD.png -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Shared-Memory/Client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Shared-Memory/Client -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Shared-Memory/Server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Shared-Memory/Server -------------------------------------------------------------------------------- /Disk Scheduling Algorithms/disk-structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/Disk Scheduling Algorithms/disk-structure.png -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Images/Pipe-Intro.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Pipe/Images/Pipe-Intro.gif -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Images/Pipe1-Desc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Pipe/Images/Pipe1-Desc.jpg -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Images/Pipe2-Desc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Pipe/Images/Pipe2-Desc.jpg -------------------------------------------------------------------------------- /Make GNU/3-2 Project Management Using GNU Make.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/Make GNU/3-2 Project Management Using GNU Make.pdf -------------------------------------------------------------------------------- /Process Creation/exec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | /* code */ 6 | printf("I am Exec.c Called by Execvp() \n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Process Creation/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | /* code */ 6 | fork(); 7 | 8 | printf("OSLab962\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Shared-Memory/shm.png.pagespeed.ic.__XqIf0Vd2.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoyeBright/OSLab/HEAD/IPC/ExchangeOfInfo/Shared-Memory/shm.png.pagespeed.ic.__XqIf0Vd2.webp -------------------------------------------------------------------------------- /Process Creation/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | /* code */ 7 | fork(); 8 | fork(); 9 | printf("OSLab962\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Thread/C# Examples/Shared/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Thread/C# Examples/Tasks/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Thread/C# Examples/Threads/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Thread/C# Examples/ParallelLinq/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Process Creation/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | /* code */ 7 | int p = fork(); 8 | if(p>0) 9 | { 10 | fork(); 11 | printf("OSLab962\n"); 12 | } 13 | printf("OSLab962\n"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Process Creation/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | /* code */ 7 | fork(); 8 | fork(); 9 | fork(); 10 | printf("OSLab962\n"); 11 | return 0; 12 | // So what does it conclude? 13 | // Number of Times OSLab962 printed is equal to number of process created 14 | // Total Number of Process = 2^n where n is number of fork system calls 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Process Creation/execCaller.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | /* code */ 7 | char *args[] = {"./exec.out",NULL} ; 8 | execvp(args[0],args); 9 | /*All statements are ignored after execvp() call as this whole 10 | process(execCaller.c) is replaced by another process (exec.c) 11 | */ 12 | printf("I am the message after 'execvp'!!"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Process Creation/wait.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | int main() { 8 | /* code */ 9 | // take one argument status and returns 10 | // a process ID of dead children. 11 | // pid_t wait(int *stat_loc); 12 | pid_t cpid; 13 | if(fork()==0) 14 | exit(0); 15 | else 16 | cpid = wait(NULL); 17 | printf("Parent pid = %d\n",getpid()); 18 | printf("Child pid = %d\n",cpid ); 19 | 20 | return 0; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Shared-Memory/Client.c: -------------------------------------------------------------------------------- 1 | // Shared memory is very fast 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SHSIZE 27 10 | int main() { 11 | key_t key; 12 | key = 5678; 13 | int shmid = shmget(key,SHSIZE,0666); 14 | if(shmid<0){ 15 | printf("Shmid!"); 16 | exit(1); 17 | } 18 | char *shm = (char*)shmat(shmid,NULL,0); 19 | if(shm ==(char*)-1){ 20 | printf("Shmat"); 21 | exit(1); 22 | } 23 | for(char *s = shm;*s!='\0';s++) 24 | putchar(*s); 25 | putchar('\n'); 26 | *shm = '*'; 27 | exit(0); 28 | } 29 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Shared-Memory/Server.c: -------------------------------------------------------------------------------- 1 | // Shared memory is very fast 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SHSIZE 27 10 | int main() { 11 | key_t key; 12 | key = 5678; 13 | int shmid = shmget(key,SHSIZE,IPC_CREAT | 0666); 14 | if(shmid<0){ 15 | printf("Shmid!"); 16 | exit(1); 17 | } 18 | char *shm = (char*)shmat(shmid,NULL,0); 19 | if(shm ==(char*)-1){ 20 | printf("Shmat"); 21 | exit(1); 22 | } 23 | char *s=shm; 24 | for(char c='a';c<='z';c++){ 25 | *s++ = c; 26 | } 27 | while(*shm !='*') 28 | sleep(1); 29 | 30 | exit(1); 31 | } 32 | -------------------------------------------------------------------------------- /Thread/Pthread_Join.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void* PrintHello (void* data){ 6 | int* data_ptr = (int*)data; 7 | int receivedData = * data_ptr; 8 | printf("Hello from new thread-Got %d\n",receivedData); 9 | pthread_exit(NULL); 10 | } 11 | 12 | int main() { 13 | 14 | pthread_t tid; 15 | pthread_attr_t attr; 16 | pthread_attr_init(&attr); 17 | int data_passed = 5; 18 | 19 | int rv = pthread_create(&tid, &attr, PrintHello, &data_passed); 20 | if(rv){ 21 | printf("Error: pthread_create generates error %d\n",rv); 22 | return 0; 23 | } 24 | // pthread_join is a functionality of pthread library which allows 25 | // the originating(main) thread to wait for the completion of all its 26 | // spawned thread's tasks 27 | pthread_join(tid, NULL); 28 | printf("The new thread id: %lld\n",(long long)tid); 29 | pthread_exit(NULL); 30 | } 31 | -------------------------------------------------------------------------------- /Thread/C# Examples/Tasks/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Shared; 7 | 8 | namespace Tasks 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | 15 | DoTasksAsync().GetAwaiter().GetResult(); 16 | 17 | Console.WriteLine("Press [Enter] to exit..."); 18 | Console.Read(); 19 | 20 | } 21 | 22 | static async Task DoTasksAsync() 23 | { 24 | 25 | await Task.WhenAll(GetTasks()); 26 | } 27 | 28 | static IEnumerable GetTasks() 29 | { 30 | foreach (var item in SharedInfo.Data) yield return Task.Run(() => 31 | { 32 | Console.WriteLine($"Result of Sum for [0 to {item}] is: {item.SumFromZero()}"); 33 | }); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Pipe1.c: -------------------------------------------------------------------------------- 1 | // Pipe behave Like FIFO (Queue DS) 2 | // Size of Read and Write dont match with each other 3 | // Can write 512 Bytes at a time but can read only 1 Byte at a time in Pipe 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define MSGSIZE 20 12 | 13 | char *msg1 = "Hello, OSLab962-1"; 14 | char *msg2 = "Hello, OSLab962-2"; 15 | char *msg3 = "Hello, OSLab962-3"; 16 | 17 | int main() { 18 | 19 | char inbuf[MSGSIZE]; 20 | int pipefd[2]; 21 | 22 | if(pipe(pipefd)<0)// we can use perror() 23 | exit(-1); 24 | 25 | write(pipefd[1], msg1, MSGSIZE); 26 | write(pipefd[1], msg2, MSGSIZE); 27 | write(pipefd[1], msg2, MSGSIZE); 28 | 29 | for(int i=0;i<3;i++){ 30 | // Read pipe 31 | read(pipefd[0], inbuf, MSGSIZE); 32 | printf("%s\n", inbuf); 33 | } 34 | // Suppress -Wno-write-strings to gcc 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Thread/C# Examples/Threads/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Shared; 8 | 9 | namespace Threads 10 | { 11 | class Program 12 | { 13 | 14 | static void Main(string[] args) 15 | { 16 | 17 | StartAllThreads(); 18 | 19 | } 20 | 21 | static void StartAllThreads() 22 | { 23 | foreach (var item in SharedInfo.Data) 24 | { 25 | new Thread(() => Print(item)) 26 | { 27 | IsBackground = true, 28 | Priority = ThreadPriority.AboveNormal 29 | }.Start(); 30 | } 31 | } 32 | 33 | static void Print(int item) 34 | { 35 | Console.WriteLine($"Result of Sum for [0 to {item}] is: {item.SumFromZero()}"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Pipe2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MSGSIZE 30 7 | 8 | char* msg1 = "Hello, OSLab962-1"; 9 | char* msg2 = "Hello, OSLab962-2"; 10 | char* msg3 = "Hello, OSLab962-3"; 11 | 12 | int main(){ 13 | 14 | int pipefd[2],nbytes; 15 | pid_t pid; 16 | char inbuf[MSGSIZE]; 17 | 18 | if(pipe(pipefd)<0) 19 | exit(1); 20 | 21 | if((pid = fork())>0){ 22 | write(pipefd[1], msg1, MSGSIZE); 23 | write(pipefd[1], msg2, MSGSIZE); 24 | write(pipefd[1], msg3, MSGSIZE); 25 | // close(pipefd[1]) wont hang on 26 | close(pipefd[1]); 27 | //............................ 28 | }else{ 29 | // close(pipefd[1]) wont hang on 30 | close(pipefd[1]); 31 | //............................. 32 | while((nbytes = read(pipefd[0], inbuf, MSGSIZE))>0){ 33 | printf("%s\n", inbuf); 34 | printf("The number of bytes actually read is: %d\n", nbytes); 35 | } 36 | if(nbytes!=0) 37 | exit(2); 38 | printf("\n***Finished Reading***\n"); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Thread/Sum_Single_Thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //The sum computed by the background thread 6 | long long sum=0; 7 | // Thread function to generate sum of 0 to N 8 | void* sum_runner(void * arg) 9 | { 10 | long long *limit_ptr = (long long*) arg; 11 | long long limit = * limit_ptr; 12 | for(long long i=0; i<=limit; i++){ 13 | sum +=i; 14 | } 15 | //Sum is a global varibale, so other threads can access 16 | pthread_exit(0); 17 | } 18 | 19 | int main(int argc, char const *argv[]) { 20 | 21 | if (argc<2){ 22 | printf("Usage: %s \n",argv[0] ); 23 | exit(-1); 24 | } 25 | 26 | long long limit = atoll(argv[1]); 27 | pthread_t tid; 28 | pthread_attr_t attr; 29 | pthread_attr_init(&attr); 30 | 31 | pthread_create(&tid, &attr, sum_runner, &limit); 32 | //Do other stuff here 33 | 34 | // Wait untill thread is done its mission 35 | pthread_join(tid, NULL); 36 | // Interstingly if pthread_join is not used, sum won't return a right result. 37 | printf("Sum is %lld\n", sum); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Thread/C# Examples/Shared/SharedInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Shared 8 | { 9 | 10 | public static class SharedInfo 11 | { 12 | 13 | /// 14 | /// Data used for calculations 15 | /// 16 | public static readonly int[] Data = new[] { 17 | 154541521, 1259, 4521, 3658, 3234, 545, 5452331, 5451211, 18 | 545454, 55121, 654521, 6526320, 5454, 5485412, 6521, 454521545, 19 | 545412151, 5415121, 545451, 56215, 4, 54845121, 87854, 20 | 54515121, 545121, 44111, 212121, 544884 21 | }; 22 | 23 | /// 24 | /// Returns 0 + 1 + 2 + ... + num 25 | /// 26 | /// Upper bound of summation 27 | /// Result of summation 28 | public static long SumFromZero(this int num) 29 | { 30 | long result = 0; 31 | for (int i = 1; i <= num; i++) 32 | result += i; 33 | 34 | return result; 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Disk Scheduling Algorithms/FCFS/main2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int HeadCurrentPosition,MaxTrackNum,ElementNum; 7 | printf("Enter the currect head position:"); 8 | scanf("%d",&HeadCurrentPosition); 9 | printf("\nEnter the Maximum tracks number:"); 10 | scanf("%d",&MaxTrackNum); 11 | printf("\nEnter number of the tracks that we are seeking:"); 12 | scanf("%d",&ElementNum); 13 | 14 | printf("\nEnter the tracks number in order:\n"); 15 | 16 | int count1=0,count2=0,queue[ElementNum],seektime=0,TotalSeekTime=0; 17 | while (count10 && value<=MaxTrackNum) queue[count1]=value; 22 | count1++; 23 | } 24 | 25 | while (count2%d\n",HeadCurrentPosition,seektime,queue[count2]); 29 | HeadCurrentPosition = queue[count2]; 30 | TotalSeekTime +=seektime; 31 | count2++; 32 | } 33 | 34 | printf("The Total seek time is equal to:%.2f\n",(float)TotalSeekTime); 35 | printf("The Average of total seek time is equal to:%.2f\n",(float)TotalSeekTime/ElementNum); 36 | 37 | return 0; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Disk Scheduling Algorithms/FCFS/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int CurrentHeadPosition,MaxNumTracks,QueueElements; 7 | 8 | printf("Enter the current head position:"); 9 | scanf("%d",&CurrentHeadPosition); 10 | printf("\nEnter the Maximum number of the tracks:"); 11 | scanf("%d",&MaxNumTracks); 12 | printf("\nEnter number of elements into the queue:"); 13 | scanf("%d",&QueueElements); 14 | printf("\nEnter Disk tracks elements:\n"); 15 | 16 | int queue[QueueElements],count1=0,count2=0,seekTime=0,TotalSeekTime=0; 17 | 18 | while(count10 && value <=MaxNumTracks) 24 | queue[count1] = value; 25 | else 26 | printf("\n***Sth goes wrong***\n"); 27 | count1++; 28 | } 29 | while(count2%d\n",CurrentHeadPosition,seekTime,queue[count2]); 33 | CurrentHeadPosition = queue[count2]; 34 | count2++; 35 | TotalSeekTime +=seekTime; 36 | } 37 | printf("Total Seek time is equal to:%d\n",TotalSeekTime); 38 | printf("Average Seek time:%.2f\n",(float)TotalSeekTime/QueueElements); 39 | 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Thread/C# Examples/ParallelLinq/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Shared; 7 | 8 | namespace ParallelLinq 9 | { 10 | class Program 11 | { 12 | 13 | static void Main(string[] args) 14 | { 15 | 16 | // Maximum number of Processors I want to use: Minimum of 512 and Enviroment.ProcessorCount 17 | int maxProcessorUsage = Math.Min(512, Environment.ProcessorCount); 18 | 19 | var parallelQuery = from item 20 | in SharedInfo.Data 21 | .AsParallel() // Make Query Parallel 22 | .WithExecutionMode(ParallelExecutionMode.ForceParallelism) // Do not optimize PLinq to Normal Linq 23 | .WithDegreeOfParallelism(maxProcessorUsage) 24 | select item; 25 | 26 | parallelQuery.ForAll(item => 27 | { 28 | Console.WriteLine($"Result of Sum for [0 to {item}] is: {item.SumFromZero()}"); 29 | }); 30 | 31 | Console.WriteLine("Press [Enter] to exit..."); 32 | Console.Read(); 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Disk Scheduling Algorithms/FCFS/main3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int CurrentHead,MaxTrackNum,ElementNum; 7 | 8 | printf("Enter the current number of the cylinder\n"); 9 | scanf("%d",&CurrentHead); 10 | printf("Enter the maximum track number\n"); 11 | scanf("%d",&MaxTrackNum); 12 | printf("Enter the number of the tracks\n"); 13 | scanf("%d",&ElementNum); 14 | 15 | printf("Please Enter the request in order\n"); 16 | 17 | int count1=0,count2=0,queue[ElementNum],seekTime=0,TotalSeekTime=0; 18 | while(count10 && value<=MaxTrackNum) 24 | { 25 | queue[count1] = value; 26 | } 27 | 28 | count1++; 29 | } 30 | while(count2%d\n",CurrentHead,seekTime,queue[count2] ); 34 | CurrentHead = queue[count2]; 35 | count2++; 36 | TotalSeekTime +=seekTime; 37 | } 38 | 39 | printf("The total Seek time is equal to:%d\n",TotalSeekTime); 40 | printf("The average of total seek time is equal to:%.2f",(float)TotalSeekTime/ElementNum); 41 | 42 | // for (int i = 0; i < ElementNum; i++) { 43 | // printf("%d\n",queue[i]); 44 | // } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Disk Scheduling Algorithms/FCFS/main4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int CurrentHeadNum,MaxTrackNum,ElementNum; 7 | printf("Enter the current number of the header\n"); 8 | scanf("%d",&CurrentHeadNum); 9 | printf("Enter the maximum number of the tracks\n"); 10 | scanf("%d",&MaxTrackNum); 11 | printf("Enter the the element/request number\n"); 12 | scanf("%d",&ElementNum); 13 | 14 | printf("Enter the requests in order\n"); 15 | int count1=0,count2=0,queue[ElementNum],SeekTime=0,TotalSeekTime=0; 16 | while (count10 && value<=MaxTrackNum) 20 | { 21 | queue[count1] = value; 22 | } 23 | count1++; 24 | } 25 | while (count2%d\n",CurrentHeadNum,SeekTime,queue[count2]); 28 | CurrentHeadNum = queue[count2]; 29 | TotalSeekTime +=SeekTime; 30 | count2++; 31 | } 32 | 33 | printf("The Total seek time is equal to:%d\n",TotalSeekTime); 34 | printf("The Average Total seek time is equal to:%.2f\n",(float)TotalSeekTime/ElementNum); 35 | 36 | // for (int i = 0; i < ElementNum; i++) { 37 | // printf("%d\n",queue[i]); 38 | // } 39 | } 40 | -------------------------------------------------------------------------------- /Thread/Sum_Multiple_Threads.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct sum_runner_struct { 6 | long long limit; 7 | long long answer; 8 | }; 9 | // Thread function to generate sum of 0 to N 10 | void* sum_runner(void * arg) 11 | { 12 | struct sum_runner_struct *arg_struct = (struct sum_runner_struct*) arg; 13 | long long sum=0; 14 | for(long long i=0; i<=arg_struct->limit; i++){ 15 | sum +=i; 16 | } 17 | arg_struct->answer = sum; 18 | pthread_exit(0); 19 | } 20 | 21 | int main(int argc, char const *argv[]) { 22 | if (argc<2){ 23 | printf("Usage: %s ... \n",argv[0] ); 24 | exit(-1); 25 | } 26 | int num_args = argc -1; // Because the first argument is for the filename 27 | // Set up thread 28 | pthread_t tids[num_args]; 29 | struct sum_runner_struct args[num_args]; 30 | 31 | for(int i=0; i 2 | 3 | int main() 4 | { 5 | int n,i,j,bt[20],wt[20],tat[20]; 6 | float awt,atat,tbt=0,twt=0,ttat=0; 7 | printf("\nPlease enter total number of Processes:"); 8 | scanf("%d",&n); 9 | 10 | printf("\nPlease Enter the burst values: \n"); 11 | for(i=0;i 2 | 3 | int main() { 4 | 5 | int n,i,bt[20],wt[20],tat[20]; 6 | float awt=0,atat=0; 7 | 8 | printf("\nPlease Enter Total number of the processes: \n"); 9 | scanf("%d",&n); 10 | 11 | for(i=0;i 2 | 3 | int main() { 4 | /* code */ 5 | int i, j, n, bt[20], pos, temp, wt[20], tat[20]; 6 | float awt=0, atat=0; 7 | 8 | printf("Enter Total Number of Processes:"); 9 | scanf("%d",&n); 10 | 11 | printf("Enter the following Burst Times:\n"); 12 | for (i = 0; i < n; i++) { 13 | /* code */ 14 | printf("Burst Time P[%d]:",i+1); 15 | scanf("%d",&bt[i]); 16 | } 17 | //Sorting burst time in ascending order... 18 | //Selection Sort.......................... 19 | for(i=0;i 2 | #include 3 | 4 | //Queue reversing......................... 5 | void ReverseAnArray(int Arr[],int start,int end) 6 | { 7 | while (start0 && value<=MaxNumTracks) 34 | queue[count1]= value; 35 | else 36 | printf("\n***Sth goes wrong***\n"); 37 | count1++; 38 | } 39 | 40 | ReverseAnArray(queue,0,QueueElements-1); 41 | 42 | while(count2%d\n",CurrentHeadPosition,seekTime,queue[count2]); 46 | CurrentHeadPosition = queue[count2]; 47 | count2++; 48 | TotalSeekTime +=seekTime; 49 | } 50 | printf("Total Seek time is equal to:%d\n",TotalSeekTime); 51 | printf("Average Seek time:%.2f\n",(float)TotalSeekTime/QueueElements); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Operating System Lab 2 | 3 | * Codes designed for Operating System Lab offered at University of Guilan: 4 | * [OSLab972] 2nd semester of 2019/1397 5 | * [OSLab971] 1st semester of 2018/1396 6 | * [OSLab962] 2nd semester of 2018-2019/1396 7 | 8 | 9 | ## Contents 10 | 11 | * ### Process Creation 12 | System calls consist of fork(), exect(), wait() 13 | 14 | * ### The CPU Scheduling Algorithms 15 | FCFS, SJF, RR and also other Algorithms will be programmed by students in different iterations. 16 | 17 | * ### The Disk Scheduling Algorithms 18 | FCFS, LCFS and also other Algorithms will be programmed by students in different iterations. 19 | 20 | * ### Project Management Using Make GNU 21 | Makefile, GNUMakefile 22 | 23 | * ### Threads 24 | POSIX Pthreads: Thread Creation and Termination - Sum of 0 to N (via 2 threads and multiple threads)
25 | Windows Thread(C#): Thread Creation and Termination - Sum of 0 to N (via 2 threads and multiple threads) 26 | 27 | * ### Exchange of Information: 28 | Pipe, Shared-Memory 29 | 30 | * ### Synchronization 31 | Solving the Producer-Consumer Problem Using Semaphores
32 | 33 | * ### Virtual Memory Management 34 | Translates logical to physical addresses for a virtual address space by using Translation Lookaside Buffer (TLB)
* There is no page fault handling 35 | 36 | ## Supported PLs 37 | Major parts of the project have been coded with C.
38 | Also, some experiments have been coded with C-family PLs. 39 | 40 | ## Instructor 41 | * [Javad PourMostafa] (http://github.com/joyebright) 42 | 43 | ## License 44 | 45 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details 46 | 47 | ## Acknowledge 48 | 49 | Drop me an E-mail in order to obtain the slides. 50 | -------------------------------------------------------------------------------- /Thread/Pthread_Creation.c: -------------------------------------------------------------------------------- 1 | 2 | // Thread Creation and Termination 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | long long sum = 0; 9 | 10 | void* sum_runner(void* data){ 11 | long long *my_data = (long long*)data; // Data gets by thread 12 | long long val = * my_data; 13 | for(long long i=0;i<=val;i++){ 14 | sum +=i; 15 | } 16 | pthread_exit(NULL); 17 | } 18 | int main(int argc, char const *argv[]) { 19 | if(argc>2){ 20 | printf("Usage: %s \n",argv[0]); 21 | return 0; 22 | } 23 | int rv; // Return Value 24 | pthread_t thread_id; // Thread's ID (int) 25 | long long data_passed = atoll(argv[1]); // Data passed to the new thread 26 | 27 | // Create a new thread that will execute 'PrintHello' 28 | /* 29 | Note: all of the arguments are pointers 30 | The first arg is a pointer to thread_id, 31 | the second arg is used to set some attributes for the new thread_id 32 | (if you use NULL it sets default values) 33 | the third is void* (*start_routine)(void*), accepts void* as an arg and also returns a void* as a rv 34 | what does it show? it indicates that is possible to pass an arbitrary piece of data to our new thread, 35 | and that our new thread can return an arbitrary piece of data when it finishes. 36 | how do we pass our thread an arbitrary argument? we use the fourth arg to call. 37 | */ 38 | rv = pthread_create(&thread_id, NULL, sum_runner, &data_passed); 39 | if(rv) 40 | { 41 | printf("Error: return code from pthread_create is %d\n", rv); 42 | return (0); 43 | } 44 | printf("\nCreated new thread (%d) .... \n", (int)thread_id); 45 | pthread_join(thread_id, NULL); 46 | printf("\nsum: %lld\n", sum); 47 | // Terminate the thread 48 | pthread_exit(NULL); 49 | } 50 | -------------------------------------------------------------------------------- /CPU Scheduling Algorithms/Round-Robin/RoundRobin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(){ 6 | int n, burst[30], wt[30], tat[30], remain[30],quantum; 7 | int time=0; 8 | int flag = 0; //to show finished process 9 | float awt=0, atat=0; 10 | 11 | printf("Enter Total Number of Processes:"); 12 | scanf("%d",&n); 13 | int rp = n; // number of remained processes 14 | printf("Enter Time Quantum:"); 15 | scanf("%d",&quantum); 16 | 17 | printf("Enter the Burst Times:\n"); 18 | for (int i = 0; i < n; i++) { 19 | printf("Burst Time P[%d]:",i+1); 20 | scanf("%d",&burst[i]); 21 | remain[i]= burst[i]; 22 | 23 | } 24 | 25 | while(1){ // we do this until there is no unfinished process 26 | for (int i = 0 ; i < n; i++){ 27 | if(remain[i]<=quantum && remain[i]>0){ 28 | time+= remain[i]; // total time until now 29 | wt[i]=time - burst[i];// the total time that has passed minus the burst time of process 30 | remain[i]=0; // the process is finished 31 | flag=1; 32 | 33 | printf("P%d\n",i+1); 34 | } 35 | if(remain[i]>quantum){ 36 | remain[i]-= quantum; 37 | time += quantum; 38 | printf("P%d\n",i+1); 39 | } 40 | if(remain[i]==0 && flag==1) 41 | rp--; // update the number of remained processes 42 | flag=0; 43 | } 44 | if(rp==0){ // there is no unfinished process 45 | break; 46 | 47 | } 48 | 49 | 50 | } 51 | printf("\nProcess\t\t Burst Time \t\t Waiting Time \t\t TurnAround Time \n"); 52 | for (int i = 0; i < n; i++) { 53 | tat[i]=wt[i]+burst[i]; // calculating the TurnAround time 54 | atat += tat[i]; 55 | awt += wt[i]; 56 | printf("\nP[%d] \t\t\t %d \t\t\t %d \t\t\t %d \n",i+1,burst[i],wt[i],tat[i]); 57 | } 58 | printf("\n"); 59 | printf("Average Turnaround Time is %.2f\n", (float)atat/n); 60 | printf("Average Waiting Time is %.2f\n", (float)awt/n); 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /IPC/ExchangeOfInfo/Pipe/Pipe3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | // We use two pipes 9 | // First pipe send input string from parent 10 | // Second pipe to send concatenated string from child 11 | int pipefd1[2]; 12 | int pipefd2[2]; 13 | char fixed_str[] = "guilan.ac.ir"; 14 | char input_str[100]; 15 | pid_t p; 16 | 17 | if(pipe(pipefd1)==-1){ 18 | fprintf(stderr, "Pipe Failed"); 19 | return 1; 20 | } 21 | if(pipe(pipefd2)==-1){ 22 | fprintf(stderr, "Pipe Failed"); 23 | return 1; 24 | } 25 | printf("Enter the input string:\n"); 26 | scanf("%s",input_str); 27 | p = fork(); 28 | if(p<0){ 29 | fprintf(stderr, "Fork Failed"); 30 | return 1; 31 | } 32 | else if(p>0){ 33 | //Parent Process 34 | char concat_str[100]; 35 | // Write=1, Read=0 36 | close(pipefd1[0]); //Close reading end of first pipe 37 | // Write the input strig and close writing end of First 38 | //pipe 39 | write(pipefd1[1],input_str,strlen(input_str)+1); 40 | // Wait for child to send a string 41 | wait(NULL); 42 | close(pipefd2[1]); // close writing end of second pipe 43 | // Read string from child, print it and close 44 | //reading end 45 | read(pipefd2[0],concat_str,100); 46 | printf("concatenated string %s\n", concat_str); 47 | close(pipefd2[0]); 48 | }// Child process 49 | else{ 50 | close(pipefd1[1]); // Close writing end of first pipe 51 | // Read a string using first pipe 52 | char concat_str[100]; 53 | read(pipefd1[0],concat_str,100); 54 | //concantenate a fixed string with it 55 | int k = strlen(concat_str); 56 | int i; 57 | for(i=0;i<(int)strlen(fixed_str);i++) 58 | concat_str[k++] = fixed_str[i]; 59 | concat_str[k] = '\0'; // strings end with '\O' 60 | //close both reading ends 61 | close(pipefd1[0]); 62 | close(pipefd2[0]); 63 | // write concatenated string and close writing end 64 | write(pipefd2[1],concat_str,strlen(concat_str)+1); 65 | close(pipefd2[1]); 66 | 67 | exit(0); 68 | } 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Thread/C# Examples/Shared/Shared.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {C06A7A9F-139C-4BF6-8396-392633FD61FA} 8 | Library 9 | Shared 10 | Shared 11 | v4.7 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /CPU Scheduling Algorithms/Round-Robin/Python/RoundRobin.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | # declare a dictionary where keys are processes' names and value of each key is the burst time of that process 4 | processes = {} 5 | # declare a dictionary of waiting time to store each process's waiting time 6 | waiting_time = {} 7 | 8 | number_of_processes = int(input("Please enter the number of processes: ")) 9 | for i in range(number_of_processes): 10 | burst_time = input("Please enter the burst time of process " + str(i+1) + ": ") 11 | processes[i] = int(burst_time) 12 | waiting_time[i] = 0 13 | 14 | # get a copy of burst times to keep track of remaining burst times 15 | remaining_burst_time = copy.deepcopy(processes) 16 | 17 | # declare the size of quantum slice 18 | quantum = int(input("Please enter the quantum size: ")) 19 | 20 | # declare the overall executing time of the processor 21 | total_time = 0 22 | 23 | # until all processes have finished executing 24 | while any(value != 0 for value in remaining_burst_time.values()): 25 | 26 | # calculate the remaining burst time and process it if needed 27 | for i in range(len(processes)): 28 | 29 | # check if the remaining burst time is larger than the quantum 30 | # so that all of quantum must be given to this process 31 | if remaining_burst_time[i] > quantum: 32 | total_time += quantum 33 | remaining_burst_time[i] -= quantum 34 | 35 | # if remaining burst time is smaller than the quantum, the process will finish its's execution 36 | # and what's left of the quantum will be spent on next process 37 | else: 38 | total_time += remaining_burst_time[i] 39 | # calculate the waiting time of the process 40 | waiting_time[i] = total_time - processes[i] 41 | remaining_burst_time[i] = 0 42 | 43 | 44 | # calculate the average waiting time 45 | total_waiting_time = 0 46 | for i in waiting_time.values(): 47 | total_waiting_time += i 48 | average_waiting_time = total_waiting_time / number_of_processes 49 | 50 | 51 | # calculate the average turnaround time (enter time of all processes are 0, by default) 52 | # if you want it, i can write another code to determine different in-time for each process 53 | total_turnaround_time = 0 54 | for item in range(number_of_processes): 55 | total_turnaround_time += waiting_time[item] + processes[item] 56 | average_turnaround_time = total_turnaround_time / number_of_processes 57 | 58 | 59 | print("\n\nThe average waiting time is: " + str(average_waiting_time)) 60 | print("The average turnaround time is: " + str(average_turnaround_time)) 61 | -------------------------------------------------------------------------------- /Thread/C# Examples/Parallel.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.2011 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tasks", "Tasks\Tasks.csproj", "{43EF342F-4642-4CA0-B716-529BD70D6EDB}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Threads", "Threads\Threads.csproj", "{9CE4091F-AAEC-45C7-8C9A-504E6106FE86}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelLinq", "ParallelLinq\ParallelLinq.csproj", "{2D12DD70-A005-41D2-956D-FE79D168B05A}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{C06A7A9F-139C-4BF6-8396-392633FD61FA}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {43EF342F-4642-4CA0-B716-529BD70D6EDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {43EF342F-4642-4CA0-B716-529BD70D6EDB}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {43EF342F-4642-4CA0-B716-529BD70D6EDB}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {43EF342F-4642-4CA0-B716-529BD70D6EDB}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {9CE4091F-AAEC-45C7-8C9A-504E6106FE86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {9CE4091F-AAEC-45C7-8C9A-504E6106FE86}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {9CE4091F-AAEC-45C7-8C9A-504E6106FE86}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {9CE4091F-AAEC-45C7-8C9A-504E6106FE86}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {2D12DD70-A005-41D2-956D-FE79D168B05A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {2D12DD70-A005-41D2-956D-FE79D168B05A}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {2D12DD70-A005-41D2-956D-FE79D168B05A}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {2D12DD70-A005-41D2-956D-FE79D168B05A}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {C06A7A9F-139C-4BF6-8396-392633FD61FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {C06A7A9F-139C-4BF6-8396-392633FD61FA}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {C06A7A9F-139C-4BF6-8396-392633FD61FA}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {C06A7A9F-139C-4BF6-8396-392633FD61FA}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | GlobalSection(ExtensibilityGlobals) = postSolution 41 | SolutionGuid = {253D4F8E-3BEE-4828-A00B-B43C7AF141EB} 42 | EndGlobalSection 43 | EndGlobal 44 | -------------------------------------------------------------------------------- /Thread/C# Examples/Threads/Threads.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {9CE4091F-AAEC-45C7-8C9A-504E6106FE86} 8 | Exe 9 | Threads 10 | Threads 11 | v4.7 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | {c06a7a9f-139c-4bf6-8396-392633fd61fa} 54 | Shared 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Thread/C# Examples/ParallelLinq/ParallelLinq.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {2D12DD70-A005-41D2-956D-FE79D168B05A} 8 | Exe 9 | ParallelLinq 10 | ParallelLinq 11 | v4.7 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | {c06a7a9f-139c-4bf6-8396-392633fd61fa} 54 | Shared 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /Thread/C# Examples/Tasks/Tasks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {43EF342F-4642-4CA0-B716-529BD70D6EDB} 8 | Exe 9 | Tasks 10 | Tasks 11 | v4.7 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | {c06a7a9f-139c-4bf6-8396-392633fd61fa} 57 | Shared 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /VirtualMemManagement/TLB.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const int TLB_SIZE = 16; 8 | const int PAGE_SIZE = 256; 9 | const int VM_SIZE = 256; 10 | const int MM_SIZE = 128; 11 | const int FRAME_SIZE = 256; 12 | 13 | int main(int argc, char const *argv[]) { 14 | /* code */ 15 | int tlb[TLB_SIZE][2]; 16 | int virtual_memory[VM_SIZE][2]; // After finding Page number, we fetch frame number 17 | int physical_mem[MM_SIZE]; 18 | 19 | //Initialization for TLB 20 | for(int i=0; i TLB_SIZE - 1)? -1 : i; 22 | tlb[i][1] = (i > TLB_SIZE - 1)? -1 : TLB_SIZE-i; 23 | } 24 | //Initialization for Page table 25 | for (int i=0; i MM_SIZE - 1)? -1 : i; 27 | virtual_memory[i][1] = (i > MM_SIZE - 1)? -1 : MM_SIZE - i; 28 | } 29 | //Initialization for Physical Memory 30 | for(int i=0;i> 8; // right shift is like dividing to two 66 | //printf("page table num:%d\n",pageNumber); 67 | int tlb_hit = 0; 68 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int producer_count, consumer_count, empty_count, buffer_length, *buffer, buffer_pos=-1; 10 | sem_t buffer_mutex, empty_count, full_count; 11 | pthread_t *producers, *consumers; 12 | 13 | int produce(pthread_t self){ 14 | int i = 0; 15 | int item = 1 + rand() % 40; // generate 1 to 40 different number 16 | while(!pthread_equal(*(producers+i), self) && i < producer_count){ 17 | i++; 18 | } 19 | printf("Producer %d produced %d\n", i+1, item); 20 | return item; 21 | } 22 | void consume(int item, pthread_t self){ 23 | int i = 0; 24 | while(!pthread_equal(*(consumers+i), self) && i < consumer_count){ 25 | i++; 26 | } 27 | printf("Buffer:"); 28 | for (int i = 0; i<=buffer_pos; ++i) { 29 | printf("%d", *(buffer+i)); 30 | } 31 | printf("\nConsumer %d consumed %d \n=> Current buffer length: %d\n", i+1, item, buffer_pos); 32 | 33 | } 34 | void *producer(void *args){ 35 | while(1){ 36 | int item = produce(pthread_self()); 37 | sem_wait(&empty_count); 38 | sem_wait(&buffer_mutex); 39 | ++buffer_pos; //CS 40 | *(buffer + buffer_pos) = item; 41 | sem_post(&buffer_mutex); 42 | sem_post(&full_count); 43 | sleep(1 + rand() % 20); 44 | } 45 | return NULL; 46 | } 47 | void* consumer(void *args){ 48 | int item; 49 | while(1){ 50 | sem_wait(&full_count); 51 | sem_wait(&buffer_mutex); 52 | item = * (buffer + buffer_pos); 53 | consume(item, pthread_self()); 54 | --buffer_pos; 55 | sem_post(&buffer_mutex); 56 | sem_post(&empty_count); 57 | sleep(1+rand() % 20); 58 | } 59 | return NULL; 60 | } 61 | 62 | int main(int argc, char const *argv[]) { 63 | /* code */ 64 | int err; 65 | 66 | sem_init(&buffer_mutex, 0, 1); 67 | sem_init(&full_count, 0, 0); 68 | 69 | // Why and When to use malloc 70 | // Dynamic memory allocation 71 | // Allocate memory at run time because we don't know the exact amount of memory during compile time 72 | printf("Enter the number of Producer:"); 73 | scanf("%d", &producer_count); 74 | producers = (pthread_t*) malloc(producer_count * sizeof(pthread_t)); 75 | 76 | printf("Enter the number of Consumer:"); 77 | scanf("%d", &consumer_count); 78 | consumers = (pthread_t*) malloc(consumer_count * sizeof(pthread_t)); 79 | 80 | printf("Enter the buffer capacity:"); 81 | scanf("%d", &buffer_length); 82 | buffer = (int*) malloc(buffer_length * sizeof(int)); 83 | // 3rd Semaphore 84 | sem_init(&empty_count, 0, buffer_length); 85 | // Create the producers 86 | for(int i=0;i