Your IP : 216.73.216.40


Current Path : /var/www/html/bibhas.ghoshal/ITP_2019/lab/
Upload File :
Current File : /var/www/html/bibhas.ghoshal/ITP_2019/lab/Tut0.tex.backup

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{color}
%opening
\title{Tutorial 0 : Compiling your first C program in the Linux Environment}
%\author{}

\begin{document}

\maketitle

\underline{\textbf{Objective : }}
\begin{itemize}
 \item This lab is intended to provide an introduction to Linux. The lab consists of performing basic system operations such as text editing and file management  in Linux environment. It also introduces the steps for compiling a C program. The objective of this lab is to make students familiar with the Linux command-line environment and develop the skills of shell scripting. This lab serves as a platform for the subsequent labs related to programming in C.
 %This assignment is intended to learn how to create, work with and manipulate processes in XV6. You are expected to refer to the text book and references mentioned in the course
 %website befor you start the lab. 
 %Some sample codes for process creation using \emph{fork} system call have been provided for your reference.\\
\end{itemize}

\underline{\textbf{Recommended Systems : }}
\begin{itemize}
 \item Any Flavour of Linux - We will be using Ubuntu Systems in the lab 5042
 %This assignment is intended to learn how to create, work with and manipulate processes in XV6. You are expected to refer to the text book and references mentioned in the course
 %website befor you start the lab. 
 %Some sample codes for process creation using \emph{fork} system call have been provided for your reference.\\
\end{itemize}

\underline{\textbf{References : }}
\begin{itemize}
 \item Unix concepts and applications, Fourth Edition, Sumitabha Das, TMH.
 \item Brian W. Kernighan and Dennis M. Ritchie,The C Programming Language, Prentice Hall of India.
 \item Byron Gottfried, Schaum’s Outline of Programming with C,McGraw-Hill.
\end{itemize}


\underline{\textbf{Getting Started}}\\
\begin{itemize}


\item Switch on your monitor.

\item Switch on your PC.
\item Allow the machine to boot. 

\item Wait until the log in prompt comes.

\item Supply your log-in and password
\begin{itemize}
 \item Log in : \emph{iiita}
 \item Password : \emph{iiita123}
 This opens your window manager(usually GNOME) with icons, the side panel, and so on. You are now ready to start your work
 
 \item Click on the terminal icon to open a shell (command prompt).
 
\end{itemize}


\underline{\textbf{Compiling Your First C Program}}\\
\begin{itemize}


\item 

\item Switch on your PC.
\item Allow the machine to boot. 

\item Wait until the log in prompt comes.

\item Supply your log-in and password
\begin{itemize}
 \item Log in : \emph{iiita}
 \item Password : \emph{iiita123}
 This opens your window manager(usually GNOME) with icons, the side panel, and so on. You are now ready to start your work
 
 \item Click on the terminal icon to open a shell (command prompt).
 
\end{itemize}

\end{itemize}

\underline{\textbf{Lab Assignments:}}

 \begin{enumerate}
  \item Use the \emph{ps, ps lx, ps tree and ps -aux} command to display the process attributes.
  \item Learn the \emph{top} command to display the resource utilization statistics of processes
       \begin{itemize}
       \item Open a terminal and type the \emph{top} command
        \item Start a browser and see the effect on the \emph{top} display
        \item Compile a C program and observe the same effect (Use a long loop - say while(1) to observe the effect)
        \item From the \emph{top} display, answer the following:
           \begin{itemize}
            \item How much memory is free in the system?
            \item Which process is taking more CPU?
            \item Which process has got maximum memory share?
           \end{itemize}
        \item Write a CPU bound C program and a I/O bound C program (e.g. using more printf statements within while(1) loop), compile and execute both of them.\\ 
        Observe the effect of their CPU share using the \emph{top} display and comment.
       \end{itemize}
 \end{enumerate}
  
%  \item  Write a program in C that creates a child process, waits for the termination of the  child and lists its PID, together with the state in which the process was terminated
 % (in decimal and hexadecimal)
   
% \item Test the codes for creation of orphan process and zombie process given in the reading resource section of Assignment 3 in the course website.
 
