Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

midterm solutions operating, Cheat Sheet of Engineering

operating system midterm solutions

Typology: Cheat Sheet

2022/2023

Uploaded on 11/28/2023

esra-guzel
esra-guzel 🇹🇷

3 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Name / ID (please PRINT) Seq#:____ Seat #:_____
______________________________________________________________________
CS 3733.001 -- Operating system
Fall 2017 -- Midterm I -- Oct 5, 2017
You have 75 min. Good Luck!
This is a closed book/note examination. But You can use C reference card(s) given to you..
This exam has 5 questions in 9 pages. Please read each question carefully and answer all the
questions, which have 100 points in total. Feel free to ask questions if you have any doubts about
questions.
Partial credit will be given, so do not leave questions blank.
______________________________________________________________________________________________________________
You can get 2pt bonus credit if you complete the boldfaced two columns of the grading table below.
Please do this after answering the questions in the exam. Thanks!
Question
Topic
Possible
Points
Difficulty level of
this question
1: Easiest
5: Most difficulty
Student
Expects
Student
Received
1
Review questions, short
explanations
20
2
Program and Process,
static keyword, cmd line
20
3
CPU Scheduling
20
4
Unix I/O and file
operations, fork/exec
20
5
IPC: pipes -fifo
20
Bonus
2
Bonus for pipe quiz
4
Total
100+6
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download midterm solutions operating and more Cheat Sheet Engineering in PDF only on Docsity!

Name / ID (please PRINT) Seq#:____ Seat #:_____

______________________________________________________________________

CS 3733.001 -- Operating system

Fall 2017 -- Midterm I -- Oct 5, 2017

You have 75 min. Good Luck!

 This is a closed book/note examination. But You can use C reference card(s) given to you..

 This exam has 5 questions in 9 pages. Please read each question carefully and answer all the

questions, which have 100 points in total. Feel free to ask questions if you have any doubts about

questions.

 Partial credit will be given, so do not leave questions blank.

______________________________________________________________________________________________________________

You can get 2pt bonus credit if you complete the boldfaced two columns of the grading table below.

Please do this after answering the questions in the exam. Thanks!

Question Topic

Possible

Points

Difficulty level of

this question

1: Easiest

5: Most difficulty

Student

Expects

Student

Received

1 Review questions, short

explanations

2 Program and Process,

static keyword, cmd line

3 CPU Scheduling 20

4 Unix I/O and file

operations, fork/exec

5 IPC: pipes -fifo 20

Bonus 2

Bonus for pipe quiz 4

Total 100+

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

  • User application calls a user-level library routine (gettimeofday(), read(), exec(), etc.)
  • Invokes system call through stub, which specifies the system call number. From unistd.h: #define __NR_getpid 172 __SYSCALL(__NR_getpid, sys_getpid)
  • This generally causes a software interrupt , trapping to kernel
  • Kernel looks up system call number in syscall table, calls appropriate function
  • Function executes and returns to interrupt handler, which returns the result to the user space process 1. The interrupt is issued 2. Processor finishes execution of current instruction 3. Processor signals acknowledgement of interrupt 4. Processor pushes PSW( Program Status Word) and PC to control stack 5. Processor loads new PC value through the interrupt vector 6. ISR saves remainder of the process state information 7. ISR executes 8. ISR restores process state information Old PSW and PC values are restored from the control stack

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.

int count=0;

char *next_label()

static char b[10];

count++;

sprintf(b,"Fig. %d", count);

return b;

char *next_label()

static int count=0;

char *b;

b = malloc(10);

if (!b) exit();

count++;

sprintf(b,"Fig. %d", count);

return b;

b. [ 10 pt ] Show the relationship among the process created in the following program and give at least

two possible outputs except a, b, c, d, e, f, g.

void main()

printf("a\n");

if (fork() == 0) {

printf("b\n");

if (fork() == 0) {

printf("c\n");

exit(0);

printf("d\n");

wait(NULL);

printf("e\n");

exit(0);

printf("f\n");

wait(NULL);

printf("g\n");

exit(0);

Complete the relationship diagram (6pt)

c

_____

b | |

______________

| d e |

a | |

____________f____________g__

Two possible outputs (each 2pt)

a, f, b, c, d, e, g

a, b, f, c, d, e, g

a, b, c, f, d, e, g

a, b, c, d, f, e, g

a, b, c, d, e, f, g

c d can appear in different order

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;

dp

si

di

ti

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 */