0% found this document useful (0 votes)
21 views7 pages

OS Lab 05

Uploaded by

Rai Bilal Sultan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

OS Lab 05

Uploaded by

Rai Bilal Sultan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OPERATING SYSTEM

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.

Plagiarism: Plagiarism cases will be dealt with strictly. If found


plagiarized, both the involved parties will be awarded zero marks in the
assignment, all of the remaining assignments, or even an F grade in the
course. Copying from the internet is the easiest way to get caught!

Deadline: The deadlines to submit the assignment are hard. Late


submission with marks deduction will be accepted according to the course
policy shared by the instructor. Correct and timely submission of the
assignment is the responsibility of every student; hence no relaxation will
be given to anyone.

Tip: For timely completion of the assignment, start as early as possible.


Furthermore, work smartly - as some of the problems can be solved using
smarter logic.
1. Note: Follow the given instructions to the letter, failing to do so will
result in a zero.
Operating System RIPHAH International University

LAB 05: C Programming in Linux:


In windows environment , We have seen different tools and IDEs like Visual Studio (VC++),
Code::blocks and DEV-C++, but in Linux we don't have such tools, We have to use command line tools
like GCC. We can easily compile and run C programs using GCC Compiler.

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:

if GCC is not installed, you can install it by following command

sudo yum install gcc

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;
}

A standard way to compile this program is with the command

gcc hello.c -o hello

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.

Stream, Redirection & Pipes:

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

> Redirects output of a command to a file or device (e.g.


printer). It overwrites the existing file.

>> It is similar to >, except if the target file already exists,


the new output is append to its end.

< Redirects input of command from a file or device.

<< The redirect operator << is used mostly in script files


(shell programs) to provide input to other
commands.

| Sends the output of one command to become the


input of another command.
Redirecting Output

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.

The format is as follow:

command > filename


or
command >> filename

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.

date > 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:

command < filename


or
command << filename

Example

cat < datafile

The contents of datefile are read into the standard input by redirection operation. Then the cat
command reads the input and displays the contents.

Combining Redirection Operators

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.

cat < fileone > filetwo

________________________ The End ________________________

You might also like