% \item In a C program, print the address of the variable and enter into a long loop (say using while(1)).
 %     \begin{itemize}
 %      \item Start three to four processes of the same program and observe the printed address values.
 %      \item Show how two processes which are members of the relationship parent-child are concurrent from execution point of view, initially the child is copy of the parent, but every 
 %      process has its own data.
       
 %     \end{itemize}
 
 
 
%  \item  Write two programs : one called \emph{client.c}, the other called \emph{server.c}. The client program lists a prompter and reads from the keyboard two integers and one of the 
%characters '+' or '-'. The read information is transmitted with the help of the system call \emph{excel} to a child process, which executes the server code. After the child (server) 
%process fiishes the operation, it transmits the result to parent process (client) with the help of the system call \emph{exit}. The client process prints the result on the screen and also 
%reprints the prompter, raedy for a new reading.

% \item Test the source code below:\\\\
%	\emph{$for(i=1;i\leq10;i++)$\{\\
%	  fork();\\
%	  printf(``The process with the PID=\%d'',getpid());\\
%	$\}$}\\\\
%	In the next phase, modify the code, such as after all created processes have finished execution, in a file \emph{process\_management.txt} the total number of created processes 
%	should be stored.
	
	


\underline{\textbf{Working with XV6 :}}
\begin{itemize}
 \item Fetch and un-tar the xv6 source (sent to you via email):\\
 Build xv6:\\
 \$ cd xv6 \\
 \$ make
 
 \item Running xv6 under QEMU\\
 Now Type:\\
 \$ make qemu
\end{itemize}

A seperate window should appear containing the display of the virtual machine. After a few seconds, QEMU's virtual BIOS will load xv6's  boot loader from a virtual hard drive image 
conatined in the file xv6.img, and the boot loader will run in turn load and run the xv6 kernel. After everything is loaded, you should get a '\$' prompt in the xv6 display window and be 
able to enter commands into the rudimentry but functional xv6 shell. For example, try:\\
 \$ ls\\
 \$ echo Hello!\\
 \$ cat README\\
 \$ grep run README\\
 \$ cat README $\mid$ grep run $\mid$ wc\\
 \$ echo MY NEW FILE > newfile\\
 \$ cat newfile\\

The small file system you're examining and modifying here resides on a second virtual disk, whose initial contents QEMU initializes from the file \emph{fs.img}. Later in the course you will examine how xv6 accessess and modifies this file system.\\
 
\underline{\textbf{Remote Debugging xv6 under QEMU :}}

The easiest way to debug xv6 under QEMU is to use GDB's remote debugging feature and QEMU's remote GDB debugging stub.Remote debugging is a very important technique for kernel development\\
In general: the basic idea is that the main debugger (GDB in this case) runs separately from the program being debugged (the xv6 kernel atop QEMU) - they could be on completely separate machines, in fact. The debugger
and the target environment communicate over some simple communication medium, such as a
network socket or a serial cable, and a small remote debugging stub handles the "immediate
supervision" of the program being debugged in the target environment. This way, the main
debugger can be a large, full-featured program running in a convenient environment for the
developer atop a stable existing operating system, even if the kernel to be debugged is running
directly on the bare hardware of some other physical machine and may not be capable of running a
full-featured debugger itself. In this case, a small remote debugging stub is typically embedded into
the kernel being debugged; the remote debugging stub implements a simple command language that
the main debugger uses to inspect and modify the target program's memory, set breakpoints, start
and stop execution, etc. Compared with the size of the main debugger, the remote debugging stub is
typically miniscule, since it doesn't need to understand any details of the program being debugged such as high-level language source files, line numbers, or C types, variables, and 
expressions: it merely executes very low-level operations on behalf of the much smarter main debugger.
When we are doing kernel development using a virtual machine such as QEMU, remote debugging
may not be quite as critical: for example, xv6 can also be run under the Bochs emulator, which is
much slower than QEMU but has a debugger built-in and thus does not require the use of GDB
remote debugging. On the other hand, while usable, the Bochs debugger is still not as complete as
GDB, so we will primarily use GDB with QEMU's remote debugging stub in this course.\\

