





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
operating system midterm solutions
Typology: Cheat Sheet
1 / 9
This page cannot be seen from the preview
Don't miss anything!
______________________________________________________________________________________________________________
1. [ 20 points] Answer the following questions with short explanations. You can also draw figures to better explain your reasoning if needed.
a. [2pt] What are the goals of Operating System? Briefly explain/discuss.
Convenience: Make the computer convenient to use for general user and programmers.. Efficiency: Manage system resources in an efficient manner
b. [5pt] What are the two key mechanisms to interact with the Operating System kernel (1pt)? And explain how they work (each 2pt)?
System Calls Interrupts
e. [4pt] There are two types of links in Unix/Linux: Hard and Symbolic/Soft links. Using diagrams explain the difference between them.
Hard link : A hard link just creates another file (a new entry in directory) with a link to the same underlying inode. Symbolic/Soft link: link to another filename in the file system
f. [ 5pt ] Suppose the following code sections are executed without any error. Draw the diagrams showing the relationship between the file descriptors, pipes and processes (first one is given).
Code section Diagram after the execution of the code section int fda[2], fdb[2]; pipe(fda); dup2(fda[0], STDIN_FILENO); dup2(fda[1], STDOUT_FILENO); close(fda[0]); close(fda[1]);
pipe(fdb); // 2pt
fork(); // 3pt
2. [ 20 points ] Program and Process, command-line arguments, static keyword , ... a. [ 10 points ] You are asked to implement a function char *next_label(); whose consecutive calls will return labels like "Fig. 1", "Fig. 2", and so on. One possible solution is given below. However, it is not safe (e.g., when the second label is generated the first one is overwritten). Also it uses a global variable, which might be changed in other parts of the program. Re-implement this function by avoiding the use of a global variable and making it thread safe.
b. [ 10 pt ] Show the relationship among the process created in the following program and give at least
4. [ 20 points ] Unix I/O and file operations a. [ 10 points ] Suppose the following code is executed correctly without generating any errors, and parent's PID is 7 while child's PID is 8. main(){ fprintf( stdout , "%d: a ", getpid()); fprintf(stderr, "%d: a has been written \n", getpid()); fprintf( stdout , "%d: b \n", getpid()); fprintf(stderr, "%d: b has been written \n", getpid()); fprintf( stdout , "%d: c ", getpid()); fork( ); fprintf( stdout , "%d: all done! \n", getpid()); return 0; }
Give a possible output for the above program. 7: a has been written 7: a 7: b 7: b has been written 7: c 7: all done 7: c 8: all done
Last two lines might change
b. [ 10 points ] Consider the given INODE structure and assume that block size is 8K bytes and pointers are 4 bytes. So each block has num_ptr = 81024/4* pointers! You are asked to implement *char get_blk_n_di(struct inode myinode, int n); which returns the address of the nth^ double indirect (di) data block (if it exists); otherwise, it returns NULL. For the first di data block (if exists), n is 0. When n is 0, return myinode->di[0][0]; For the last di data block (if exists), n is num_ptrnum_ptr-1. In this case, return myinode->di[num_ptr-1][num_ptr-1]; Note that direct or any indirect level might be partially filled with blocks! So if there is no more block or level the corresponding pointer will contain NULL.
*char get_blk_n_di(struct inode myinode, int n) int i, j, k; int count=0; int num_ptr = 8 * 1024 / 4; if (n<0 || n > num_ptrnum_ptr-1) return NULL;
if (myinode->di){ for(i=0; i < num_blk; i++) if (myinode->di[i]) { for(j=0; j < num_blk; j++) if (myinode->di[i][j]) { if (n==0) return myinode->di[i][j]; n--; } else return NULL; } else return NULL; } return NULL; } // A BETTER WAY i = n / num_blk; j = n % num_blk; if (myinode->di && myinode->di[i] && myinode->di[i][j]) return myinode->di[i][j]; else return NULL;
5. [ 20 points ] IPC and pipes -fifo Write a program (say prog.c) that forks and runs a sub-shell as a child process, and simply counts the number of output characters that the shell printed on the standard output. When the sub-shell terminates, the parent simply reports the number of output characters produced by the sub-shell.
For parent to do its job, it needs to get everything the child shell prints on the standard output. Hope you see how a pipe will be useful here: child [1:STDOUT_FILENO] --> pipe --> [0: STDIN_FILENO] parent. Since now the parent can get everything the child writes on the standard output, the parent can count the number of characters and write them into its own standard output.
You are asked to complete the following program so you can create the necessary pipe, child process and connect them as explained in the above scenario. You can ignore most of the error checking to make your solution clear, but you need to close all unnecessary file or pipe descriptors and check what read-write etc return. Also read/write one char at a time to make counting job easy!
/* your simple implementation of prog.c */ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <sys/stat.h> int main (int argc, char *argv[]) { int mypipe[2]; int childpid, numread, numwrite, count=0; char buf;
/* 4pt - create pipe and child process */
pipe(mypipe); childpid = fork();
if(childpid == 0){ /* child / _/ 4pt - child sets up the pipe */_
dup2(mypipe[1], STDOUT_FILENO); close(mypipe[0]); close(mypipe[1]);
execl("/bin/bash","shell",NULL); perror("cannot start shell"); return 1; }
/* continue in the next page */