Você está na página 1de 3

Birla Institute of Technology & Science, Pilani Hyderabad Campus CS C372/IS C362: Operating Systems

Assignment1(Process creation, execution, system calls, IPC) Assigned: 10.09.2012 Date of Submission: 03.10.2012

-----------------------------------------------------Problem1: Developing a Shell: The shell or command line interpreter is the fundamental User interface to an Operating System. Every shell is structured as the following loop: 1. 2. 3. 4. print out a prompt read a line of input from the user parse the line into the program name, and an array of parameters use the fork() system call to spawn a new child process o the child process then uses the exec() system call to launch the specified program o the parent process (the shell) uses the wait() system call to wait for the child to terminate When the child (i.e. the launched program) finishes, the shell repeats the loop by jumping to step 1.

5.

Write a simple shell in C named as myshell that has the following properties: It should support the following internal commands: 1. cd <directory> - Change the current default directory to <directory>. If the <directory> argument is not present, report the current directory. If the directory does not exist an appropriate error message should be reported. 2. echo <comment> - Display <comment> on the display flowed by a new line. 3. quit - Quit the shell. All other command line input is interpreted as program invocation, which should be done by shell forking and execing the programs as its own child processes. When you have read a command you may need to split the actual command and the arguments. You can do this by suing strsep function or strtok. The function strtok( ) will be helpful for parsing the input line into words separated by whitespaces (spaces and '\t' tab characters) and will place these words into an array of strings. Try man pages to learn more. Once you've parsed the command, it's time to execute it. This can be done using the function execv. This function takes two arguments. The first is the path to an executable, while the second is an array containing all the arguments. This function turns the current process into another process. Doing so allows us to execute a command, but in return we lose our shell process. The solution is to use the function fork, which makes a copy of the current process. When fork is called we have two nearly identical processes, both at the same point in the code. The only difference between the two is the return value of the fork function. The original process will get the process id of the new process, while the new process gets a return value of 0 (zero). You may use an if-statement to have the new ('child') process call execv to run the command, while the original ('parent') process can call the wait function to wait for the child to finish. Problem2: Adding a System Call to the Linux Kernel: A system call is the standard way an OS service is exported to a user program. For example, a system call may be used to give users access to internal information of a file system such as superblock or inodes. The procedure to create a system call varies in different versions of Linux. When you add a system call you will have to recompile the Linux kernel, so you must have the Linux source tree installed on your system. You may use the following steps in the Open source Linux versions (Fedora, and Ubuntu) that are installed in the System Software Lab of Computer Sc block. 1. Creating the System Call Source We will be creating a simple hello world system call. Create a file mysyscall.c and fill it with the following code.

#include <linux/kernel.h> #include <linux/linkage.h> asmlinkage long mysyscall(int i) { printk(KERN_DEBUG "Hello World! The number was %dn\n Implementation teams members ID are 2010, 2010.. ", i); return(1); } 2. Adding the System Call 1. Firstly, navigate to the root directory of your Linux source (source is generally folder names linux-sourceversion). Create a folder source/mysyscall. Next, inside your newly created folder, add the source file mysyscall.c created in step1. 2. Next, create a make file within this directory. This make file only needs to contain one line that says what to compile. Create source/mysyscall/Makefile (keep the capital M) obj-y := mysyscall.o 3. Next we need to modify the kernels make file. This is located in the root folder of your kernels source. Find the line that adds a bunch of folders to the core-y variable and add the folder name that the system call source is in. (mysyscall in this example) In source/Makefile, the line to change looks something like this: core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ It should be changed to something like this: core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ mysyscall/ 4. Now, navigate to the source/arch/x86/kernel folder. You will need to add your system call to the system call table. This is done in the file syscall_table_32.S. At the end of the file you add an entry for the new system call. Make sure to prefix your entry with sys_. It should look something like this: .long sys_mysyscall You should notice the numbers commented out to the right of some of the entries. They tell you what number the system call is. Keep track of which number your new system call is. 5. Next navigate to the source/include/asm-x86 folder and edit unistd_32.h. Add a #define line for the new system call. Use the number that you noted in the last step. Note that you may need to actually modify arch/x86/include/asm/unistd_32.h. You should add a line similar to this (before the line with __NR_syscall_max): #define __NR_mysyscall 327 6. You are now ready to recompile and install your kernel. You instruction from tutorial to build your kernel and boot into the new kernel. NOTE: If you are running a 64 bit kernel, you may have to modify appropriate files under the x86 folder. 3. Using the System Call Now, to use the system call you need to create a header file that exposes the new call. Create a file, mysyscall.h which will be imported into any program that wants to use the new system call. This is an executable that you will run as a user application. This code should contain contain the following: #include <unistd.h>

#define __NR_mysyscall 327 long mysyscall(int i){ return syscall(__NR_mysyscall, i); } int main() { printf(Team members name 2010A7.. 2010C6.. :%d\n, mysyscall(20)); return 0; } For submission, include the a) application code (with executable binary), b) screen capture of dmesg output when system call is invoked, c) files modified in linux kernel code (preferably as a patch) d) along with reference of where the linux code was picked from. Problem 3: Write a program that forks a child process. The parent has a set of integers {0,2,4,6,8} stored in an array. The parent waits for a specified interval of time, n seconds, to be taken as input from the command line. Every n seconds, the parent chooses a number from this set, at random, and sends it to the child. The parent must print the number being sent to the child. The child computes 100/(number), where number is the integer that the parent sends the child. The child process and parent process must terminate only when the user types the interrupt key (Ctrl C/Del). The child must print the number received from the parent and the sum of the quotients of all previous computations each time it performs a computation. Problem 4: Write a program that takes a series of commands from the command line. All the commands given in the command line should be separated by space. Do not input any option as a part of a command specified in the command line. The output of the first command should be given as input to the second command and output of second to third and so on till it reaches the last command. Handle the various error conditions that might occur like command not found etc. Display the output on the screen. This problem should be solved using Pipes. Few examples are: bash$ prog1 ls wc bash$ prog1 ls wc date bash$ prog1 ls la 12 12 73 Sun Sept 09 14:34:30 IST 2012 -Myshell: la: Command not found

Note: Use system calls like fork, exec, pipe, and wait. If you feel any other system call is needed, then use it as well.

Submission Instructions: You may form your own group of up to3 students. All your programs must run over either Fedora core or Ubuntu machines available in IPC Terminal 1 of Computer Science block. Submit source files and executables. Also submit a readme.txt with your group details. Put all your deliverables into a tar file (like, f2012023.tar) and send it to OS2012@bits-hyderabad.ac.in as a mail attachment. Also, please keep a backup of your work in the machine that you have used from the lab to implement the assignment. Copied codes will be awarded zero marks. Every assignment will have a demo/viva session which will be intimated later through a separate notice. CSIS N/B, 10.09.2012 Instructor In-Charge CS C372/IS C362 (Pl. remove on 20.09.2012)

Você também pode gostar