To run xv6 under QEMU and enable remote debugging:
\begin{itemize}

 \item Open new terminal(T1) and enter the 1st command\\
 \$ nm kernel $\mid$ grep\_start 
 
 \item note\_the\_break\_point 
 
 \item Open a new terminal(T2) and enter the 2nd command\\
 \$ make qemu-nox-gdb
 
 \item Open a new terminal(T3) and enter the following command \\
 \$ gdb \\
 \$ target remote: port\_no obatined from terminal T2\\
 \$ br * 0X break\_point\\
 \$ continue or c\\
\end{itemize} 
 
Now close this QEMU session, destroying the state of the xv6 virtual machine. You can do so either by entering quit at the (qemu) promt in the original window from which you started QEMU
,or just by pressing CTRL-C in that window.\\

\begin{itemize}
 \item Four simple C programs are provided to you with this lab: \emph{cpu.c,cpu-print.c,disk.c,and disk1.c}. You may want to compile and keep all the executables handy for solving the
 exercises. For example, you can create the \emph{cpu} executable as follows.\\
 \$ gcc cpu.c -o cpu
 
 \item The disk programs read a large number of files,  and you must create and setup these files be-fore you run the program.  Create a subfolder \emph{files} in the working directory of 
 the \emph{disk /disk1} programs.  Create 10,000 files of size 2MB each in the folder, with names \emph{foo0.txt,foo1.txt}, and so on until \emph{foo9999.txt}.  This repository of 10,000 
 files of 2MB each will be used in several take-home labs subsequently, so you may want to keep them around on yourmachine. Feel free to write 2MB of random data into each of these files.
\end{itemize}

\underline{\textbf{Exercises :}}

Do the following exercises, and record your observations in your report. Note: run the programs one afterthe other (and not simulatenously) when doing the exercises, in order to cleanly 
record the observationsfrom each program separately.\\

\begin{enumerate}
 \item Collect the following basic information about your machine using \emph{proc}.  How many CPU coresdoes the machine have?  How much memory, and what fraction of it is free?  
 How many context switches has the system performed since bootup? How many processes has it forked since bootup?
 \item Every process consumes some resources (CPU, memory, disk or network bandwidth, and so on).When a process runs as fast as it can, one of these resources is fully utilized, limiting
 the maximumrate at which the process can make progress.  Such a resource is called the \emph{bottleneck resource} of a process.   A process can be bottlenecked by different resources at
 different points of time,depending on the type of work it is doing.\\
 Run each of the four programs \emph{(cpu,cpu-print,disk, anddisk1)} separately, and identifywhat the bottleneck resource for each is (without reading the code). For example, you may 
 monitorthe utilizations of various resources and see which ones are at the peak.  Next, read through thecode and justify how the bottleneck you identified is consistent with what the 
 code does.\\
 For each of the programs, you must write down three things: the bottleneck resource, the reasoningthat went into identifying the bottleneck, (e.g., the commands you ran, and the outputs 
 you got),and a justification of the bottleneck from reading the code.
 \item Recall that every process runs in one of two modes at any time: \emph{user mode and kernel mode}.  It runs in user mode when it is executing instructions / code from the user. 
 It executes in kernel modewhen running code corresponding to system calls etc.\\
 Compare (qualitatively) the programs \emph{cpu} and \emph{cpu-print} in terms of the amount of time each spends in the user mode and kernel mode,  using information from the \emph{proc}
 file system.   Forexamples, which programs spend more time in kernel mode than in user mode, and vice versa?Read through their code and justify your observations.
 \item Recall that a running process can be interrupted for several reasons.  When a process must stoprunning and give up the processor, it’s CPU state and registers are stored, and the 
 state of anotherprocess  is  loaded.   A  process  is  said  to  have  experienced  a \emph{context switch} when  this  happens.Context switches are of two types:  voluntary and 
 involuntary.  \\
 A process can voluntarily decideto give up the CPU and wait for some event, e.g., disk I/O. A process can be made to give up its CPU forcibly, e.g., when it has run on a processor for 
 too long, and must give a chance to otherprocesses sharing the CPU. The former is called a voluntary context switch, and the latter is called an involuntary context switch.\\
 Compare the programs \emph{cpu and disk} in terms of the number of voluntary and involuntary context switches.  Which program has mostly voluntary context switches, and which has mostly
 involun-tary context switches? Read through their code and justify your observations.
