├── .gitignore ├── Lab 1 ├── Documents │ ├── Os_assign1.doc │ ├── Zombie and orphan process.doc │ ├── fork and memory usage.doc │ └── fork.ppt ├── Programs │ ├── ass1.c │ ├── execl_program.c │ ├── fork.c │ └── zombie.c ├── Solution │ └── assignment_1.pdf ├── assignment1.c └── assignment2.c ├── Lab 2 ├── Assignment │ ├── assignment2.pdf │ ├── implementation of IPC.pdf │ └── pipe.pdf ├── Documents │ ├── IPC.pdf │ ├── Pipe.pdf │ ├── ipc.pdf │ └── ipc1.pdf ├── Solution │ └── assignment_2.pdf ├── a.out └── asssignment1.c ├── Lab 3 ├── Assignment 2b.doc ├── Pipe.doc ├── Programs │ ├── Two_Pipes.c │ ├── cli │ ├── client.c │ ├── pipe │ ├── salman.txt │ ├── serv │ ├── server.c │ ├── single_pipe │ └── user.c ├── fifo_client_serverprogram.pdf └── ipc.pdf ├── Lab 4 ├── Lab_assignment_messagequeue.doc ├── Message Queues.ppt ├── Programs │ ├── chat1 │ ├── chat2 │ ├── msgq_recv.c │ ├── msgq_send.c │ ├── receive.c │ ├── recv │ ├── send │ └── send.c ├── lab-msgqueue.doc ├── labb.doc ├── msg_que.ppt └── program.doc ├── Lab 5 ├── Shared_memory_assignment.doc ├── fibonacci │ ├── fibo │ └── fibo.c ├── producer_consumer.doc ├── program.doc ├── programs │ ├── README.md │ ├── shm_client │ ├── shm_client.c │ ├── shm_server │ └── shm_server.c └── shared_memory.pdf ├── Lab 6 ├── Programs │ ├── semaphore │ └── semaphore.c ├── semaphore.c ├── semaphore_explanation.ppt └── semaphore_pgm.doc ├── Lab 7 ├── dining_mutex.c ├── dining_philospher_problem.pdf ├── dining_sem_op ├── dining_semaphore.c └── dining_semaphore_op ├── Lab 8 ├── .~lock.Assignment.doc# ├── Assignment.doc ├── Banker's algorithm_explanation.doc ├── bankers.c └── bankers_algorithm.c ├── Practice ├── CodeChef_orphan.cpp ├── bankers_algo.cpp ├── cli ├── code ├── dining_mutex.c ├── dining_semaphore.c ├── fifo_client.c ├── fifo_server.c ├── fork.c ├── fork.cpp ├── msgqueue.c ├── new_process.c ├── pipe ├── pipe-example.c ├── pipe.c ├── pipe_dream.c ├── queue.c ├── semaphore.c ├── semaphore.cpp ├── serv ├── test ├── test.c ├── threads ├── threads.cpp ├── threads_args.cpp └── zombie.cpp ├── References ├── W. Richard Stevens - Unix Network Programming.pdf └── operating_system_concepts.pdf └── Solutions ├── Lab1 ├── fork_sort.c ├── orphan.c ├── os1.c ├── os1_1.c ├── os2.c └── zombie.c ├── Lab2 ├── pipe_fibo.c └── pipe_fibo2.c ├── Lab3 ├── client.c ├── hello ├── pipe2.c ├── pipedup.c └── server.c ├── Lab4 ├── chat1.c ├── chat2.c ├── chat_msgque.c ├── msgque_rcv.c ├── msgque_send.c └── send_msg.c └── Lab5 ├── chat1.c ├── chat2.c └── shared_server.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /Lab 1/Documents/Os_assign1.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 1/Documents/Os_assign1.doc -------------------------------------------------------------------------------- /Lab 1/Documents/Zombie and orphan process.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 1/Documents/Zombie and orphan process.doc -------------------------------------------------------------------------------- /Lab 1/Documents/fork and memory usage.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 1/Documents/fork and memory usage.doc -------------------------------------------------------------------------------- /Lab 1/Documents/fork.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 1/Documents/fork.ppt -------------------------------------------------------------------------------- /Lab 1/Programs/ass1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | pid_t pid = fork(); 8 | 9 | if(pid == 0) { 10 | printf("Entering Child process now ...\n"); 11 | 12 | int n; 13 | scanf("%d",&n); 14 | int fib[n]; 15 | fib[0] = 0; 16 | fib[1] = 1; 17 | 18 | for(int i = 2; i < n; i ++) { 19 | fib[i] = fib[i-1] + fib[i-2]; 20 | } 21 | 22 | for(int i = 0; i < n; i ++) { 23 | printf("%d ",fib[i]); 24 | } 25 | printf("\n"); 26 | 27 | printf("Exiting Child process now ...\n"); 28 | } 29 | 30 | else if(pid > 0) { 31 | printf("Entering Parent process now ...\n"); 32 | sleep(10); 33 | printf("Exiting Parent process now ...\n"); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Lab 1/Programs/execl_program.c: -------------------------------------------------------------------------------- 1 | //Include header files 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | //Main function 8 | int main() { 9 | pid_t pid; 10 | //Fork a child process 11 | pid = fork(); 12 | 13 | //Error has occurred 14 | if(pid < 0) { 15 | fprintf(stderr, "Fork failed"); 16 | exit(-1); 17 | } 18 | //Child process has been created 19 | else if(pid == 0) { 20 | execlp("/bin/ls","ls",NULL); 21 | } 22 | //Parent process 23 | //Parent will wait for child to complete 24 | else { 25 | wait(NULL); 26 | printf("\nChild complete\n"); 27 | exit(0); 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Lab 1/Programs/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | pid_t pid = fork(); 8 | 9 | if(pid == 0) { 10 | printf("Child process created\n"); 11 | int n; 12 | n = 10; 13 | int fibo[n]; 14 | 15 | fibo[0] = 0; 16 | fibo[1] = 1; 17 | 18 | for(int i = 2; i <= n; i ++) 19 | fibo[i] = fibo[i-1] + fibo[i-2]; 20 | 21 | printf("The %dth fibonacci number is %d\n",n, fibo[10]); 22 | } 23 | else 24 | printf("Parent process created\n"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Lab 1/Programs/zombie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | int arr[100]; 8 | for(int i = 0; i < 6; i ++) 9 | scanf("%d",&n); 10 | 11 | pid_t pid = fork(); 12 | 13 | if(pid == 0) { 14 | printf("Entering Child process now ...\n"); 15 | 16 | printf("Exiting Child process now ...\n"); 17 | exit(0); 18 | } 19 | 20 | else if (pid > 0) { 21 | printf("Entering Parent process now ...\n"); 22 | sleep(5); 23 | printf("Exiting Parent process now ...\n"); 24 | 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Lab 1/Solution/assignment_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 1/Solution/assignment_1.pdf -------------------------------------------------------------------------------- /Lab 1/assignment1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | pid_t pid = fork(); 8 | 9 | if(pid == 0) { 10 | printf("Child process created\n"); 11 | int n; 12 | n = 10; 13 | int fibo[n]; 14 | 15 | fibo[0] = 0; 16 | fibo[1] = 1; 17 | 18 | for(int i = 2; i <= n; i ++) 19 | fibo[i] = fibo[i-1] + fibo[i-2]; 20 | 21 | printf("The %dth fibonacci number is %d\n",n, fibo[10]); 22 | } 23 | else 24 | printf("Parent process created\n"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Lab 1/assignment2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int arr[] = {1,4,8,7,5,3,6,9,5,2}; 7 | int b[100]; 8 | 9 | void swap(int a, int b) { 10 | int tmp = arr[a]; 11 | arr[a] = arr[b]; 12 | arr[b] = tmp; 13 | } 14 | 15 | void quickSort(int l, int r) { 16 | if (l < r) { 17 | int v = arr[r]; 18 | int i = l - 1, j = r; 19 | 20 | for (;;) { 21 | while (arr[++i] < v); 22 | while (arr[--j] > v); 23 | if (i >= j) break; 24 | swap(i, j); 25 | } 26 | 27 | swap(i, r); 28 | quickSort(l, i - 1); 29 | quickSort(i + 1, r); 30 | } 31 | } 32 | 33 | void merging(int low, int mid, int high) { 34 | //Declare variables 35 | int l1, l2, i; 36 | 37 | //Put the element of the two smaller elements in the new array 38 | for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { 39 | if(arr[l1] <= arr[l2]) 40 | b[i] = arr[l1++]; 41 | else 42 | b[i] = arr[l2++]; 43 | } 44 | 45 | //Finish off in case some of the elements are not put 46 | while(l1 <= mid) 47 | b[i++] = arr[l1++]; 48 | 49 | //Finish off in case some of the elements are not put 50 | while(l2 <= high) 51 | b[i++] = arr[l2++]; 52 | 53 | //Copy the elements of first array into the second array 54 | for(i = low; i <= high; i++) 55 | arr[i] = b[i]; 56 | } 57 | 58 | void mergeSort(int low, int high) { 59 | int mid; 60 | 61 | if(low < high) { 62 | mid = (low + high) / 2; 63 | mergeSort(low, mid); 64 | mergeSort(mid+1, high); 65 | merging(low, mid, high); 66 | } 67 | 68 | else 69 | return; 70 | 71 | } 72 | 73 | 74 | int main() { 75 | 76 | 77 | pid_t pid = fork(); 78 | 79 | if(pid == 0) { 80 | printf("Entering Child process now ...\n"); 81 | sleep(20); 82 | 83 | quickSort(0, 9); 84 | 85 | printf("Entering Child Quick Sort\n"); 86 | for(int i = 0; i < 10; i ++) 87 | printf("%d ", arr[i]); 88 | printf("\n"); 89 | 90 | printf("Exiting Child process now ...\n"); 91 | exit(0); 92 | } 93 | 94 | else if (pid > 0) { 95 | printf("Entering Parent process now ...\n"); 96 | mergeSort(0, 9); 97 | 98 | printf("Entering Parent Merge Sort\n"); 99 | for(int i = 0; i < 10; i++) 100 | printf("%d ", arr[i]); 101 | printf("\n"); 102 | 103 | wait(NULL); 104 | 105 | printf("Exiting Parent process now ...\n"); 106 | 107 | } 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /Lab 2/Assignment/assignment2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Assignment/assignment2.pdf -------------------------------------------------------------------------------- /Lab 2/Assignment/implementation of IPC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Assignment/implementation of IPC.pdf -------------------------------------------------------------------------------- /Lab 2/Assignment/pipe.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Assignment/pipe.pdf -------------------------------------------------------------------------------- /Lab 2/Documents/IPC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Documents/IPC.pdf -------------------------------------------------------------------------------- /Lab 2/Documents/Pipe.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Documents/Pipe.pdf -------------------------------------------------------------------------------- /Lab 2/Documents/ipc.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Documents/ipc.pdf -------------------------------------------------------------------------------- /Lab 2/Documents/ipc1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Documents/ipc1.pdf -------------------------------------------------------------------------------- /Lab 2/Solution/assignment_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/Solution/assignment_2.pdf -------------------------------------------------------------------------------- /Lab 2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 2/a.out -------------------------------------------------------------------------------- /Lab 2/asssignment1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() { 9 | int pid,pfd[2],n,a,b,c; 10 | 11 | if(pipe(pfd)==-1) { 12 | printf("Error in pipe connection\n"); 13 | exit(1); 14 | } 15 | pid=fork(); 16 | 17 | if(pid>0) { 18 | printf("Parent Process\n"); 19 | printf("Fibonacci Series\n"); 20 | printf("Enter the limit for the series:\n"); 21 | scanf("%d",&n); 22 | close(pfd[0]); 23 | write(pfd[1],&n,sizeof(n)); 24 | close(pfd[1]); 25 | exit(0); 26 | } 27 | else { 28 | close(pfd[1]); 29 | read(pfd[0],&n,sizeof(n)); 30 | printf("Child Process\n"); 31 | a=0; 32 | b=1; 33 | close(pfd[0]); 34 | printf("Fibonacci Series is:\n"); 35 | printf("%d\n%d\n",a,b); 36 | while(n>2) { 37 | c=a+b; 38 | printf("%d\n",c); 39 | a=b; 40 | b=c; 41 | n--; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Lab 3/Assignment 2b.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Assignment 2b.doc -------------------------------------------------------------------------------- /Lab 3/Pipe.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Pipe.doc -------------------------------------------------------------------------------- /Lab 3/Programs/Two_Pipes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | 11 | int pipe1fd[2],pipe2fd[2],n,file; 12 | char buffer[1024],fname[50]; 13 | 14 | if(pipe(pipe1fd)==-1 || pipe(pipe2fd)==-1 ) { 15 | printf("\nError in pipe connection\n"); 16 | exit(1); 17 | } 18 | 19 | if(!fork()) { 20 | close(pipe1fd[1]); 21 | printf("Enter filename to request:\n"); 22 | scanf("%s", fname); 23 | write(pipe2fd[1], fname, sizeof(fname)); 24 | printf("Received response\n"); 25 | while((n = read(pipe1fd[0], buffer, sizeof(buffer)))>0) { 26 | write(1, buffer, n); 27 | } 28 | close(pipe2fd[0]); 29 | } 30 | 31 | else { 32 | close(pipe1fd[0]); 33 | printf("Waiting for request...\n"); 34 | read(pipe2fd[0], fname, sizeof(fname)); 35 | printf("Received request for %s\n", fname); 36 | file = open(fname,O_RDONLY); 37 | if (file < 0) 38 | write(pipe1fd[1], "File not found\n", 15); 39 | else { 40 | while((n = read(file, buffer, sizeof(buffer))) > 0) 41 | write(pipe1fd[1], buffer, n); 42 | } 43 | close(pipe2fd[1]); 44 | exit(0); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Lab 3/Programs/cli: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Programs/cli -------------------------------------------------------------------------------- /Lab 3/Programs/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() { 9 | char fname[50], buffer[1025]; 10 | int req, res, n; 11 | 12 | req = open("req.fifo", O_WRONLY); 13 | res = open("res.fifo", O_RDONLY); 14 | 15 | if(req < 0 || res < 0) { 16 | printf("Please start the server"); 17 | exit(-1); 18 | } 19 | 20 | printf("Enter the filename to request\n"); 21 | scanf("%s",fname); 22 | 23 | write(req, fname,sizeof(fname)); 24 | 25 | printf("Recieved response\n"); 26 | 27 | while((n = read(res, buffer, sizeof(buffer))) > 0) { 28 | write(1, buffer, n); 29 | } 30 | 31 | close(req); 32 | close(res); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Lab 3/Programs/pipe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Programs/pipe -------------------------------------------------------------------------------- /Lab 3/Programs/salman.txt: -------------------------------------------------------------------------------- 1 | Hi my name is Salman Shah. 2 | Your program has executed successfully 3 | 4 | -------------------------------------------------------------------------------- /Lab 3/Programs/serv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Programs/serv -------------------------------------------------------------------------------- /Lab 3/Programs/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() { 9 | char fname[50], buffer[1025]; 10 | int req, res,n,file; 11 | 12 | mkfifo("req.fifo",0777); 13 | mkfifo("res.fifo",0777); 14 | 15 | printf("Waiting for request...\n"); 16 | 17 | req = open("req.fifo", O_RDONLY); 18 | res = open("res.fifo", O_WRONLY); 19 | 20 | read(req, fname, sizeof(fname)); 21 | 22 | printf("Recieved request for filename%s\n", fname); 23 | 24 | file = open(fname, O_RDONLY); 25 | 26 | if(file < 0) 27 | write(res, "File not found\n", 15); 28 | else { 29 | while((n = read(file, buffer, sizeof(buffer))) > 0) { 30 | write(res,buffer, n); 31 | } 32 | } 33 | 34 | close(req); 35 | close(res); 36 | unlink("req.fifo"); 37 | unlink("res.fifo"); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Lab 3/Programs/single_pipe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/Programs/single_pipe -------------------------------------------------------------------------------- /Lab 3/Programs/user.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int pfds[2]; 7 | pipe(pfds); 8 | 9 | if(!fork()) { 10 | close(1); 11 | dup(pfds[1]); 12 | close(pfds[0]); 13 | execlp("who","who", NULL); 14 | } 15 | 16 | else { 17 | close(0); 18 | dup(pfds[0]); 19 | close(pfds[1]); 20 | execlp("wc","wc","-l",NULL); 21 | } 22 | } -------------------------------------------------------------------------------- /Lab 3/fifo_client_serverprogram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/fifo_client_serverprogram.pdf -------------------------------------------------------------------------------- /Lab 3/ipc.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 3/ipc.pdf -------------------------------------------------------------------------------- /Lab 4/Lab_assignment_messagequeue.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Lab_assignment_messagequeue.doc -------------------------------------------------------------------------------- /Lab 4/Message Queues.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Message Queues.ppt -------------------------------------------------------------------------------- /Lab 4/Programs/chat1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Programs/chat1 -------------------------------------------------------------------------------- /Lab 4/Programs/chat2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Programs/chat2 -------------------------------------------------------------------------------- /Lab 4/Programs/msgq_recv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAXSIZE 128 10 | 11 | void die(char *s) { 12 | perror(s); 13 | exit(1); 14 | } 15 | 16 | struct msgbuf { 17 | long mtype; 18 | char mtext[MAXSIZE]; 19 | }; 20 | 21 | main() { 22 | 23 | int msqid; 24 | int msgflg = IPC_CREAT | 0666; 25 | 26 | key_t key; 27 | 28 | struct msgbuf sbuf; 29 | size_t buflen; 30 | 31 | key = 1234; 32 | 33 | if(msqid = msgget(key, 0666) < 0) { 34 | die("msgget"); 35 | } 36 | 37 | if(msgrcv(msqid, &sbuf, MAXSIZE, 1, 0) < 0) { 38 | perror("msgrcv"); 39 | exit(1); 40 | } 41 | 42 | printf("%s\n",sbuf.mtext); 43 | exit(0); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Lab 4/Programs/msgq_send.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAXSIZE 128 10 | 11 | void die(char *s) { 12 | perror(s); 13 | exit(1); 14 | } 15 | 16 | struct msgbuf { 17 | long mtype; 18 | char mtext[MAXSIZE]; 19 | }; 20 | 21 | main() { 22 | 23 | int msqid; 24 | int msgflg = IPC_CREAT | 0666; 25 | key_t key; 26 | 27 | struct msgbuf sbuf; 28 | size_t buflen; 29 | key = 1234; 30 | 31 | //Get the message queue ID for the given key 32 | if ((msqid=msgget(key,msgflg))< 0) 33 | die("msgget"); 34 | 35 | //Message Type 36 | sbuf.mtype = 1; 37 | printf("Enter a message to add to message queue : "); 38 | scanf("%[^\n]",sbuf.mtext); 39 | 40 | getchar(); 41 | buflen = strlen(sbuf.mtext) + 1 ; 42 | 43 | if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) { 44 | printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen); 45 | die("msgsnd"); 46 | } 47 | else 48 | printf("Message Sent\n"); 49 | 50 | exit(0); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Lab 4/Programs/receive.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAXSIZE 128 8 | void die(char *s) 9 | { 10 | perror(s); 11 | exit(1); 12 | } 13 | 14 | typedef struct msgbuf 15 | { 16 | long mtype; 17 | char mtext[MAXSIZE]; 18 | } ; 19 | main() 20 | { 21 | int msqid,i; 22 | key_t key; 23 | struct msgbuf rcvbuffer,sbuf; 24 | key = 1234; 25 | size_t buflen; 26 | sbuf.mtype = 1; 27 | 28 | for(i=0;i<5;i++) 29 | { 30 | if ((msqid = msgget(key, 0666)) < 0) 31 | die("msgget()"); 32 | //Receive an answer of message type 1. 33 | if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) 34 | die("msgrcv"); 35 | printf("%s\n", rcvbuffer.mtext); 36 | 37 | 38 | 39 | printf("Enter a message to add to message queue : "); 40 | scanf("%[^\n]",sbuf.mtext); 41 | getchar(); 42 | buflen = strlen(sbuf.mtext) + 1 ; 43 | if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) 44 | { 45 | printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen); 46 | die("msgsnd"); 47 | } 48 | else 49 | printf("Message Sent\n"); 50 | 51 | 52 | } 53 | exit(0); 54 | } 55 | -------------------------------------------------------------------------------- /Lab 4/Programs/recv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Programs/recv -------------------------------------------------------------------------------- /Lab 4/Programs/send: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/Programs/send -------------------------------------------------------------------------------- /Lab 4/Programs/send.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAXSIZE 128 8 | void die(char *s) 9 | { 10 | perror(s); 11 | exit(1); 12 | } 13 | struct msgbuf 14 | { 15 | long mtype;// mtype has to be any positive value. on the receiving side also mtype has to be same 16 | char mtext[MAXSIZE]; 17 | }; 18 | 19 | main() 20 | { 21 | int msqid,i; 22 | int msgflg = IPC_CREAT | 0666; 23 | key_t key; 24 | struct msgbuf sbuf,rcvbuffer; 25 | size_t buflen; 26 | key = 1234; 27 | if ((msqid=msgget(key,msgflg))< 0) //Get the message queue ID for the given key 28 | die("msgget"); 29 | //Message Type 30 | sbuf.mtype = 1; 31 | for( i=0;i<5;i++) 32 | { 33 | printf("Enter a message to add to message queue : "); 34 | scanf("%[^\n]",sbuf.mtext); 35 | getchar(); 36 | buflen = strlen(sbuf.mtext) + 1 ; 37 | if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) 38 | { 39 | printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen); 40 | die("msgsnd"); 41 | } 42 | else 43 | printf("Message Sent\n"); 44 | 45 | 46 | 47 | 48 | printf("message received is \n"); 49 | if ((msqid = msgget(key, 0666)) < 0) 50 | die("msgget()"); 51 | //Receive an answer of message type 1. 52 | if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) 53 | die("msgrcv"); 54 | printf("%s\n", rcvbuffer.mtext); 55 | 56 | 57 | 58 | } 59 | exit(0); 60 | } 61 | -------------------------------------------------------------------------------- /Lab 4/lab-msgqueue.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/lab-msgqueue.doc -------------------------------------------------------------------------------- /Lab 4/labb.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/labb.doc -------------------------------------------------------------------------------- /Lab 4/msg_que.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/msg_que.ppt -------------------------------------------------------------------------------- /Lab 4/program.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 4/program.doc -------------------------------------------------------------------------------- /Lab 5/Shared_memory_assignment.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 5/Shared_memory_assignment.doc -------------------------------------------------------------------------------- /Lab 5/fibonacci/fibo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 5/fibonacci/fibo -------------------------------------------------------------------------------- /Lab 5/fibonacci/fibo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | void main(){ 10 | int shmid; 11 | int *shmp; 12 | int MIN; 13 | int temp; 14 | int flag =SHM_R | SHM_W; 15 | 16 | int pid=fork(); 17 | if(pid==0){ 18 | int i; 19 | printf("Child Process\n"); 20 | printf("Trying to get shared memory ID: %d\n",3456); 21 | if((shmid=shmget(3456,1024,flag))<0) 22 | perror("Error in shmget\n"); 23 | shmp = (int *)shmat(shmid, 0,0); 24 | int num; 25 | printf("Enter the number of terms\n->" ); 26 | scanf("%d",&num); 27 | if(numshmp[1]){ 28 | printf("Enter again !\n"); 29 | scanf("%d",&num); 30 | } 31 | shmp[0]=num; 32 | shmp[1]=0; 33 | shmp[2]=1; 34 | for(i=3;i 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char **argv) { 9 | int shmid; 10 | char *shm; 11 | key_t key; 12 | 13 | if(argc < 2) { 14 | fprintf(stderr, "Usage:%s key\n",argv[0]); 15 | exit(2); 16 | } 17 | key = atoi(argv[1]); 18 | 19 | printf("Trying shared memory:%d\n",key); 20 | shmid = shmget(key, 1000, SHM_R | SHM_W); 21 | if(shmid < 0) { 22 | perror("shmget"); 23 | shmid= key; 24 | } 25 | else 26 | printf("Shared memory: %d\n",shmid); 27 | 28 | shm = (char *)shmat(shmid, /*addr*/0, /*flag*/0); 29 | if(shm == (char *)-1) { 30 | perror("shmat"); 31 | exit(1); 32 | } 33 | printf("Shared Memory: %p\n",shm); 34 | 35 | if(shm != 0){ 36 | printf("Shared memory content:%s\n",shm); 37 | } 38 | 39 | sleep(500); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Lab 5/programs/shm_server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 5/programs/shm_server -------------------------------------------------------------------------------- /Lab 5/programs/shm_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char **argv){ 10 | int shmid; 11 | char *shm; 12 | key_t key = IPC_PRIVATE; 13 | int flag = SHM_R | SHM_W; 14 | 15 | if(argc >= 2) 16 | key = atoi(argv[1]); 17 | 18 | shmid = shmget(key, 1000, flag); 19 | printf("Shared memory for key %d:%d\n",key,shmid); 20 | 21 | if(shmid < 0) { 22 | perror("shmget"); 23 | printf("Try to create this segment\n"); 24 | shmid = shmget(key, 1000, flag | IPC_CREAT); 25 | if(shmid < 0) 26 | perror("shmget | IPC_CREAT"); 27 | } 28 | 29 | shm = (char *)shmat(shmid, /*addr*/0, /*flag*/0); 30 | if(shm == (char *)-1) { 31 | perror("shmat"); 32 | exit(1); 33 | } 34 | 35 | sprintf(shm, "Hello World\n"); 36 | printf("Shared memory content: %s\n",shm); 37 | sleep(1000); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Lab 5/shared_memory.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 5/shared_memory.pdf -------------------------------------------------------------------------------- /Lab 6/Programs/semaphore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 6/Programs/semaphore -------------------------------------------------------------------------------- /Lab 6/Programs/semaphore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mutex = 1, full = 0, empty = 5, x = 0; 5 | 6 | main() { 7 | int n; 8 | void producer(); 9 | void consumer(); 10 | int wait(int); 11 | int signal(int); 12 | 13 | printf("1.Producer\n2.Consumer\n3.Exit\n"); 14 | 15 | while(1) { 16 | printf("Enter your choice\n"); 17 | scanf("%d",&n); 18 | 19 | switch(n) { 20 | case 1: 21 | if((mutex == 1)&&(empty != 0)) 22 | producer(); 23 | else 24 | printf("Buffer is Full\n"); 25 | break; 26 | case 2: 27 | if((mutex ==1)&&(full!=0)) 28 | consumer(); 29 | else 30 | printf("Buffer is Empty\n"); 31 | break; 32 | case 3: 33 | printf("Exiting the program\n"); 34 | exit(0); 35 | break; 36 | } 37 | } 38 | } 39 | 40 | int wait(int s) { 41 | return(--s); 42 | } 43 | 44 | int signal(int s) { 45 | return(++s); 46 | } 47 | 48 | void producer() { 49 | mutex = wait(mutex); 50 | full = signal(full); 51 | empty = wait(empty); 52 | x++; 53 | 54 | printf("Producer produces the item:%d\n",x); 55 | mutex = signal(mutex); 56 | } 57 | 58 | void consumer() { 59 | mutex = wait(mutex); 60 | full = wait(full); 61 | empty = signal(empty); 62 | 63 | printf("Consumer consumes item %d\n",x); 64 | x--; 65 | mutex = signal(mutex); 66 | } -------------------------------------------------------------------------------- /Lab 6/semaphore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mutex = 1, full = 0, empty = 5, x = 0; 5 | 6 | main() { 7 | int n; 8 | void producer(); 9 | void consumer(); 10 | int wait(int); 11 | int signal(int); 12 | 13 | printf("1.Producer\n2.Consumer\n3.Exit\n"); 14 | 15 | while(1) { 16 | printf("Enter your choice\n"); 17 | scanf("%d",&n); 18 | 19 | switch(n) { 20 | case 1: 21 | if((mutex == 1)&&(empty != 0)) 22 | producer(); 23 | else 24 | printf("Buffer is Full\n"); 25 | break; 26 | case 2: 27 | if((mutex ==1)&&(full!=0)) 28 | consumer(); 29 | else 30 | printf("Buffer is Empty\n"); 31 | break; 32 | case 3: 33 | exit(0); 34 | break; 35 | } 36 | } 37 | } 38 | 39 | int wait(int s) { 40 | return(--s); 41 | } 42 | 43 | int signal(int s) { 44 | return(++s); 45 | } 46 | 47 | void producer() { 48 | mutex = wait(mutex); 49 | full = signal(full); 50 | empty = wait(empty); 51 | x++; 52 | 53 | printf("Producer produces the item:%d\n",x); 54 | mutex = signal(mutex); 55 | } 56 | 57 | void consumer() { 58 | mutex = wait(mutex); 59 | full = wait(full); 60 | empty = signal(empty); 61 | 62 | printf("Consumer consumes item %d\n",x); 63 | x--; 64 | mutex = signal(mutex); 65 | } -------------------------------------------------------------------------------- /Lab 6/semaphore_explanation.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 6/semaphore_explanation.ppt -------------------------------------------------------------------------------- /Lab 6/semaphore_pgm.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Lab 6/semaphore_pgm.doc -------------------------------------------------------------------------------- /Lab 7/dining_mutex.c: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | # include 5 | 6 | # include 7 | # include 8 | # include 9 | # include 10 | 11 | # define philosopher 5 12 | 13 | pthread_mutex_t chopstick[philosopher]; 14 | 15 | void *thread_func(int n) { 16 | 17 | int i,iteration=5; 18 | 19 | for(i=0;i 2 | # include 3 | # include 4 | # include 5 | 6 | # include 7 | # include 8 | # include 9 | # include 10 | # include 11 | 12 | sem_t chopstick[100]; 13 | 14 | int n; 15 | 16 | void *thread_func(int no) { 17 | 18 | int i,iteration=5; 19 | 20 | for(i=0;i Begin eating",no); 27 | 28 | sleep(1); 29 | 30 | printf("\nPhilosopher %d –> Finish eating\n",no); 31 | 32 | sem_post(&chopstick[no]); 33 | 34 | sem_post(&chopstick[(no+1)%n]); 35 | 36 | } 37 | 38 | pthread_exit(NULL); 39 | 40 | } 41 | 42 | int main() { 43 | 44 | int i,res; 45 | 46 | pthread_t a_thread[100]; 47 | 48 | printf("\nEnter the number of philosopher :"); 49 | 50 | scanf("%d",&n); 51 | 52 | for(i=0;i 2 | 3 | int current[5][5], maximum_claim[5][5], available[5]; 4 | int allocation[5] = {0, 0, 0, 0, 0}; 5 | int maxres[5], running[5], safe = 0; 6 | int counter = 0, i, j, exec, resources, processes, k = 1; 7 | 8 | int main() { 9 | printf("\nEnter number of processes: "); 10 | scanf("%d", &processes); 11 | 12 | for (i = 0; i < processes; i++) { 13 | running[i] = 1; 14 | counter++; 15 | } 16 | 17 | printf("\nEnter number of resources: "); 18 | scanf("%d", &resources); 19 | 20 | printf("\nEnter Claim Vector:"); 21 | for (i = 0; i < resources; i++) { 22 | scanf("%d", &maxres[i]); 23 | } 24 | 25 | printf("\nEnter Allocated Resource Table:\n"); 26 | for (i = 0; i < processes; i++) { 27 | for(j = 0; j < resources; j++) { 28 | scanf("%d", ¤t[i][j]); 29 | } 30 | } 31 | 32 | printf("\nEnter Maximum Claim Table:\n"); 33 | for (i = 0; i < processes; i++) { 34 | for(j = 0; j < resources; j++) { 35 | scanf("%d", &maximum_claim[i][j]); 36 | } 37 | } 38 | 39 | printf("\nThe Claim Vector is: "); 40 | for (i = 0; i < resources; i++) { 41 | printf("\t%d", maxres[i]); 42 | } 43 | 44 | printf("\nThe Allocated Resource Table:\n"); 45 | for (i = 0; i < processes; i++) { 46 | for (j = 0; j < resources; j++) { 47 | printf("\t%d", current[i][j]); 48 | } 49 | printf("\n"); 50 | } 51 | 52 | printf("\nThe Maximum Claim Table:\n"); 53 | for (i = 0; i < processes; i++) { 54 | for (j = 0; j < resources; j++) { 55 | printf("\t%d", maximum_claim[i][j]); 56 | } 57 | printf("\n"); 58 | } 59 | 60 | for (i = 0; i < processes; i++) { 61 | for (j = 0; j < resources; j++) { 62 | allocation[j] += current[i][j]; 63 | } 64 | } 65 | 66 | printf("\nAllocated resources:"); 67 | for (i = 0; i < resources; i++) { 68 | printf("\t%d", allocation[i]); 69 | } 70 | 71 | for (i = 0; i < resources; i++) { 72 | available[i] = maxres[i] - allocation[i]; 73 | } 74 | 75 | printf("\nAvailable resources:"); 76 | for (i = 0; i < resources; i++) { 77 | printf("\t%d", available[i]); 78 | } 79 | printf("\n"); 80 | 81 | while (counter != 0) { 82 | safe = 0; 83 | for (i = 0; i < processes; i++) { 84 | if (running[i]) { 85 | exec = 1; 86 | for (j = 0; j < resources; j++) { 87 | if (maximum_claim[i][j] - current[i][j] > available[j]) { 88 | exec = 0; 89 | break; 90 | } 91 | } 92 | if (exec) { 93 | printf("\nProcess%d is executing\n", i + 1); 94 | running[i] = 0; 95 | counter--; 96 | safe = 1; 97 | 98 | for (j = 0; j < resources; j++) { 99 | available[j] += current[i][j]; 100 | } 101 | break; 102 | } 103 | } 104 | } 105 | if (!safe) { 106 | printf("\nThe processes are in unsafe state.\n"); 107 | break; 108 | } 109 | else { 110 | printf("\nThe process is in safe state"); 111 | printf("\nAvailable vector:"); 112 | 113 | for (i = 0; i < resources; i++) { 114 | printf("\t%d", available[i]); 115 | } 116 | 117 | printf("\n"); 118 | } 119 | } 120 | return 0; 121 | } -------------------------------------------------------------------------------- /Lab 8/bankers_algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int k=5,output[10],ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=; 7 | printf("\n Enter the number of resources : "); 8 | scanf("%d", &rz); 9 | printf("\n enter the max instances of each resources\n"); 10 | for(i=0;i"); 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Practice/CodeChef_orphan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() { 7 | int pid = fork(); 8 | 9 | //Parent process 10 | if(pid > 0) { 11 | printf("In Parent Process\n"); 12 | } 13 | 14 | Child Process 15 | else if(pid == 0) { 16 | sleep(10); 17 | printf("In Child process\n"); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Practice/bankers_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int max[10][10], allocation[10][10], available[10], allocation[10][10], process_order[10]; 5 | 6 | int main() { 7 | //Number of processes 8 | int n; 9 | 10 | //Number of states 11 | int m; 12 | 13 | //Enter the max matrix 14 | for(int i = 0; i < n; i ++) { 15 | for(int j = 0; j < m; j ++) { 16 | scanf("%d",&max[i][j]); 17 | } 18 | } 19 | 20 | //Enter the allocation matrix 21 | for(int i = 0; i < n; i ++) { 22 | for(int j = 0; j < m; j ++) { 23 | scanf("%d",&allocation[i][j]); 24 | } 25 | } 26 | 27 | //Enter the available matrix 28 | for(int i = 0; i < m; i ++) { 29 | scanf("%d",&available[i]); 30 | } 31 | 32 | //Enter the need matrix 33 | for(int i = 0; i < n; i ++) { 34 | for(int j = 0; j < m; j ++) { 35 | need[i][j] = max[i][j] - allocation[i][j]; 36 | } 37 | } 38 | 39 | int count = n; 40 | int process_no = 1; 41 | 42 | while(count) { 43 | for(int i = 0; i < n; i ++) { 44 | if(process_no > 5) 45 | process_no = 1; 46 | if(process_no == process_order[i]) { 47 | process_no = (process_no+1); 48 | } 49 | } 50 | 51 | 52 | int flag = 1; 53 | for(int i = 0; i < m; i ++) { 54 | if(need[process_no-1][i] > available[i]) 55 | flag = 0; 56 | } 57 | 58 | if(flag) 59 | 60 | } 61 | 62 | 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Practice/cli: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/cli -------------------------------------------------------------------------------- /Practice/code: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/code -------------------------------------------------------------------------------- /Practice/dining_mutex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define philosopher 5 12 | 13 | pthread_mutex_t chopstick[philosopher]; 14 | 15 | void *thread_func(int n) { 16 | 17 | for(int i = 0; i < philosopher; i ++) { 18 | sleep(1); 19 | 20 | pthread_mutex_lock(&chopstick[n]); 21 | pthread_mutex_lock(&chopstick[(n+1)%philosopher]); 22 | 23 | printf("Begin Eating: %d\n",n); 24 | sleep(1); 25 | printf("Finish Eating: %d\n",n); 26 | 27 | pthread_mutex_unlock(&chopstick[n]); 28 | pthread_mutex_unlock(&chopstick[(n+1)%philosopher]); 29 | 30 | } 31 | 32 | pthread_exit(NULL); 33 | } 34 | 35 | int main() { 36 | 37 | pthread_t a_thread[philosopher]; 38 | 39 | void *thread_func(int n); 40 | 41 | for(int i = 0; i < philosopher; i ++) { 42 | int res = pthread_mutex_init(&chopstick[i], NULL); 43 | 44 | if(res == -1) { 45 | perror("Mutex initialization failed"); 46 | exit(1); 47 | } 48 | } 49 | 50 | for(int i = 0; i < philosopher; i ++) { 51 | int res = pthread_create(&philosopher[i], NULL, thread_func, ); 52 | } 53 | } -------------------------------------------------------------------------------- /Practice/dining_semaphore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | sem_t chopstick[100]; 13 | 14 | int n; 15 | 16 | void *thread_func(int no) { 17 | 18 | int i, iterations = 5; 19 | 20 | for(i = 0; i < iterations; i ++) { 21 | 22 | sem_wait(&chopstick[no]); 23 | sem_wait(&chopstick[(no+1)%n]); 24 | 25 | printf("Philosopher %d -> Begin eating\n",no); 26 | sleep(1); 27 | printf("Philosopher %d -> Finish eating\n",no ); 28 | 29 | sem_post(&chopstick[no]); 30 | sem_post(&chopstick[(no+1)%n]); 31 | } 32 | 33 | pthread_exit(NULL); 34 | } 35 | 36 | int main() { 37 | int i, res; 38 | 39 | pthread_t a_thread[100]; 40 | printf("Enter the number of philosophers:\n"); 41 | scanf("%d",&n); 42 | 43 | for(int i = 0; i < n; i ++) { 44 | 45 | res = sem_init(&chopstick[i], 0, 0); 46 | 47 | if(res == -1) { 48 | perror("Semaphore initalization failed"); 49 | exit(1); 50 | } 51 | } 52 | 53 | for(int i = 0; i < n; i ++) { 54 | res = pthread_Create(&a_thread[i], NULL, thread_func, (int*)); 55 | 56 | if(res != 0) { 57 | perror("Semaphore creation failed"); 58 | exit(1); 59 | } 60 | 61 | sem_post(&chopstick[i]); 62 | } 63 | 64 | for(int i = 0; i < n; i ++) { 65 | res = pthread_join(a_thread[i], NULL); 66 | if(res != 0) { 67 | perror("Semaphore join failed"); 68 | exit(1); 69 | } 70 | } 71 | 72 | printf("Thread join successful\n"); 73 | for(int i = 0; i < n; i ++) { 74 | res = sem_destroy(&chopstick[i]); 75 | 76 | if(res == -1) { 77 | perror("Semaphore destruction failed"); 78 | exit(1); 79 | } 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Practice/fifo_client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() { 9 | char fname[50], buffer[1025]; 10 | int req, res, n; 11 | 12 | req = open("req.fifo", O_WRONLY); 13 | res = open("res.fifo", O_RDONLY); 14 | 15 | if(req < 0 || res < 0) { 16 | printf("Please start the server first\n"); 17 | exit(-1); 18 | } 19 | printf("Enter the filename to be sent\n"); 20 | scanf("%s",fname); 21 | write(req, fname, sizeof(fname)); 22 | 23 | printf("Recieved response\n"); 24 | while((n = read(res, buffer, sizeof(buffer))) > 0) 25 | write(1, buffer, sizeof(buffer)); 26 | 27 | close(req); 28 | close(res); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Practice/fifo_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | char fname[50], buffer[1025]; 9 | int req, res, n, file; 10 | 11 | mkfifo("req.fifo",0777); 12 | mkfifo("res.fifo",0777); 13 | 14 | printf("Waiting for request...\n"); 15 | 16 | req = open("req.fifo", O_RDONLY); 17 | res = open("res.fifo", O_WRONLY); 18 | 19 | read(req, fname, sizeof(fname)); 20 | printf("Received request for %s\n",fname); 21 | 22 | file = open(fname, O_RDONLY); 23 | if(file < 0) 24 | write(res, "File not found\n", 15); 25 | else { 26 | while(n = read(file, buffer, sizeof(buffer)) > 0) { 27 | write(res, buffer, n); 28 | } 29 | } 30 | 31 | close(req); 32 | close(res); 33 | 34 | unlink("req.fifo"); 35 | unlink("res.fifo"); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Practice/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_COUNT 200 6 | #define BUF_SIZE 100 7 | 8 | void main(void) { 9 | pid_t pid; 10 | int i; 11 | char buf[BUF_SIZE]; 12 | 13 | fork(); 14 | 15 | pid = getpid(); 16 | for (i = 1; i <= MAX_COUNT; i++) { 17 | sprintf(buf, "This line is from pid %d, value = %d\n", pid, i); 18 | write(1, buf, strlen(buf)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Practice/fork.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_COUNT 200 5 | #define BUF_SIZE 100 6 | 7 | void main(void) { 8 | pid_t pid; 9 | int i; 10 | char buf[BUF_SIZE]; 11 | 12 | fork(); 13 | 14 | pid = getpid(); 15 | 16 | for(i = 1; i <= MAX_COUNT; i ++) { 17 | sprintf(buf, "This line is from pid %d, value = %d\n", pid, i); 18 | write(1, buf, strlen(buf)); 19 | } 20 | } -------------------------------------------------------------------------------- /Practice/msgqueue.c: -------------------------------------------------------------------------------- 1 | #include /* For printf, scanf, etc...*/ 2 | #include 3 | #include 4 | #include 5 | #include /* For getpid() function */ 6 | #include /* For strcpy() function */ 7 | 8 | #define M_SIZE 100 /* Size of the Message */ 9 | 10 | /* Defining our own message, up to M_SIZE char's in length */ 11 | typedef struct { 12 | ulong msg_to; 13 | ulong msg_from; 14 | char msg_content[M_SIZE]; 15 | } MESSAGE; 16 | 17 | int main() { 18 | 19 | key_t key; 20 | int mq_handle; 21 | struct msqid_ds *info; // Leave for now 22 | int ret; 23 | int mypid = getpid(); /* To uniquely identify ourselves */ 24 | MESSAGE msg, ret_msg; /* msg - messge to send. ret_msg - to retrieve message 25 | 26 | /* Create Key */ 27 | key = ftok(".", 1234); 28 | printf("Key generated = %08X\n", key); 29 | 30 | /* Create message queue */ 31 | mq_handle = msgget(key, IPC_CREAT | 0777); /* Create it if it doesn't exist */ 32 | if(mq_handle<=-1) { 33 | printf("Error creating message queue\n"); 34 | perror("MQ Sample"); /* print the error */ 35 | return(1); 36 | } 37 | printf("Message Queue created\n"); 38 | 39 | /* Obtain information */ 40 | /* To demonstrate IPC_STAT flag */ 41 | ret = msgctl(mq_handle, IPC_STAT, info); 42 | if(ret==-1) { 43 | printf("Failed to obtain message queue information\n"); 44 | perror("MQ Sample"); 45 | exit(1); 46 | } 47 | printf("MQ Information:\n"); 48 | printf("Owner UID: %d\n", info->msg_perm.uid); 49 | printf("Owner GID: %d\n", info->msg_perm.gid); 50 | printf("# of bytes on queue: %d\n", info->msg_cbytes); 51 | printf("# of messages on queue: %d\n", info->msg_qnum); 52 | printf("# of bytes on queue: %d\n", info->msg_qbytes); 53 | 54 | /* Send a message */ 55 | 56 | msg.msg_to = mypid; /* send it to ourselves - look at the msgrcv call below*/ 57 | msg.msg_from = mypid; 58 | 59 | memset(msg.msg_content, 0x0, M_SIZE); /* clear the msg buffer */ 60 | strcpy(msg.msg_content, "Testing the MQ"); 61 | 62 | ret = msgsnd(mq_handle, &msg, sizeof(msg), 0); 63 | if(ret==-1) { 64 | perror("MQ Sample msgsend"); 65 | exit(1); 66 | } 67 | printf("Message sent\n"); 68 | 69 | /* Retrieve a message */ 70 | 71 | ret = msgrcv(mq_handle, &ret_msg, sizeof(ret_msg), mypid,0); 72 | if(ret==-1) { 73 | perror("MQ Sample msgrcv"); 74 | exit(1); 75 | } 76 | printf("Message received: %s\n", ret_msg.msg_content); 77 | 78 | /* Delete message queue */ 79 | msgctl(mq_handle, IPC_RMID, (struct msqid_ds *) 0); 80 | printf("Message Queue deleted\n"); 81 | 82 | return(0); 83 | } 84 | -------------------------------------------------------------------------------- /Practice/new_process.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | pid_t pid = fork(); 6 | 7 | if(pid == 0) 8 | printf("Child process created\n"); 9 | else 10 | printf("Parent process created\n"); 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Practice/pipe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/pipe -------------------------------------------------------------------------------- /Practice/pipe-example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int pfds[2]; 7 | char buf[30]; 8 | 9 | pipe(pfds); 10 | 11 | if(!fork()) { 12 | printf("Child reading to pipe\n"); 13 | read(pfds[0], buf, 6); 14 | printf("%s\n",buf ); 15 | printf("Child finished reading\n"); 16 | } 17 | else { 18 | printf("Parent writing from pipe\n"); 19 | write(pfds[1], "Hello", 6); 20 | printf("Parent finished writing\n"); 21 | } 22 | } -------------------------------------------------------------------------------- /Practice/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char const *argv[]) { 6 | int n; 7 | int fd[2]; 8 | 9 | char buf[1025]; 10 | char *data = "Hello .. this is sample data."; 11 | 12 | pipe(fd); 13 | write(fd[1], data, strlen(data)); 14 | 15 | if((n = read(fd[0], buf, 1024)) >= 0) { 16 | buf[n] = 0; 17 | printf("Read %d bytes from the pipe %s\n",n,buf); 18 | } 19 | else 20 | perror("Read"); 21 | 22 | exit(0); 23 | } -------------------------------------------------------------------------------- /Practice/pipe_dream.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | int pfds[2]; 7 | 8 | pipe(pfds); 9 | 10 | if(!fork()) { 11 | close(1); 12 | dup(pfds[1]); 13 | close(pfds[0]); 14 | execlp("ls", "ls", NULL); 15 | } 16 | else { 17 | close(0); 18 | dup(pfds[0]); 19 | close(pfds[1]); 20 | execlp("wc", "wc", "-l", NULL); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Practice/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define M_SIZE 100 9 | 10 | typedef struct { 11 | ulong msg_to; 12 | ulong msg_from; 13 | char msg_content[M_SIZE]; 14 | } MESSAGE; 15 | 16 | int main() { 17 | key_t key; 18 | int mq_handle; 19 | int ret; 20 | int mypid = getpid(); 21 | MESSAGE msg, ret_msg; 22 | 23 | //Create key 24 | key = ftok(".", 1234); 25 | printf("Key generated = %08X\n",key ); 26 | 27 | //Create Message queue 28 | mq_handle = msgget(key, IPC_CREAT | 0777); 29 | if(mq_handle <= -1) { 30 | printf("Error creating message queue\n"); 31 | perror("MQ Sample"); 32 | return 1; 33 | } 34 | printf("Message Queue created\n"); 35 | 36 | msg.msg_to = mypid; 37 | msg.msg_from = mypid; 38 | 39 | memset(msg.msg_content, 0x0, M_SIZE); 40 | strcpy(msg.msg_content, "Testing the Message Queue"); 41 | 42 | ret = msgsnd(mq_handle, &msg, sizeof(msg), 0); 43 | if(ret == -1) { 44 | perror("MQ sample msgsend"); 45 | exit(1); 46 | } 47 | printf("Message sent\n"); 48 | 49 | ret = msgrcv(mq_handle, &ret_msg, sizeof(ret_msg), mypid, 0); 50 | if(ret == -1) { 51 | perror("MQ Sample msgrcv"); 52 | exit(1); 53 | } 54 | printf("Message received: %s\n",ret_msg.msg_content ); 55 | 56 | msgctl(mq_handle, IPC_RMID, (struct msqid_ds *) 0); 57 | printf("Message Queue deleted\n"); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Practice/semaphore.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int mutex=1,full=0,empty=3,x=0; 5 | 6 | main() { 7 | 8 | int n; 9 | 10 | void producer(); 11 | 12 | void consumer(); 13 | 14 | int wait(int); 15 | 16 | int signal(int); 17 | 18 | printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n"); 19 | 20 | while(1) { 21 | 22 | printf("\nENTER YOUR CHOICE\n"); 23 | 24 | scanf("%d",&n); 25 | 26 | switch(n) { 27 | 28 | case 1: 29 | 30 | if((mutex==1)&&(empty!=0)) 31 | 32 | producer(); 33 | 34 | else 35 | 36 | printf("BUFFER IS FULL"); 37 | 38 | break; 39 | 40 | case 2: 41 | 42 | if((mutex==1)&&(full!=0)) 43 | 44 | consumer(); 45 | 46 | else 47 | 48 | printf("BUFFER IS EMPTY"); 49 | 50 | break; 51 | 52 | case 3: 53 | 54 | exit(0); 55 | 56 | break; 57 | 58 | } 59 | 60 | } 61 | 62 | } 63 | 64 | int wait(int s) { 65 | 66 | return(--s); 67 | 68 | } 69 | 70 | int signal(int s) { 71 | 72 | return(++s); 73 | 74 | } 75 | 76 | void producer() { 77 | 78 | mutex=wait(mutex); 79 | 80 | full=signal(full); 81 | 82 | empty=wait(empty); 83 | 84 | x++; 85 | 86 | printf("\nproducer produces the item%d",x); 87 | 88 | mutex=signal(mutex); 89 | 90 | } 91 | 92 | void consumer() { 93 | 94 | mutex=wait(mutex); 95 | 96 | full=wait(full); 97 | 98 | empty=signal(empty); 99 | 100 | printf("\n consumer consumes item%d",x); 101 | 102 | x--; 103 | 104 | mutex=signal(mutex); 105 | 106 | } -------------------------------------------------------------------------------- /Practice/semaphore.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mutex = 1, full = 0, empty = 3, x = 0; 5 | 6 | int wait(int s) { 7 | return (--s); 8 | } 9 | 10 | int signal(int s) { 11 | return (++s); 12 | } 13 | 14 | void producer() { 15 | 16 | mutex = wait(mutex); 17 | 18 | full = signal(full); 19 | 20 | empty = wait(empty); 21 | 22 | x++; 23 | 24 | printf("Producer produces the item %d\n",x); 25 | 26 | mutex = signal(mutex); 27 | } 28 | 29 | void consumer() { 30 | 31 | mutex = wait(mutex); 32 | 33 | full = wait(full); 34 | 35 | empty = signal(empty); 36 | 37 | printf("Consumer consumes item:%d\n",x); 38 | 39 | x--; 40 | 41 | mutex = signal(empty); 42 | 43 | } 44 | 45 | int main() { 46 | 47 | int n; 48 | 49 | printf("\n1.Producer\n2.Consumer\n3.Exit\n"); 50 | 51 | while(1) { 52 | printf("Enter your choice\n->"); 53 | scanf("%d",&n); 54 | 55 | switch(n) { 56 | case 1: 57 | if((mutex == 1) && (empty != 0)) 58 | producer(); 59 | else 60 | printf("Buffer is full"); 61 | break; 62 | 63 | case 2: 64 | if((mutex == 1) && (full!=0)) 65 | consumer(); 66 | else 67 | printf("Buffer is empty\n"); 68 | break; 69 | 70 | case 3: 71 | exit(0); 72 | break; 73 | } 74 | 75 | } 76 | 77 | return 0; 78 | 79 | } -------------------------------------------------------------------------------- /Practice/serv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/serv -------------------------------------------------------------------------------- /Practice/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/test -------------------------------------------------------------------------------- /Practice/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char const *argv[]) { 7 | int n; 8 | // scanf("%d",&n); 9 | while((n = read(0, &n, sizeof(n))) != 0){ 10 | write(1, &n, sizeof(n)); 11 | printf("End program\n"); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Practice/threads: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/Practice/threads -------------------------------------------------------------------------------- /Practice/threads.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define NUM_THREADS 5 8 | 9 | void *PrintHello(void *threadid) { 10 | long tid; 11 | tid = (long)threadid; 12 | cout << "Hello World! Thread ID, " << tid << endl; 13 | pthread_exit(NULL); 14 | } 15 | 16 | int main() { 17 | pthread_t threads[NUM_THREADS]; 18 | int rc; 19 | int i; 20 | 21 | for(i = 0; i < NUM_THREADS; i++) { 22 | cout << "main(): creating thread, " << i << endl; 23 | rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i); 24 | 25 | if (rc) { 26 | cout << "Error: unable to create thread," << rc << endl; 27 | exit(-1); 28 | } 29 | } 30 | 31 | pthread_exit(NULL); 32 | } -------------------------------------------------------------------------------- /Practice/threads_args.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define NUM_THREADS 5 8 | 9 | struct thread_data { 10 | int thread_id; 11 | char 12 | } -------------------------------------------------------------------------------- /Practice/zombie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | pid_t child_pid; 7 | 8 | //Create a child 9 | child_pid = fork(); 10 | 11 | //Parent process 12 | if(child_pid > 0) { 13 | sleep(60); 14 | } 15 | //Child Process 16 | else { 17 | exit(0); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /References/W. Richard Stevens - Unix Network Programming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/References/W. Richard Stevens - Unix Network Programming.pdf -------------------------------------------------------------------------------- /References/operating_system_concepts.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbshah97/Operating-Systems-Algorithms/3dea3af9361d9766188d5907747da4c410f6bed9/References/operating_system_concepts.pdf -------------------------------------------------------------------------------- /Solutions/Lab1/fork_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void swap(int* a, int* b) 6 | { 7 | int t = *a; 8 | *a = *b; 9 | *b = t; 10 | } 11 | 12 | int partition (int a[], int low, int high) 13 | { 14 | int j; 15 | int pivot = a[high]; 16 | int i = (low - 1); 17 | 18 | for (j = low; j <= high- 1; j++) 19 | { 20 | if (a[j] <= pivot) 21 | { 22 | i++; 23 | swap(&a[i], &a[j]); 24 | } 25 | } 26 | swap(&a[i + 1], &a[high]); 27 | return (i + 1); 28 | } 29 | 30 | void quickSort(int a[], int low, int high) 31 | { 32 | if (low < high) 33 | { 34 | int pi = partition(a, low, high); 35 | 36 | quickSort(a, low, pi - 1); 37 | quickSort(a, pi + 1, high); 38 | } 39 | } 40 | 41 | void printArray(int a[], int size) 42 | { 43 | int i; 44 | for (i=0; i < size; i++) 45 | printf("%d ", a[i]); 46 | printf("\n"); 47 | } 48 | 49 | void Merge(int *a,int *L,int leftCount,int *R,int rightCount) { 50 | 51 | int i,j,k; 52 | 53 | i = 0; j = 0; k =0; 54 | 55 | while(i 2 | #include 3 | #include 4 | int main() 5 | { 6 | 7 | pid_t pid=fork(); 8 | if(pid==0) 9 | { 10 | printf("Child process sleeping\n"); 11 | sleep(15); 12 | printf("Child process terminating\n"); 13 | } 14 | else{ 15 | printf("Parent process exiting\n"); 16 | exit(0); 17 | 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Lab1/os1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | 6 | pid_t pid=fork(); 7 | if(pid==0) 8 | { 9 | printf("Child Process created\n"); 10 | printf("Creating fibonacci series\nEnter n "); 11 | int n; 12 | scanf("%d",&n); 13 | if(n==1) 14 | printf("1\n"); 15 | else if(n==2) 16 | printf("1\t1\n"); 17 | else 18 | { 19 | printf("1\t1\t"); 20 | int f1=1,f2=1; 21 | int i,f3; 22 | for(i=3;i<=n;i++) 23 | { 24 | f3=f1+f2; 25 | printf("%d\t",f3); 26 | f1=f2; 27 | f2=f3; 28 | } 29 | printf("\n"); 30 | } 31 | } 32 | else{ 33 | wait(); 34 | printf("Parent process created\n");} 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Solutions/Lab1/os1_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | //printf("Hello"); 6 | pid_t pid=fork(); 7 | if(pid==0) 8 | { 9 | printf("Child Process created\n"); 10 | printf("Creating fibonacci series\nEnter n "); 11 | int n; 12 | scanf("%d",&n); 13 | if(n==1) 14 | printf("1\n"); 15 | else if(n==2) 16 | printf("1\t1\n"); 17 | else 18 | { 19 | printf("1\t1\t"); 20 | int f1=1,f2=1; 21 | int i,f3; 22 | for(i=3;i<=n;i++) 23 | { 24 | f3=f1+f2; 25 | printf("%d\t",f3); 26 | f1=f2; 27 | f2=f3; 28 | } 29 | printf("\n"); 30 | } 31 | } 32 | else{ 33 | wait(); 34 | printf("Parent process created\n");} 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Solutions/Lab1/os2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void mergesort(int a[],int i,int j); 4 | void merge(int a[],int i1,int j1,int i2,int j2); 5 | void mergesort(int a[],int i,int j) 6 | { 7 | int mid; 8 | 9 | if(iarray[pivotIndex]) 65 | { 66 | index2--; 67 | } 68 | 69 | if(index1 2 | #include 3 | #include 4 | int main() 5 | { 6 | 7 | pid_t pid=fork(); 8 | if(pid==0) 9 | { 10 | printf("Child Process created\n"); 11 | printf("Exiting\n"); 12 | exit(0); 13 | } 14 | else{ 15 | sleep(15); 16 | printf("Parent process\n"); 17 | 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/Lab2/pipe_fibo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | int n,a,b,c,pid; 7 | int fibo[100]; 8 | int pfd[2]; 9 | if(pipe(pfd)==-1) 10 | { 11 | printf("Error\n"); 12 | exit(0); 13 | } 14 | pid=fork(); 15 | if(pid>0) //parent 16 | { 17 | printf("\nParent Process");\ 18 | printf("\nFibonacci Series"); 19 | printf("\nEnter the limit for the series:"); 20 | scanf("%d",&n); 21 | close(pfd[0]); 22 | write(pfd[1],&n,sizeof(n)); 23 | close(pfd[1]); 24 | exit(0); 25 | } 26 | else //child 27 | { 28 | close(pfd[1]); 29 | read(pfd[0],&n,sizeof(n)); 30 | printf("\nChild Process"); 31 | a=0; 32 | b=1; 33 | close(pfd[0]); 34 | printf("\nFibonacci Series is:"); 35 | printf("\n%d\n%d",a,b); 36 | while(n>2) 37 | { 38 | c=a+b; 39 | printf("\n%d",c); 40 | a=b; 41 | b=c; 42 | n--; 43 | } 44 | } 45 | printf("\n"); 46 | return 0; 47 | }//main 48 | -------------------------------------------------------------------------------- /Solutions/Lab2/pipe_fibo2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | main() 6 | { 7 | pid_t pid; 8 | int pfd[2]; 9 | int i,j,flg,f1,f2,f3; 10 | static unsigned int ar[25],br[25]; 11 | if(pipe(pfd) == -1) 12 | { 13 | printf("Error in pipe"); 14 | exit(-1); 15 | } 16 | pid=fork(); 17 | if (pid == 0) 18 | { 19 | printf("Child process generates Fibonacci series\n"); 20 | f1 = -1; 21 | f2 = 1; 22 | for(i = 0;i < 25; i++) 23 | { 24 | f3 = f1 + f2; 25 | printf("%d\t",f3); 26 | f1 = f2; 27 | f2 = f3; 28 | ar[i] = f3; 29 | } 30 | write(pfd[1],ar,25*sizeof(int)); 31 | } 32 | else if (pid > 0) 33 | { 34 | wait(NULL); 35 | read(pfd[0], br, 25*sizeof(int)); 36 | printf("\nParent prints Fibonacci that are Prime\n"); 37 | for(i = 0;i < 25; i++) 38 | { 39 | flg = 0; 40 | if (br[i] <=1) 41 | flg = 1; 42 | for(j=2; j<=br[i]/2; j++) 43 | { 44 | if (br[i]%j == 0) 45 | { 46 | flg=1; 47 | break; 48 | } 49 | } 50 | if (flg == 0) 51 | printf("%d\t", br[i]);} 52 | printf("\n"); 53 | } 54 | else 55 | { 56 | printf("Process creation failed"); 57 | exit(-1); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Solutions/Lab3/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | int main() 8 | { 9 | char fname[50], buffer[1025]; 10 | int req, res, n; 11 | req = open("req.fifo", O_WRONLY); 12 | res = open("res.fifo", O_RDONLY); 13 | if(req < 0 || res < 0) { 14 | printf("Please Start the server first\n"); 15 | exit(1); 16 | } 17 | printf("Enter filename to request:\n"); 18 | scanf("%s", fname); 19 | write(req, fname, sizeof(fname)); 20 | printf("Received response\n"); 21 | while((n = read(res, buffer, sizeof(buffer)))>0) { 22 | write(1, buffer, n); 23 | } 24 | close(req); 25 | close(res); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/Lab3/hello: -------------------------------------------------------------------------------- 1 | hello123 2 | -------------------------------------------------------------------------------- /Solutions/Lab3/pipe2.c: -------------------------------------------------------------------------------- 1 | //Parent process requests child process 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | int pipe1fd[2],pipe2fd[2],n,file; 13 | char buffer[1024],fname[50]; 14 | if(pipe(pipe1fd)==-1 || pipe(pipe2fd)==-1 ) 15 | { 16 | printf("\nError in pipe connection\n"); 17 | exit(1); 18 | } 19 | if(!fork())//parent 20 | { 21 | close(pipe1fd[1]);//close write side of pipe 1 22 | printf("Enter filename to request:\n"); 23 | scanf("%s", fname); 24 | write(pipe2fd[1], fname, sizeof(fname));//write into pipe2 25 | printf("Received response\n"); 26 | while((n = read(pipe1fd[0], buffer, sizeof(buffer)))>0) { 27 | write(1, buffer, n);//1-standard output 28 | } 29 | close(pipe2fd[0]); 30 | } 31 | else 32 | { 33 | close(pipe1fd[0]); 34 | printf("Waiting for request...\n"); 35 | read(pipe2fd[0], fname, sizeof(fname));//read from pipe2 36 | printf("Received request for %s\n", fname); 37 | file = open(fname,O_RDONLY);//open file to read 38 | if (file < 0) 39 | write(pipe1fd[1], "File not found\n", 15); 40 | else { 41 | while((n = read(file, buffer, sizeof(buffer))) > 0) 42 | 43 | write(pipe1fd[1], buffer, n); 44 | 45 | } 46 | close(pipe2fd[1]); 47 | exit(0); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Solutions/Lab3/pipedup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | int pfds[2]; 7 | pipe(pfds); 8 | if (!fork())//parent process 9 | { 10 | close(1);//close standard output 11 | dup(pfds[1]);//duplicate write file descriptor 12 | close(pfds[0]);//close the read end of file 13 | execlp("who", "who", NULL); 14 | } 15 | 16 | else//child process 17 | { 18 | close(0);//close standard input 19 | dup(pfds[0]); 20 | close(pfds[1]); 21 | execlp("wc", "wc","-l", NULL); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Solutions/Lab3/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include /*used for file handling*/ 4 | #include /*used for mkfifo function*/ 5 | #include /*when compiled in gcc, mkfifo() has dependency on both 6 | types.h and stat.h*/ 7 | int main() 8 | { 9 | char fname[50], buffer[1025]; 10 | int req, res, n, file; 11 | mkfifo("req.fifo", 0777); 12 | mkfifo("res.fifo", 0777); 13 | printf("Waiting for request...\n"); 14 | req = open("req.fifo", O_RDONLY); 15 | res = open("res.fifo", O_WRONLY); 16 | read(req, fname, sizeof(fname)); 17 | printf("Received request for %s\n", fname); 18 | file = open(fname, O_RDONLY); 19 | if (file < 0) 20 | write(res, "File not found\n", 15); 21 | else { while((n = read(file, buffer, sizeof(buffer))) > 0) { 22 | write(res, buffer, n); 23 | } 24 | } 25 | close(req); 26 | close(res); 27 | unlink("req.fifo"); 28 | unlink("res.fifo"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/Lab4/chat1.c: -------------------------------------------------------------------------------- 1 | //sending message 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAXSIZE 128 9 | void die(char *s) 10 | { 11 | perror(s); 12 | exit(1); 13 | } 14 | struct msgbuf 15 | { 16 | long mtype; 17 | char mtext[MAXSIZE]; 18 | }; 19 | void main() 20 | { 21 | int msqid; 22 | struct msgbuf rcvbuffer; 23 | int msgflg=IPC_CREAT|0666; 24 | key_t key; 25 | struct msgbuf sbuf; 26 | size_t buflen; 27 | key=1234; 28 | int i; 29 | printf("HI"); 30 | 31 | 32 | if((msqid=msgget(key,msgflg))<0) 33 | { 34 | die("msgget"); 35 | } 36 | sbuf.mtype=1; 37 | for(i=0;i<5;i++) 38 | { 39 | printf("Enter a message to add to the message queue:\n"); 40 | scanf("%[^\n]",sbuf.mtext); 41 | getchar(); 42 | buflen=strlen(sbuf.mtext)+1; 43 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 44 | { 45 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 46 | die("msgsnd"); 47 | } 48 | else 49 | { 50 | printf("message sent\n"); 51 | } 52 | if((msqid=msgget(key,0666))<0) 53 | { 54 | die("msgget()"); 55 | } 56 | if(msgrcv(msqid,&rcvbuffer,MAXSIZE,1,0)<0) 57 | { 58 | die("msgrcv"); 59 | } 60 | else 61 | { 62 | printf("%s\n",rcvbuffer.mtext); 63 | } 64 | } 65 | exit(0); 66 | } 67 | -------------------------------------------------------------------------------- /Solutions/Lab4/chat2.c: -------------------------------------------------------------------------------- 1 | //receive 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAXSIZE 128 8 | void die(char *s) 9 | { 10 | perror(s); 11 | exit(1); 12 | } 13 | struct msgbuf 14 | { 15 | long mtype; 16 | char mtext[MAXSIZE]; 17 | }; 18 | void main() 19 | { 20 | struct msgbuf sbuf; 21 | size_t buflen; 22 | int msqid; 23 | key_t key; 24 | struct msgbuf rcvbuffer; 25 | key=1234; 26 | while(1) 27 | 28 | { 29 | if((msqid=msgget(key,0666))<0) 30 | { 31 | die("msgget()"); 32 | } 33 | sbuf.mtype=1; 34 | 35 | if(msgrcv(msqid,&rcvbuffer,MAXSIZE,1,0)<0) 36 | { 37 | die("msgrcv"); 38 | } 39 | printf("%s\n",rcvbuffer.mtext); 40 | //send 41 | printf("Enter a message to add to the message queue:\n"); 42 | scanf("%[^\n]",sbuf.mtext); 43 | getchar(); 44 | buflen=strlen(sbuf.mtext)+1; 45 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 46 | { 47 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 48 | die("msgsnd"); 49 | } 50 | else 51 | { 52 | printf("message sent\n"); 53 | } 54 | } 55 | exit(0); 56 | } 57 | -------------------------------------------------------------------------------- /Solutions/Lab4/chat_msgque.c: -------------------------------------------------------------------------------- 1 | #include /* For printf, scanf, etc...*/ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include /* For getpid() function */ 7 | #include /* For strcpy() function */ 8 | #define M_SIZE 80 /* Size of the Message */ 9 | 10 | typedef struct 11 | { 12 | long msg_to; 13 | long msg_from; 14 | char msg_content[M_SIZE]; 15 | } MESSAGE; 16 | 17 | int main() 18 | { 19 | key_t key; 20 | int mq_handle; 21 | struct msqid_ds *info; 22 | int ret; 23 | int mypid=getpid(); /* To uniquely identify ourselves */ 24 | MESSAGE msg,ret_msg; /* msg - messge to send. ret_msg - to retrieve message 25 | 26 | /* Create Key */ 27 | key=ftok(".",2234); 28 | printf("Key generated = %08X\n",key); 29 | 30 | /* Create message queue */ 31 | mq_handle=msgget(key,IPC_CREAT|0666); /* Create it if it doesn't exist */ 32 | if(mq_handle<=-1) 33 | { 34 | printf("Error creating message queue\n"); 35 | perror("MQ Sample"); /* print the error */ 36 | return(1); 37 | } 38 | printf("Message Queue created\n"); 39 | 40 | /* Obtain information */ 41 | ret=msgctl(mq_handle,IPC_STAT,info); 42 | if(ret==-1) 43 | { 44 | printf("Failed to obtain message queue information\n"); 45 | perror("MQ Sample"); 46 | exit(1); 47 | } 48 | printf("MQ Information:\n"); 49 | printf("Owner UID: %d\n",info->msg_perm.uid); 50 | printf("Owner GID: %d\n",info->msg_perm.gid); 51 | printf("# of bytes on queue: %d\n",info->msg_cbytes); 52 | printf("# of messages on queue: %d\n",info->msg_qnum); 53 | printf("# of bytes on queue: %d\n",info->msg_qbytes); 54 | 55 | /* Send a message */ 56 | msg.msg_to=mypid; /* send it to ourselves - look at the msgrcv call below*/ 57 | msg.msg_from=mypid; 58 | 59 | memset(msg.msg_content,0x0,M_SIZE); /* clear the msg buffer */ 60 | strcpy(msg.msg_content,"Testing the MQ"); 61 | ret=msgsnd(mq_handle,&msg,sizeof(msg),0); 62 | if(ret==-1) 63 | { 64 | perror("MQ Sample msgsend"); 65 | exit(1); 66 | } 67 | printf("Message sent\n"); 68 | 69 | /* Retrieve a message */ 70 | ret=msgrcv(mq_handle,&ret_msg,sizeof(ret_msg),mypid,0); 71 | if(ret==-1) 72 | { 73 | perror("MQ Sample msgrcv"); 74 | exit(1); 75 | } 76 | printf("Message received: %s\n",ret_msg.msg_content); 77 | 78 | /* Delete message queue */ 79 | msgctl(mq_handle,IPC_RMID,(struct msqid_ds *)0); 80 | printf("Message Queue deleted\n"); 81 | 82 | exit(1); 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Solutions/Lab4/msgque_rcv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define MAXSIZE 128 7 | void die(char *s) 8 | { 9 | perror(s); 10 | exit(1); 11 | } 12 | struct msgbuf 13 | { 14 | long mtype; 15 | char mtext[MAXSIZE]; 16 | }; 17 | void main() 18 | { 19 | int msqid; 20 | key_t key; 21 | struct msgbuf rcvbuffer; 22 | key=1234; 23 | if((msqid=msgget(key,0666))<0) 24 | { 25 | die("msgget()"); 26 | } 27 | if(msgrcv(msqid,&rcvbuffer,MAXSIZE,1,0)<0) 28 | { 29 | die("msgrcv"); 30 | } 31 | printf("%s\n",rcvbuffer.mtext); 32 | exit(0); 33 | } 34 | -------------------------------------------------------------------------------- /Solutions/Lab4/msgque_send.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAXSIZE 128 8 | void die(char *s) 9 | { 10 | perror(s); 11 | exit(1); 12 | } 13 | struct msgbuf 14 | { 15 | long mtype; 16 | char mtext[MAXSIZE]; 17 | }; 18 | void main() 19 | { 20 | int msqid; 21 | int msgflg=IPC_CREAT|0666; 22 | key_t key; 23 | struct msgbuf sbuf; 24 | size_t buflen; 25 | key=1234; 26 | if((msqid=msgget(key,msgflg))<0) 27 | { 28 | die("msgget"); 29 | } 30 | sbuf.mtype=1; 31 | printf("Enter a message to add to the message queue:\n"); 32 | scanf("%[^\n]",sbuf.mtext); 33 | //getchar(); 34 | buflen=strlen(sbuf.mtext)+1; 35 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 36 | { 37 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 38 | die("msgsnd"); 39 | } 40 | else 41 | { 42 | printf("message sent\n"); 43 | } 44 | exit(0); 45 | } 46 | -------------------------------------------------------------------------------- /Solutions/Lab4/send_msg.c: -------------------------------------------------------------------------------- 1 | //IPC_msgq_send.c 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define MAXSIZE 128 11 | 12 | 13 | struct msgbuf 14 | 15 | { 16 | 17 | long mtype; 18 | 19 | char mtext[MAXSIZE]; 20 | 21 | }; 22 | 23 | main() 24 | 25 | { 26 | imt msgflg=IPC_CREAT | 0666; 27 | key_t key=1234; 28 | int msqid=msgget(1234,IPC_CREAT); 29 | printf("Enter message to be sent: "); 30 | struct msgbuf sbuf; 31 | scanf("%[^\n]",sbuf.mtext); 32 | getchar(); 33 | 34 | size_t buflen; 35 | 36 | if(msqid<0) 37 | { 38 | printf("1"); 39 | printf("Failed\n"); 40 | exit(1); 41 | } 42 | //Message Type 43 | 44 | sbuf.mtype = 1; 45 | 46 | buflen=strlen(sbuf.mtext)+1; 47 | 48 | if (msgsnd(msqid, &sbuf,buflen , IPC_NOWAIT) < 0) 49 | { 50 | printf("%d %ld %s %d",msqid,sbuf.mtype,sbuf.mtext,buflen); 51 | printf("2"); 52 | printf ("Failed"); 53 | exit(1); 54 | } 55 | 56 | else 57 | { 58 | printf("Message Sent"); 59 | 60 | exit(1); 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Solutions/Lab5/chat1.c: -------------------------------------------------------------------------------- 1 | //sending message 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAXSIZE 128 9 | void die(char *s) 10 | { 11 | perror(s); 12 | exit(1); 13 | } 14 | struct msgbuf 15 | { 16 | long mtype; 17 | char mtext[MAXSIZE]; 18 | }; 19 | void main() 20 | { 21 | int msqid; 22 | struct msgbuf rcvbuffer; 23 | int msgflg=IPC_CREAT|0666; 24 | key_t key; 25 | struct msgbuf sbuf; 26 | size_t buflen; 27 | key=12; 28 | int i; 29 | //printf("HI"); 30 | 31 | 32 | if((msqid=msgget(key,msgflg))<0) 33 | { 34 | die("msgget"); 35 | } 36 | sbuf.mtype=1; 37 | strcpy(sbuf.mtext,"Are you hearing me?"); 38 | //printf("Enter a message to add to the message queue:\n"); 39 | //scanf("%[^\n]",sbuf.mtext); 40 | //getchar(); 41 | buflen=strlen(sbuf.mtext)+1; 42 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 43 | { 44 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 45 | die("msgsnd"); 46 | } 47 | else 48 | { 49 | //printf("message sent\n"); 50 | } 51 | getchar(); 52 | if((msqid=msgget(key,0666))<0) 53 | { 54 | die("msgget()"); 55 | } 56 | if(msgrcv(msqid,&rcvbuffer,MAXSIZE,1,0)<0) 57 | { 58 | die("msgrcv"); 59 | } 60 | else 61 | { 62 | printf("%s\n",rcvbuffer.mtext); 63 | } 64 | strcpy(sbuf.mtext,"I can hear you too"); 65 | //printf("Enter a message to add to the message queue:\n"); 66 | //scanf("%[^\n]",sbuf.mtext); 67 | getchar(); 68 | buflen=strlen(sbuf.mtext)+1; 69 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 70 | { 71 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 72 | die("msgsnd"); 73 | } 74 | else 75 | { 76 | //printf("message sent\n"); 77 | } 78 | exit(0); 79 | } 80 | -------------------------------------------------------------------------------- /Solutions/Lab5/chat2.c: -------------------------------------------------------------------------------- 1 | //receive 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAXSIZE 128 8 | void die(char *s) 9 | { 10 | perror(s); 11 | exit(1); 12 | } 13 | struct msgbuf 14 | { 15 | long mtype; 16 | char mtext[MAXSIZE]; 17 | }; 18 | void main() 19 | { 20 | struct msgbuf sbuf; 21 | size_t buflen; 22 | int msqid; 23 | key_t key; 24 | struct msgbuf rcvbuffer; 25 | key=12; 26 | sbuf.mtype=1; 27 | if((msqid=msgget(key,0666))<0) 28 | { 29 | die("msgget()"); 30 | } 31 | 32 | 33 | if(msgrcv(msqid,&rcvbuffer,MAXSIZE,1,0)<0) 34 | { 35 | die("msgrcv"); 36 | } 37 | printf("%s\n",rcvbuffer.mtext); 38 | //send 39 | strcpy(sbuf.mtext, "Loud and clear!"); 40 | //printf("Enter a message to add to the message queue:\n"); 41 | //scanf("%[^\n]",sbuf.mtext); 42 | //getchar(); 43 | buflen=strlen(sbuf.mtext)+1; 44 | if(msgsnd(msqid,&sbuf,buflen,IPC_NOWAIT)<0) 45 | { 46 | printf("%d ,%d ,%s ,%d\n",msqid,sbuf.mtype,sbuf.mtext,buflen); 47 | die("msgsnd"); 48 | } 49 | else 50 | { 51 | //printf("message sent\n"); 52 | } 53 | 54 | exit(0); 55 | } 56 | -------------------------------------------------------------------------------- /Solutions/Lab5/shared_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | 10 | { 11 | 12 | int shmid; 13 | 14 | int *shm; 15 | 16 | key_t key=8765; //= IPC_PRIVATE; 17 | 18 | int flag; //= SHM_R | SHM_W; 19 | 20 | /*if (argc >= 2) { 21 | 22 | key = atoi(argv[1]); 23 | 24 | }*/ 25 | 26 | shmid = shmget(key, 1000, IPC_CREAT | 0666); 27 | 28 | printf("shared memory for key %d: %d\n", key, shmid); 29 | 30 | if (shmid < 0) { 31 | printf("\nError in getting shared memory"); 32 | //perror("shmget"); 33 | 34 | printf("\nTry to create this segment\n"); 35 | 36 | shmid = shmget(key, 1000, IPC_CREAT | 0666); 37 | 38 | if (shmid < 0) { 39 | printf("Error again\n"); 40 | //perror("shmget | IPC_CREAT"); 41 | exit(1); 42 | } 43 | 44 | } 45 | 46 | shm = (int *)shmat(shmid, NULL, /*flag*/0); 47 | 48 | if ((int)shm ==-1) { 49 | 50 | //perror("shmat"); 51 | printf("Error in attaching\n"); 52 | exit(1); 53 | 54 | } 55 | 56 | sprintf(shm, "hello world\n"); 57 | 58 | printf("shared memory content: %s\n", shm); 59 | 60 | sleep(10); 61 | 62 | return 0; 63 | 64 | } /* main */ 65 | --------------------------------------------------------------------------------