├── .gitattributes ├── Lab1 ├── How to create Shared Folder.docx ├── Input File.txt ├── Task 1 solution.rar ├── Task 2 solution.cpp ├── task 1.docx └── task 2.docx ├── Lab2 ├── Lab 2.pdf ├── Lab2_Q1_solution.pdf └── Lab2_Q2_Solution.pdf ├── Lab3 ├── Lab 3.docx ├── Makefile ├── file.txt ├── lab.cpp └── pipe.cpp ├── Lab4 ├── Lab 4.docx ├── Makefile ├── PostLab solution.cpp ├── Question1.cpp ├── Question2.cpp └── input.txt ├── Lab5 ├── Lab 5.docx ├── Makefile ├── Q1.cpp ├── Q2.cpp └── Shared Memory.docx ├── Lab7 ├── client.cpp └── server.cpp ├── Lab8 ├── Q1.c ├── Q2.c └── a.out ├── README.md └── change text color.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Lab1/How to create Shared Folder.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab1/How to create Shared Folder.docx -------------------------------------------------------------------------------- /Lab1/Input File.txt: -------------------------------------------------------------------------------- 1 | Welcome 2 to 3 OS lab 1 El205 2 | 3 | 4 | -------------------------------------------------------------------------------- /Lab1/Task 1 solution.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab1/Task 1 solution.rar -------------------------------------------------------------------------------- /Lab1/Task 2 solution.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab1/Task 2 solution.cpp -------------------------------------------------------------------------------- /Lab1/task 1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab1/task 1.docx -------------------------------------------------------------------------------- /Lab1/task 2.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab1/task 2.docx -------------------------------------------------------------------------------- /Lab2/Lab 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab2/Lab 2.pdf -------------------------------------------------------------------------------- /Lab2/Lab2_Q1_solution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab2/Lab2_Q1_solution.pdf -------------------------------------------------------------------------------- /Lab2/Lab2_Q2_Solution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab2/Lab2_Q2_Solution.pdf -------------------------------------------------------------------------------- /Lab3/Lab 3.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab3/Lab 3.docx -------------------------------------------------------------------------------- /Lab3/Makefile: -------------------------------------------------------------------------------- 1 | final: 2 | g++ lab.cpp -o lab 3 | clear 4 | ./lab 5 | -------------------------------------------------------------------------------- /Lab3/file.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab3/file.txt -------------------------------------------------------------------------------- /Lab3/lab.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | bool isSpecialChar(char match){ // check is given character is a special character or not 12 | if(match == '&'){ 13 | return 1; 14 | } 15 | else if(match == '@'){ 16 | return 1; 17 | } 18 | else if(match == '#'){ 19 | return 1; 20 | } 21 | else if(match == '%'){ 22 | return 1; 23 | } 24 | else if(match == '*'){ 25 | return 1; 26 | } 27 | else if(match == '?'){ 28 | return 1; 29 | } 30 | else if(match == '$'){ 31 | return 1; 32 | } 33 | else if(match == '"'){ 34 | return 1; 35 | } 36 | else if(match == '~'){ 37 | return 1; 38 | } 39 | else if(match == '"'){ 40 | return 1; 41 | } 42 | else { 43 | return 0; 44 | } 45 | } 46 | 47 | int arrSize(char*str){ // calculate string size 48 | int size = 0; 49 | for(; str[size]!='\0'; size++); 50 | return size; 51 | } 52 | 53 | void removeSpecialChar(char * str){ // remove special characters from the given array 54 | int size = arrSize(str) + 1; 55 | for(int i = 0; i<= size; i++){ 56 | if(isSpecialChar(str[i])){ 57 | for(int j = i; j <= size; j++){ 58 | str[j] =str[j+1]; 59 | } 60 | i--; 61 | } 62 | } 63 | } 64 | 65 | 66 | void readDataFromFd(int inputFd, char *str){ // read data from fd character byu character 67 | char tempChar = ' '; 68 | int i = 0; 69 | for(; tempChar !='\n' ;i++){ 70 | read(inputFd,&tempChar,1); // read from file 71 | str[i] = tempChar; 72 | } 73 | str[i] ='\0'; 74 | 75 | } 76 | 77 | int main(){ 78 | 79 | char inputFile[15] = "file.txt"; 80 | char outputFile[15] = "output.txt"; 81 | char input[1000];//use to read data from file 82 | int pipeFd[2]; //Child to parent pipe 83 | int pToCFd[2]; //parent to child pipe 84 | pipe(pipeFd); 85 | pipe(pToCFd); 86 | 87 | int inputFd = open(inputFile, O_RDONLY);//open input file just for reading 88 | if(inputFd == -1){ // retrun 1 if input file not open 89 | return 1; 90 | } 91 | 92 | readDataFromFd(inputFd,input);// read from file 93 | write(pToCFd[1],input,arrSize(input)); // write in pipe 94 | close(pToCFd[1]); 95 | 96 | /*--------------------------------------------- 97 | ----CREAT CHILD AND PARENT PROCESS BY FORK----- 98 | -----------------------------------------------*/ 99 | 100 | int forkId = fork(); 101 | if(forkId == -1){ // child process not created 102 | return 4; 103 | } 104 | wait(NULL); 105 | 106 | if(forkId == 0){ // child process 107 | char localStr[1000]; 108 | close(pipeFd[0]); 109 | close(pToCFd[1]); 110 | readDataFromFd(pToCFd[0],localStr);// read string from pipe 111 | removeSpecialChar(localStr); // remove special char 112 | 113 | //insert into pipe 114 | write(pipeFd[1],localStr, arrSize(localStr)); 115 | 116 | close(pipeFd[1]); 117 | } 118 | else if(forkId>0){ // parent process 119 | wait(NULL); 120 | char localStr[1000]; 121 | close(pipeFd[1]); 122 | 123 | readDataFromFd(pipeFd[0],localStr);// read string from pipe 124 | //remove(outputFile); 125 | //creat and open file for reading purpose 126 | if(creat(outputFile, S_IRWXU) == -1){ // return 2 if creat file process not sucessfully process 127 | return 2; 128 | } 129 | 130 | 131 | int outputFd = open(outputFile,O_WRONLY); 132 | if(outputFd == -1){ //return 3 if output file not open 133 | return 3; 134 | } 135 | 136 | //enter data in output file; 137 | write(outputFd,localStr,arrSize(localStr)); 138 | wait(NULL); 139 | } 140 | return 0; 141 | } 142 | -------------------------------------------------------------------------------- /Lab3/pipe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int fd1[2]; 11 | pipe(fd1); 12 | int cpid=fork(); 13 | if(cpid==0) 14 | { 15 | //child is write process 16 | 17 | close(fd1[0]); 18 | char message[] ="hello parent \n"; 19 | int w= write(fd1[1],message, strlen(message)); 20 | _exit(0); 21 | 22 | } 23 | else if(cpid>0) 24 | { 25 | //parent is read process 26 | cout<<"message sent from child process"< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | struct command{ 8 | char*cmd_name; 9 | char** argv; 10 | command(){ 11 | cmd_name = new char[50]; 12 | argv = new char*[4]; 13 | argv[3] = NULL; 14 | } 15 | void setCmd_command(string str){ 16 | for(int i = 0; str[i] != '\0'; i++){ 17 | cmd_name[i] = str[i]; 18 | } 19 | } 20 | ~command(){ 21 | if(cmd_name!=NULL) delete []cmd_name; 22 | for(int i = 0; i< 4; i++){ 23 | if(argv[i]!=NULL) delete []argv[i]; 24 | } 25 | } 26 | }; 27 | 28 | char* strToArr(string str2){ 29 | char * temp = new char[50]; 30 | int i = 0; 31 | for(; str2[i] != '\0'; i++){ 32 | temp[i] = str2[i]; 33 | } 34 | temp[i] = '\0'; 35 | return temp; 36 | } 37 | 38 | 39 | void creat2Folder(){//it will creat 2 folders with name "myFolder_1" nad "my_Folder2"; 40 | command exeCommand; 41 | exeCommand.setCmd_command("mkdir"); 42 | exeCommand.argv[0] = strToArr("./mkdir"); 43 | exeCommand.argv[1] = strToArr("./myFolder_1"); 44 | exeCommand.argv[2] = strToArr("./my_Folder1"); 45 | execvp(exeCommand.cmd_name,exeCommand.argv); 46 | 47 | } 48 | void creatSubFolder(){ // it will creat sub folder in "myFolder_1" 49 | command exeCommand; 50 | exeCommand.setCmd_command("mkdir"); 51 | exeCommand.argv[0] = strToArr(".mkdir"); 52 | exeCommand.argv[1] = strToArr("./my_Folder1/fazedsadel");; 53 | execvp(exeCommand.cmd_name,exeCommand.argv); 54 | } 55 | void executeLS(){ // it will execute LS command with argument -S 56 | command exeCommand; 57 | exeCommand.setCmd_command("ls"); 58 | exeCommand.argv[0] = strToArr("-S"); 59 | exeCommand.argv[1] = NULL; 60 | execvp(exeCommand.cmd_name,exeCommand.argv); 61 | } 62 | int main(){ 63 | pid_t fFID;// first Fork ID 64 | fFID = fork(); // creat child and parent processes 65 | 66 | if(fFID==0){ // first child process 67 | cout<<"\nEnter in first child process"; 68 | cout<<"\ntwo folder created"; 69 | cout<<"\n\n\n"; 70 | creat2Folder(); 71 | } 72 | 73 | else if(fFID>0){ // PARENT PROCESS 74 | pid_t fSID = fork(); 75 | 76 | if(fSID == 0){ // second child 77 | cout<<"\nEnter in second child process"; 78 | cout<<"\nSub folder created"; 79 | cout<<"\n\n\n"; 80 | creatSubFolder(); 81 | } 82 | else if(fSID>0) {// parent process 83 | wait(NULL); 84 | pid_t fTID = fork(); // third child; 85 | if(fTID == 0 ){ // third child 86 | cout<<"\nEnter in third child process"; 87 | cout<<"\nExecute Ls command with argument -S"; 88 | cout<<"\n\n\n"; 89 | executeLS(); 90 | } 91 | } 92 | } 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /Lab4/Question1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main(void) { 12 | char characters; 13 | 14 | int iFds = open("./input.txt", O_RDONLY); // inputFD 15 | int oFds = open("./output.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); //outputFD 16 | 17 | close(1); 18 | dup(oFds); 19 | 20 | while(read(iFds, &characters,1)){ 21 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | struct command_t{ 10 | char** ptr; 11 | char** pCmd; // hold array parse by space 12 | char** pPipe; //hold array parse by pipe symbol 13 | char** pOut; // hold array parse by output operator 14 | command_t(){ 15 | ptr = NULL; 16 | pCmd = NULL; 17 | pPipe = NULL; 18 | pOut = NULL; 19 | } 20 | ~command_t(){ 21 | for(int i = 0;i< 3; i++){ 22 | if(ptr!=NULL){ 23 | delete[]ptr; 24 | } 25 | } 26 | for(int i = 0;i< 10; i++){ 27 | if(pCmd!=NULL){ 28 | delete[]pCmd; 29 | } 30 | } 31 | for(int i = 0;i< 10; i++){ 32 | if(pPipe!=NULL){ 33 | delete[]pPipe; 34 | } 35 | } 36 | for(int i = 0; i< 10; i++){ 37 | if(pOut!=NULL){ 38 | delete[]pOut; 39 | } 40 | } 41 | } 42 | }; 43 | using namespace std; 44 | int countPipeSymbol(char * str){ 45 | int count = 0;//count pipe symbol occurance 46 | for(int i = 0; str[i] != '\0'; i++){ 47 | if(str[i] == '|'){ 48 | count ++; 49 | } 50 | } 51 | return count; 52 | } 53 | 54 | char ** parsePipe(char *str){ 55 | char **ptr = new char*[3]; 56 | int ptrIndex = 0; // control ptr index 57 | 58 | if(countPipeSymbol(str) == 0){ // if pipe symbol not present 59 | return NULL; 60 | } 61 | else { 62 | int i = 0; // control str array index 63 | for(; i< str[i]!= '\0'; i++){ 64 | char *arr = new char[20]; 65 | int arrIndex = 0; // control arr array index 66 | bool isRun = false; 67 | while( str[i] != '\0' && str[i] != '|') { 68 | arr[arrIndex] = str[i]; 69 | arrIndex ++; 70 | isRun = true; 71 | i++; 72 | } 73 | arr[arrIndex] = '\0'; 74 | if(isRun){ 75 | ptr[ptrIndex] = arr; 76 | ptrIndex++; 77 | } 78 | } 79 | ptr[ptrIndex] = NULL; 80 | return ptr; 81 | } 82 | } 83 | 84 | char** parseSpace(char*str){ 85 | char **ptr = new char*[10]; 86 | int ptrIndex = 0; // control ptr index 87 | 88 | int i = 0; // control str array index 89 | for(; i< str[i]!= '\0'; i++){ 90 | char *arr = new char[20]; 91 | int arrIndex = 0; // control arr array index 92 | bool isRun = false; 93 | 94 | while( str[i] != '\0' && str[i] != ' ') { 95 | arr[arrIndex] = str[i]; 96 | arrIndex ++; 97 | isRun = true; 98 | i++; 99 | } 100 | 101 | arr[arrIndex] = '\0'; 102 | if(isRun){ 103 | ptr[ptrIndex] = arr; 104 | ptrIndex++; 105 | } 106 | } 107 | ptr[ptrIndex] = NULL; 108 | return ptr; 109 | 110 | } 111 | 112 | char** parseOut(char* str){ // parse by output operator 113 | char **ptr = new char*[10]; 114 | int ptrIndex = 0; // control ptr index 115 | 116 | int i = 0; // control str array index 117 | for(; i< str[i]!= '\0'; i++){ 118 | char *arr = new char[20]; 119 | int arrIndex = 0; // control arr array index 120 | bool isRun = false; 121 | 122 | while( str[i] != '\0' && str[i] != '>') { 123 | arr[arrIndex] = str[i]; 124 | arrIndex ++; 125 | isRun = true; 126 | i++; 127 | } 128 | 129 | arr[arrIndex] = '\0'; 130 | if(isRun){ 131 | ptr[ptrIndex] = arr; 132 | ptrIndex++; 133 | } 134 | } 135 | ptr[ptrIndex] = NULL; 136 | return ptr; 137 | 138 | } 139 | 140 | void executePipe(char** parsed, char** parsedpipe) { 141 | int pipefd[2]; 142 | pipe(pipefd); // pipe initialzed 143 | 144 | pid_t fd1 = fork(); 145 | if (fd1 < 0) { 146 | cout<<"\nerror occur during fork"; 147 | return; 148 | } 149 | 150 | if (fd1 == 0) { 151 | close(pipefd[0]); 152 | dup2(pipefd[1], STDOUT_FILENO); 153 | close(pipefd[1]); 154 | if (execvp(parsed[0], parsed) < 0) { 155 | for(char **temp = parsed; temp!=NULL; temp++){ 156 | cout<<*temp; 157 | } 158 | cout<<": Command not execute....."; 159 | exit(0); 160 | } 161 | } 162 | else if(fd1 > 0){ //parent process. Here we will execute pipe 163 | pid_t fd2 = fork(); 164 | 165 | if (fd1 < 0) { 166 | cout<< "\nerror occur during fork"; 167 | return; 168 | } 169 | 170 | if (fd2 == 0) { 171 | close(pipefd[1]); 172 | dup2(pipefd[0], STDIN_FILENO); 173 | close(pipefd[0]); 174 | 175 | if (execvp(parsedpipe[0], parsedpipe) < 0) { 176 | for(char **temp = parsed; temp!=NULL; temp++){ 177 | cout<<*temp; 178 | } 179 | cout<<": Command not execute....."; 180 | exit(0); 181 | } 182 | } 183 | else { 184 | wait(NULL); 185 | wait(NULL); 186 | } 187 | } 188 | } 189 | int main(){ 190 | command_t command; 191 | char inputCommand[50] = "man ls | grep ls > file.txt"; 192 | command.pOut = parseOut(inputCommand); // pars output symbol ">" 193 | 194 | int oFds = open(command.pOut[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); //open file in writing mode 195 | close(1); 196 | dup(oFds); 197 | command.ptr = parsePipe(command.pOut[0]); 198 | 199 | if(command.ptr !=NULL){ // if pipe exist in input command 200 | command.pCmd = parseSpace(command.ptr[0]); 201 | executePipe(command.pCmd,command.pPipe); 202 | } 203 | else{ 204 | cout<<"Pipe symbol not present \n"; 205 | } 206 | 207 | close(oFds); 208 | 209 | close(1); 210 | return 0; 211 | } -------------------------------------------------------------------------------- /Lab4/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 3 | 10 4 | 5 | 15 6 | 7 | 20 8 | 3 9 | 4 10 | 55 11 | 66 12 | 777 13 | 34 14 | 23 15 | 45 16 | 66 17 | 78 18 | -------------------------------------------------------------------------------- /Lab5/Lab 5.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab5/Lab 5.docx -------------------------------------------------------------------------------- /Lab5/Makefile: -------------------------------------------------------------------------------- 1 | final: 2 | clear 3 | g++ Q1.cpp -o Q1.exe 4 | ./Q1.exe 5 | g++ Q2.cpp -o Q2.exe 6 | ./Q2.exe 7 | 8 | -------------------------------------------------------------------------------- /Lab5/Q1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | key_t key = 9876; 13 | 14 | int main(){ 15 | int shmId; 16 | 17 | pid_t fid = fork(); // creat child and parent process 18 | if(fid == -1){ 19 | perror("fork"); 20 | return 0; 21 | } 22 | if(fid == 0){ //child Process 23 | cout<<"\nChild Process \n"; 24 | shmId = shmget(key, sizeof(int) * 5, 0666 | IPC_CREAT); 25 | int *data = (int *)shmat(shmId, (void*)0,0); 26 | 27 | for(int i = 0; i< 5; i++){ 28 | cout<<"Enter number in "<>data[i]; 30 | } 31 | } 32 | else{ 33 | wait(NULL); // wait untill child process not terminate 34 | cout<<"\nparent Process"; 35 | shmId = shmget(key, sizeof(int) * 5, 0666 | IPC_CREAT); 36 | int *data = (int *)shmat(shmId, (void*)0,0); 37 | int *temp = data; 38 | 39 | for(int i = 0; i< 5; i++, temp++ ){ 40 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | void convertCase(char*str){ 9 | for(int i = 0;str[i] != '\0'; i++){ 10 | if(str[i]>='A' && str[i]<='Z') { 11 | str[i] +=32; // if character in upper case add 32 12 | } 13 | else if(str[i]>='a' && str[i]<='z') { //if case in lower case negative 32 14 | str[i] -=32; 15 | } 16 | } 17 | } 18 | 19 | int main() { 20 | int pipe1[2]; // parent can write but child only can read 21 | int pipe2[2]; // parent can read but child only can write 22 | char str[100]; 23 | 24 | cout<<"Enter string : "; // take input from user 25 | cin.getline(str,100); 26 | 27 | if (pipe(pipe1) == -1) { 28 | perror("pipe"); 29 | return 1; 30 | } 31 | 32 | if (pipe(pipe2) == -1) { 33 | perror("pipe"); 34 | return 1; 35 | } 36 | 37 | pid_t pid = fork(); 38 | 39 | if(pid == 0){ // child process call 40 | close(pipe1[1]); // Close the writing end of pipe1 41 | close(pipe2[0]); // Close the reading end of pipe2 42 | cout<<"\nchild Process\n"; 43 | char receiveMessage[100]; 44 | read(pipe1[0], receiveMessage, 100); 45 | convertCase(receiveMessage); // reverse each case 46 | write(pipe2[1], receiveMessage, sizeof(receiveMessage)); 47 | } 48 | else{ // Parent process 49 | char finalAnswer[100]; 50 | close(pipe1[0]); // Close the reading end of pipe1 51 | close(pipe2[1]); // Close the writing end of pipe2 52 | cout<<"\nParentProcess\n"; 53 | write(pipe1[1], str, sizeof(str));// writting in pipe1 54 | read(pipe2[0], finalAnswer, sizeof(finalAnswer)); // reading from pipe2; 55 | cout<<"final answer is :"< 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | bool isDollarSignPresent(char* arr){ 9 | for(int i = 0; arr[i]!+'\0'; i++){ 10 | if(arr[i] == '$'){ 11 | arr[i] = '\0'; 12 | return 1; 13 | } 14 | } 15 | return false; 16 | } 17 | 18 | 19 | int main() { 20 | char *buffer; 21 | 22 | int id_sem=shmget(990, 1024, 0); 23 | int id_cs= shmget(991, 1024, 0); 24 | 25 | sem_t *semaphore; 26 | 27 | if (id_sem==-1 || id_cs==-1) 28 | { 29 | cout<<"ERROR!"; 30 | return 1; 31 | } 32 | semaphore= (sem_t*) shmat(id_sem, NULL, 0); 33 | buffer= (char*) shmat(id_cs, NULL, 0); 34 | 35 | bool is_terminated = false; 36 | while(!is_terminated ){ 37 | int writeFd = open("output.txt", O_WRONLY); 38 | sem_wait(semaphore); 39 | 40 | is_terminated = isDollarSignPresent(buffer); 41 | write(writeFd,buffer,1); 42 | sem_post(semaphore); 43 | } 44 | shmdt(semaphore); 45 | shmdt(buffer); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Lab7/server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | 16 | 17 | char *buffer; 18 | 19 | int id_sem=shmget(990, 1024, 0666 | IPC_CREAT | IPC_EXCL); 20 | int id_cs= shmget(991, 1024, 0666| IPC_CREAT | IPC_EXCL); 21 | 22 | sem_t *semaphore; 23 | 24 | if (id_sem==-1 || id_cs==-1) 25 | { 26 | 27 | cout<<"ERROR!"; 28 | return 1; 29 | 30 | } 31 | 32 | int readFd = open("input.txt", O_RDONLY); 33 | char * readData= new char(2); 34 | 35 | semaphore= (sem_t*) shmat(id_sem, NULL, 0); 36 | buffer= (char*) shmat(id_cs, NULL, 0); 37 | sem_init(semaphore, 1, 0); //1 means semaphore is used for process synchronization 38 | 39 | while(*readData!='$'&& *readData!='\n'){ 40 | char temp[21]; 41 | int i = 0; 42 | 43 | for(; i< 20 && *readData!='$'; i++){ 44 | read(readFd,readData,1); 45 | temp[i] = readData[0]; 46 | } 47 | temp[i] = '\0'; 48 | strcpy(buffer, temp); 49 | char a; 50 | cout<<"Press any key to continue: "; 51 | cin>>a; 52 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | // initialization of sempahores 7 | sem_t semaphore[3]; 8 | pthread_t thread[3]; 9 | 10 | void* thread1(void* arg); 11 | void* thread2(void* arg); 12 | void* thread3(void* arg); 13 | 14 | int main() { 15 | for(int i = 0; i< 3; i++){ //init semaphore 16 | sem_init(&semaphore[i], 0, 1); 17 | } 18 | 19 | pthread_create(&thread[0],NULL,thread1,NULL); 20 | pthread_create(&thread[1],NULL,thread2,NULL); 21 | pthread_create(&thread[2],NULL,thread3,NULL); 22 | 23 | for(int i = 0; i< 3; i++){ // join thread 24 | pthread_join(thread[i],NULL); 25 | } 26 | 27 | for(int i = 0; i< 3; i++){ // destory semaphore 28 | sem_destroy(&semaphore[i]); 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | 35 | // thread 1 36 | void* thread1(void* arg) { 37 | while(1) { 38 | sem_post(&semaphore[2]); 39 | printf("aaa"); 40 | sem_wait(&semaphore[1]); 41 | } 42 | } 43 | 44 | // thread 2 45 | void* thread2(void* arg) { 46 | 47 | while(1){ 48 | sem_wait(&semaphore[2]); 49 | printf("c"); 50 | 51 | sem_post(&semaphore[0]); 52 | } 53 | } 54 | void* thread3(void* arg) { 55 | while(1){ 56 | sem_wait(&semaphore[0]); 57 | printf("b"); 58 | sem_post(&semaphore[1]); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Lab8/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | sem_t semaphore[2]; 7 | 8 | int buffer[100]; 9 | int bufferIndex = 0; 10 | int sum = 0; 11 | int producer_exit = 0; 12 | void * Producer(void * arg); 13 | void * Consumer(void * arg); 14 | 15 | int main() 16 | { 17 | 18 | sem_init(&semaphore[0],0,1); 19 | sem_init(&semaphore[1],0,1); 20 | 21 | pthread_t thread[2]; 22 | 23 | srand(time(NULL)); 24 | pthread_create(&thread[0],NULL,&Producer,NULL); 25 | pthread_create(&thread[1],NULL,&Consumer,NULL); 26 | 27 | pthread_join(thread[0],NULL); 28 | pthread_join(thread[1],NULL); 29 | 30 | sem_destroy(&semaphore[0]); 31 | sem_destroy(&semaphore[1]); 32 | return 0; 33 | } 34 | 35 | 36 | void * Producer(void * arg) { 37 | 38 | for(int i = 0; bufferIndex != 100; i++) { 39 | int j = 0; 40 | 41 | //sem_post(&semaphore2); 42 | for(; j < rand()%9 + 1 && bufferIndex<100; j++){ 43 | buffer[bufferIndex]=rand()%6 + 1; // it will generate number from 1 to 6 44 | printf("index: %d \t Number : %d \n",bufferIndex,buffer[bufferIndex]); 45 | bufferIndex++; 46 | } 47 | sem_post(&semaphore[1]); 48 | sem_wait(&semaphore[0]); 49 | } 50 | producer_exit = 1; 51 | sem_wait(&semaphore[0]); 52 | 53 | } 54 | void * Consumer(void * arg) { 55 | 56 | for(int i = 0; i< 100; i++){ 57 | 58 | sem_wait(&semaphore[1]); 59 | 60 | if(!producer_exit){ 61 | sum += buffer[i]; 62 | printf(" Sum : %d \n",sum); 63 | } 64 | else{ 65 | 66 | printf("\n\nProducer end\n\n"); 67 | while(i<100){ 68 | sum += buffer[i]; 69 | printf(" Sum : %d \n",sum); 70 | i++; 71 | } 72 | 73 | } 74 | sem_post(&semaphore[0]); 75 | } 76 | } -------------------------------------------------------------------------------- /Lab8/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Operating-System-real-life-problems-and-solution/032028335285a0bf4f0eb91aecabac8a395ce3c6/Lab8/a.out -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Operating-System-real-life-problems-and-solution 2 | 3 | 4 |

LAB - 1

5 | 6 |

Task1 Question 1:

7 | 8 | See the usage of the following commands online. Also run them on the terminal.
9 | 1. pwd
10 | 2. ls
11 | 3. cd
12 | 4. cp
13 | 5. mkdir & rmdir
14 | 6. man
15 | 7. sudo
16 | 8. apt-get
17 | 9. kill
18 | 10. ping
19 | 11. grep
20 | 12. mount
21 | 13. unmount
22 | 23 | 24 |

Task1 Question 2:

25 | a. Create a function removeNonAlphabets(char * inputFileName, char * outputFileName) in C or C++ that is passed as parameters: an input file name and an output file name. The function then reads the input file using read system call and removes all non-alphabets. It then writes the data to output file using write system call. You will need to see open, read, write, and close system calls. https://www.geeksforgeeks.org/input-output-system-calls-c-create-open-close-read-write/
26 | b. Place the signature of the function in a header file named util.h, and place the implementation of the function in a cpp file named util.cpp
27 | c. Now create a main.cpp file containing the main function which calls the function created previously. (For input file, create a random text file containing alphanumeric characters)
28 | 29 | 30 |

Task2:

31 | Write a C++ which perform following tasks.
32 | Create a parent and child process using the fork command where the child tells if the number is odd or not and parent process will write the number on screen.
33 | 34 |
35 |
36 |
37 | 38 |

LAB - 2

39 | 40 |

Question 1:

41 | Write a C++ which performs following tasks.
42 | Create a parent and child process using the fork command where the child process will calculate
43 | the first 15 numbers in Fibonacci series and parent process will write those numbers on screen
44 | 45 |

Question 2:

46 | Create a program named stat that takes an integer array as command line argument (deliminated 47 | by some character such as $). The program then creates 3 child processes each of which does 48 | exactly one task from the following: 49 | 50 | 1. Adds them and print the result on the screen. (Done by child 1) 51 | 2. Shows average on the screen. (Done by child 2) 52 | 3. Prints the maximum number on the screen. (Done by child 3) -------------------------------------------------------------------------------- /change text color.txt: -------------------------------------------------------------------------------- 1 | void blueColor(){ 2 | printf("\033[0;34m"); 3 | } 4 | void greenColor(){ 5 | printf("\033[0;32m"); 6 | } 7 | 8 | void redColor(){ 9 | printf("\033[0;31m"); 10 | } 11 | 12 | void resetColor() { 13 | printf("\033[0m"); 14 | } 15 | 16 | //change text size 17 | void boldText(){ 18 | printf("\033[1m"); 19 | } 20 | 21 | void resetText(){ 22 | printf("\\033[22m"); 23 | } 24 | 25 | 26 | --------------------------------------------------------------------------------