hw2

pdf

School

Clemson University *

*We aren’t endorsed by this school

Course

(ECE)

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

8

Uploaded by ConstableSquid3550

Report
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 1 This homework aims to help you understand the system calls and prepare for project 2. 1. Type of System Calls System calls can be grouped roughly into six major categories: Process management, file management, device management, information maintenance, communications, and protection. Put the following system calls into their right category. System Call Category pid = fork() Process management int exit(int) Process management Int wait(int *) Process management int pipe(int*); Communication int write(int, const void*, int); File management int read(int, void*, int); File management int close(int); File management int kill(int); Process management int exec(char*, char**); Process management int open(const char*, int); File management int mknod(const char*, short, short); File management int dup(int); Communication int chdir(const char*); Process management int getpid(void); Process management char* sbrk(int); Process management 2. System call implementations. a. Fill in the appropriate names in the following figure. The list of possible names are: user mode, kernel mode, system call interface, system call table, stack, heap. User Application System call interface open() implementation of open() system call .. return ope n() Kernel mode System Call Interface User Mode
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 2 b. System calls can be run in either user mode or kernel mode. True False c. There are three general methods for a program to pass parameters to the operating systems: A) Pass parameters in registers, B) store parameters in a block or a table in memory, and pass the address of the block as a parameter in a register, and C) the program pushes the parameters onto a stack, and the operating system pops the parameters off the stack. If the operating system does not want to limit the number or length of the parameters being passed, which parameter passing approach(es) should it use? Choose any that is correct. A) Register B) Register for the address of a memory block that stores parameters in memory C) Stack (program pushes parameter onto the stack; kernel pop parameters off the stack) D) Cache memory d. In C programs, printf() is provided as A) a standard library B) a system call C) a program D) a thread 3. Command Interpreter or Shell a. What is the purpose of the command interpreter? It reads commands from user or a file of commands and executes them turning them into system calls b. Why is shell usually separate from the kernel? Its usually not part of the kernel since the command interpreter can change c. Which system calls have to be executed by a shell in order to start a new process on UNIX? Fork(),exec(),wait(),pipe(),dup2() d. What is the purpose of system programs? System calles let user level processes get services from the operating system e. Provide three examples of system programs. File management tools, Text editors and Antivirus software f. Is it possible to write a new command interpreter using a high-level programming language other than C (for example, using Java, Python, or Go)? Yes g. A shell command can be either an external command (implemented in another program and run in a new process) or an internally builtin command (implemented within a shell as a function or a
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 3 block of code). Tell which type each of the following commands is. (Hint: you may want to use command man type to see what type does). Command Purpose Implementation pwd Print name of current working directory built-in command cd Change the current directory. built-in command exit Cause the shell to exit built-in command history Display the command history built-in command kill Send a signal to a process built-in command grep Print lines that match a pattern external command find Search for files in a directory hierarchy external command wc Print newline, word, and byte count for each file external command xargs Build and execute command line from standard input external command cat Concatenate files and print on the standard output external command 4. Process Creation The following figure illustrates a basic process creation on XV6 using the source code provided below. You can run the program on XV6 to find out the answers. 1 #include "kernel/types.h" 2 #include "user/user.h" 3 #include "kernel/fcntl.h" 4 5 int main() { 6 int pid; 7 char *cmd[] = {"echo", "3220", 0}; 8 9 pid = fork(); 10 11 if (pid < 0) { 12 fprintf(1, "fork() failed\n"); 13 exit(-1); 14 } else if (pid == 0) { 15 exec(cmd[0], cmd); 16 fprintf(1, "%s: why I am here?\n", cmd[0]); 17 } else { 18 wait(0); 19 fprintf(1, "child exited\n");
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 4 20 } 21 exit(0); 22 } In the following questions, a ssume “echo“ is correctly implemented and within in the process’s current work directory (i.e., calling exec() should succeed. (a) What is the output after running the above program? 3220 child exited (b) Will line 16 be executed? No it will not (c) Why does the string array cmd end with 0? Is ending “0” required or optional for the arguments passed into the exec() system call on XV6? The 0 at the end is required to properly terminatethe argument list as it acts as a null terminator or else there can be undefined behavior. (d) What is the output of the program if Line 15 is deleted? echo: why I an here? child exited (e) What would happen to the child process if Line 18 is deleted? Parent process would not wait for the child process and the child exited message gets printed before the child processs actually gets exited 5. I/O Redirect 1 #include "kernel/types.h" 2 #include "user/user.h" 3 #include "kernel/fcntl.h" 4 5 int main() { 6 int pid, fd; 7 char *cmd[] = {"echo", "3220", 0}; 8 9 pid = fork(); 10 11 if (pid < 0) { 12 fprintf(1, "fork() failed\n"); 13 exit(-1); 14 } else if (pid == 0) { 15 close(1); 16 fd = open("a.out", O_CREATE|O_WRONLY); 17 fprintf(1, "fd=%d\n", fd); 18 exec(cmd[0], cmd); 19 fprintf(1, "%s: why I here?\n", cmd[0]); 20 } else { 21 fd = open("b.out", O_CREATE|O_WRONLY); 22 fprintf(1, "fd=%d\n", fd);
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 5 23 wait(0); 24 } 25 exit(0); 26 } Assuming the shell that executes the above program has only three file descriptors (0, 1,and 2) opened. You can run the program on XV6 to find out the answers. a. What would be the value of fd after Line 16 is executed? fd=1 b. What would be the value of fd after Line 21 is executed? fd=2 c. What does file “a.out” contain? fd=1\n3220\n d. What does file “b.file” contain? b.out is empty 6. Pipes 1 #include "kernel/types.h" 2 #include "user/user.h" 3 #include "kernel/fcntl.h" 4 5 int main() { 6 int pfd[2]; 7 char *cmd1[] = {"echo", "cpsc3220", 0}; 8 char *cmd2[] = {"cat", 0}; 9 10 pipe(pfd); 11 if (fork() == 0) { 12 close (pfd[0]; 13 dup(pfd[1]); 14 close(pfd[1]); 15 Click or tap here to enter text. ; 16 exec(cmd1[0], cmd1); 17 } 18 if (fork() == 0) { 19 close(pfd[1]); 20 dup(pfd[0]); 21 close(pfd[0]) ; 22 Click or tap here to enter text. ; 23 exec(cmd2[0], cmd2); 24 } 25 close(pfd[0]); 26 close(pfd[1]); 27 wait(0); 28 wait(0);
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 6 29 30 exit(0); 31 } a. Suppose you want to execute the command “echo cpsc3220 | cat”, what changes would you want to make in the above code to achieve that purpose? You can directly change the code in the highlighted fields. b. In the following code, how many times the function fork() is called? 2 c. Line 20 invokes the system call dup(pfd[0]). What does the function dup() do? It duplicates pfd[0], the file discripter I think, to the lowest avalible dile descriptor number. d. Why did the parent process close both file descriptors of the pipe at Lines 25-26? Similar to how you free memory in C for malloc you need to close the file descriptors properly so that they can be used later and there are no leaks. 7. Read/write data from pipe and detect end of incoming data 1 #include "kernel/types.h" 2 #include "user/user.h" 3 #include "kernel/fcntl.h" 4 5 int main() { 6 int pfd[2]; 7 int n = 1; 8 9 pipe(pfd); 10 if (fork() == 0) { 11 close(pfd[1]); 12 while (read(pfd[0], &n, sizeof(n)) > 0) { 13 fprintf(1, "%d\n", n); 14 }; 15 } else { 16 close(pfd[0]); 17 for (n = 1; n < 5; ++n) { 18 write(pfd[1], &n, sizeof(n)); 19 } 20 wait(0); 21 } 22 23 exit(0); 24 } a. The above code demonstrates communications using pipe between two processes. First, look at the read() and write() functions at Line 12 and Line 18. These two lines read and write variable n, which is of an integer type. Execute the above code prints 1 through 4 on the screen. However, when you
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 7 change Line 13 to: “write(1, &n, sizeof(n));”, the program prints some strange characters or nothing on the screen. Can you explain why? Because trying to write this new line 13 would mean trying to write binary data to stout which is not readable as its not formatted as text b. The above code has a bug: it does not exit. You quickly found out that this issue is caused by Line 12 because the child process keeps polling from the pipe and it does not know when the incoming data end. What change can you make so that the child process will exit once the parent process finishes writing the data? A exit(0) can be added to the child process after line 14 to terminate itself. 8. Test the Shell (This is just for information purposes) Shell, like any other program, must be texted exhaustively before releasing to the customers. However, one difficulty is that shell is an interactive program that takes a command from the user and then runs the command, which makes it difficult to test every combination of the cases. Furthermore, it is time-consuming to run the command one by one multiple times. Here, you need some better strategy. a. The Input-Process-Output (IPO) pattern of programs. Most programs follow the IPO pattern, which takes some input, does some computation, and then generates some output. If you know the expected output for a given input, then you can design a test function that takes a designed input (DI) and the corresponding expected output (EO), runs the program with the input DI, catches the actual output (AO), and the tests if AO matches EO. This generalized test function can be used in your testing and potentially an auto-grading program. b. Automate the test process for interactive programs. To automate the testing process for an interactive program like Shell, your OS knowledge comes to help. You can create and save the designed input in a test input file. Then you set up a pipe, and run the shell program in a new process with standard input redirected to the test input file and standard output redirected to pipe. The test program reads the program’s output from the pipe and then compares it with the expected output. c. Enhance the testing program. To catch the bugs and prevent the bugged program from crashing your test program, you probably need to consider a few more enhancement. (a) Run a particular test function in a separate process with a given run time bound. Although the test program does not have the quality of OS kernels, you don’t want the test program to wait for a bugged tested program that never stops. (b) Repeat a large number of runs with randomized input. Like we discussed in the synchronization lecture, software bugs like heisenbugs are difficult to be detected but might occur when your boss is giving a demo of your innovation at the annual CES show. To avoid such embarrassment, you want your testing program to be able to catch some of the hidden and rarely occurred bugs. For example, a large number of random runs may be
CPSC/ECE 3220-001 HW2 Due 11:59PM Sept 27, 2023 Name : Ohm Patel Page | 8 able to detect the program defects of failing to release unused file descriptors or allocated memory. (c) Separate test cases from test logic. Many test programs are so long that make people wonder if the test programs are more buggy than the program being tested. Separating test cases from the test logic can simplify the test logic but requires careful design. Now, the principles that you have learned in the OS class can help you to create a small set of well-designed interfaces and then construct a more complex programs using these interfaces.