\end{enumerate}




%\item Write two programs file1.c and file2.c\\
% Program file1.c uses these : 
% \begin{enumerate}
%   \item \textbf{fork()} to launch another process
%   \item \textbf{exec()} to replace the program driving this process, while supplying arguments to file2.c to complete its execution
%   \item \textbf{wait()} to complete the execution of the child process
%   \item file1.c takes two arguments x( a number less than 1) and n (number of terms to be added, 1 or more). For example: file1 0.5 5\\
%   \item When the child proces finishes, the parent prints:\\
%     \textbf{Parent(PID=yyy) : Done}
%\end{enumerate}

%Program file2.c requires two arguments to obtain the approximate value of $e^{x}$ by adding the first n terms in the relation : $e^{x}$ = 1+x+$x^{2}/2!$+$x^{3}/3!$+.......\\and prints the 
%result in the format: \\\\
% \textbf{Child(PID=yyy) : For x = 0.5 the first 5 terms yields 1.6484375}\\

%\emph{Hint : Child-specific processing immediately following the fork() command should load file2.c into the newly created process using the exec() command.  This exec() command should
%also pass 2 arguments to the child. Refer to the man page of exec() command to know how to pass on arguments to the child process. Parent-specific processing should ensure that the parent 
%will wait() for the child-
%specific processing to complete.}


%\item  Write two programs : one called \emph{client.c}, the other called \emph{server.c}. The client program lists a prompter and reads from the keyboard two integers and one of the
%characters '+' or '-'. The read information is transmitted with the help of the system call \emph{execl} to a child process, which executes the server code. After the child (server) 
%process fiishes the operation, it transmits the result to parent process (client) with the help of the system call \emph{exit}. The client process prints the result on the screen and also
%reprints the prompter, ready for a new reading.




% \item 
% Write a C program that takes a file name as a command line parameter and sorts a 
% set of integers stored in the file (use any sorting method). You can assume that the 
% file will always be there in the current directory and that it will always contain a 
% set of integers (maximum no. of integers is 1000). The sorted output is written to 
% the display and the input file is left unchanged. Compile the C file into an 
% executable named \emph{"sort1"}. Name the C file \emph{sort1.c}.\\
% Now write a C program (\emph{xsort.c}) that implements a command called \emph{"xsort"} that 
% you will invoke from the shell prompt. The syntax of the command is \emph{"xsort $<filename>$ "}. When you type the command, the command opens a new xterm window (\emph{terminal}), 
%and then sorts the integers stored in the file \emph{$<filename>$} using the program \emph{"sort1"}. Look up the man pages for \emph{xterm, fork} and the different 
% variations of \emph{exec*} system calls (such as \emph{execv, execve, execlp} etc.) to do this assignment.\\ 
% Submit the C files \emph{sort1.c} and \emph{xsort.c}. 

%\end{enumerate}


% 8.      
% 
% 9.      
% 
% 10.  Write a C program that finds a file in a file-tree starting from a given directory. The name of the file for which we are searching for, as well as the name of the starting 
%directory should be read from the command line. Optionally, the name of the file can be specified as a pattern using the ‘*’ character.
% 
% 11.  Write a C program similar to the previous one, but this time try to find a string in the files.
% 
% 12.  
% 
% 13.  Write a program that would allow traversing a file-tree printing out the type, size and access rights for of the found files. The name of the starting directory should be read from 
%the command line. Observation: be attentive to the symbolic links.  
% 
% 14.  A binary file fil.bin contains integers. Calculate the arithmetic mean of each group of numbers that are situated between two 0s in the file. Write these obtained values on distinct
%lines in the file means.txt. The beginning and the end of the file plays the role of a 0, except the case when the first and last element of the file is 0.
% 
% 15.  A binary file fil.bin contains numbers and characters in the following way: 2 integers followed by a character from the set {+,-,*,/}. Write a C program that reads a group of
%numbers and the attached character and performs the operation on the two integers and then writes the result in a file res.txt in the form:
% 
% nr1 operator nr2 = result
% 
% The result line should be appended to the end of the file. The number of groups in the binary file should be specified in the command line as an input parameter.

 


\end{document}