OS Lab 05
OS Lab 05
LAB-Manual 05
BS-Computer Sciences
________________ _________________
Faculty of Computing
Riphah International University Islamabad Campus I-14
Operating System RIPHAH International University
Instructions
Submission: Use proper naming convention for your submission file.
Name the submission file as
LabNO_SAPID_Section (e.g. Lab03_23456_CS5)
Submit the file on Moellim within the deadline. Failure to submit
according to the above format would result in deduction of 10% marks.
Submissions on the email will not be accepted.
GCC:
The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project. Originally
named the GNU C Compiler, GCC 1.0 was released in 1987. It was extended to compile C++ in
December of that year, later developed for Objective C/C++, Java and Fortron. You can check which
version of gcc is installed by command:
after installation, Any file containing source code for C should be saved with the extension .c For example
test.c.
Consider the following example: Let "hello.c" (only save files with .c extension)
#include <stdio.h>
int main()
{
printf("Hello...\n");
return 0;
}
This command compiles hello.c into an executable program named "hello" that you run by typing ./hello
at the command line. It does nothing more than print "Hello..." on the screen.
gcc has an option –o to specify the name of the output file. If the name of the output file is not specified,
gcc names the output file a.out by default.
Operating System RIPHAH International University
Once you got the source program compiled successfully, its object file is ready to be execute. To execute
the object file type following command line on the prompt.
./hello
Note:
Never type ./hello.c
you need to have execute permission to run that file.
Exercise:
Write a C program that ask the user to enter a number and determine whether the number entered is even
or odd.
Java:
Similarly we can check whether java is installed by command: java -version
Date Command:
Command Description
date The date command displays the current date and time on the
screen. The system administrator sets the date users cannot change
them.
Example:
There are number of options in which date can be displayed. If you want to
see only date, you can do it like this:
Operating System RIPHAH International University
Clear Command:
Command Description
clear Clears the screen.
Streams:
In the computer field streams is an abstract idea. Informally, anything flow into the computer in the form
of input, and anything flow out of computer in form of output are referred as streams. Formally, input
flowing from input device like keyboard to memory is referred as input stream, and output flowing from
memory to output device is referred as output stream. So streams are associated with flow of data.
As we know when a Linux command executes it may take input before execution and gives some results
after execution. Therefore, each command in Linux is associated with streams. Stream associated with
the execution of Linux commands are
i) Standard Input Stream (Stdin)
ii) Standard Output Stream (Stdout), and
iii) Standard error Stream (Stderr)
Normally, standard input flows from keyboard to the memory, and standard output and standard error
flow to the terminal. When a Linux command executes, in case of errors, errors messages flow as output
from computer to the terminal, is called standard error stream.
I/O Redirections:
Redirection changes the assignments for standard input and standard output.
In normal cases standard input comes from keyboard, and standard output goes to the screen. The term
redirection is used when the standard input will not be coming from the keyboard, but from some
other scours like a file, and standard output will not be going to screen, but to some other scours like
file or to other commands.
Operating System RIPHAH International University
In the case of I/O redirection the shell should be informed. To tell the shell to redirect input or output of any
command, following characters are used.
Operator Description
Normally, the output is sent to screen, output can be sent to some other scours like a file. The operator >
is used with a command to redirect output.
Example
As we know when the date command executes it sends its output to the screen. The output from the
date command, for example, can be sent to a file xyz.
Note:-
If the already exists, then it will be over written over, and the contents of the existing file are
lost. If the specified filename does not exist, then the shell creates one to save the output in it.
Redirecting Input
Normally, the input is taken from keyboard; input can be taken from other source like file. The operator <
is used for redirecting input.
Operating System RIPHAH International University
The standard input may be received from a file rather then the keyboard. The operator < reads standard
input from file rather than keyboard. The format is as follow:
Example
The contents of datefile are read into the standard input by redirection operation. Then the cat
command reads the input and displays the contents.
The redirection operations for both standard input and standard output can be combined. For example, in
this example, the standard input has been redirected to receive its data from a file, and the standard output
has been redirected to place its data in a file.