Anda di halaman 1dari 7

Experiment - 1

Aim: Write a C program to perform bubble sort in linux operaring system and compile it
using gcc and use gdb debugger.

Description of Aim and Related Theory:

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that
repeatedly steps through the list to be sorted, compares each pair of adjacent items
and swaps them if they are in the wrong order. The pass through the list is repeated
until no swaps are needed, which indicates that the list is sorted. The algorithm, which is
a comparison sort, is named for the way smaller or larger elements "bubble" to the top
of the list. Although the algorithm is simple, it is too slow and impractical for most
problems even when compared to insertion sort.[2] It can be practical if the input is
usually in sorted order but may occasionally have some out-of-order elements nearly in
position.

GCC:
The GCC (GNU Compiler Collection) is widely regarded as the most important piece
of free software. Formerly called the GNU C Compiler, the GCC now
contains compilers for the C, C++, Objective C, Fortran, Java and Ada programming
languages.

GCC -The GNU Compiler Collection (GCC) is a compiler system produced by the GNU
Project supporting various programming languages. GCC is a key component of the GNU
toolchain and the standard compiler for most Unix-like Operating Systems.

GCC is a key component of "GNU Toolchain", for developing applications, as well as


operating systems. The GNU Toolchain includes:
1. GNU Compiler Collection (GCC): a compiler suit that supports many languages,
such as C/C++, Objective-C and Java.
2. GNU Make: an automation tool for compiling and building applications.
3. GNU Binutils: a suit of binary utility tools, including linker and assembler.
4. GNU Debugger (GDB).
5. GNU Autotools: A build system including Autoconf, Autoheader, Automake and
Libtool.
6. GNU Bison: a parser generator (similar to lex and yacc).
GCC Compilation Process
GCC compiles a C/C++ program into executable in 4 steps as shown in the above
diagram. For example, a "gcc -o hello.exe hello.c" is carried out as follows:
1. Pre-processing: via the GNU C Preprocessor (cpp.exe), which includes the headers
(#include) and expands the macros (#define).
> cpp hello.c > hello.i

The resultant intermediate file "hello.i" contains the expanded source code.
2. Compilation: The compiler compiles the pre-processed source code into assembly
code for a specific processor.
> gcc -S hello.i

The -S option specifies to produce assembly code, instead of object code. The
resultant assembly file is "hello.s".
3. Assembly: The assembler (as.exe) converts the assembly code into machine code
in the object file "hello.o".
> as -o hello.o hello.s
4. Linker: Finally, the linker (ld.exe) links the object code with the library code to
produce an executable file "hello.exe".
> ld -o hello.exe hello.o ...libraries...

GDB
A debugger is a program that runs other programs, allowing the user to exercise control
over these programs, and to examine variables when problems arise.

GNU Debugger, which is also called gdb, is the most popular debugger for UNIX systems
to debug C and C++ programs.

GNU Debugger helps you in getting information about the following:

 If a core dump happened, then what statement or expression did the program
crash on?

 If an error occurs while executing a function, what line of the program contains
the call to that function, and what are the parameters?

 What are the values of program variables at a particular point during execution
of the program?

 What is the result of a particular expression in a program?


How GDB Debugs?
GDB allows you to run the program up to a certain point, then stop and print out the
values of certain variables at that point, or step through the program one line at a time
and print out the values of each variable after executing each line.

GDB uses a simple command line interface.

Points to Note
 Even though GDB can help you in finding out memory leakage related bugs, but
it is not a tool to detect memory leakages.

 GDB cannot be used for programs that compile with errors and it does not help
in fixing those errors.
Code :

#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]); // input array element
for (i = 0 ; i < ( n - 1 ); i++) // last element is already in place
{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1]) // interchange element
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", array[i]); // print array element
return 0;
}
Output:

Banwari@banwari~VirtualBox:~/home$ gcc bubble.c


Banwari@banwari~VirtualBox:~/home$ ./a.out
Enter total number of elements
5
Enter 5 numbers
3
2
6
4
1
Sorted list in ascending order:
1
2
3
4
6
Banwari@banwari~VirtualBox:~/home$

Finding and Learning:


We have written and compiled C program in linux system using gcc compiler
and got the desired outputs as shown above. It was a nice hands-on on the
linux system and found it easy and attractive to work on.
Experiment 2
Aim: Write a C program to check process IDs of the program and the parent of the
program process
1. Use system command to execute ps command to get all the processes under the
current user system(“ps”)
2. Use getpid() and getppid() to get the program process and parant process ids check if
the program process id and parant process ids are listed in the ps command.
Output to be directed to an output file for submission.
Extend this program to fork 5 child processes , parant must wait for all children to exit.

Description of aim and related theory:

ps command

The ps (i.e., process status) command is used to provide information about the currently
running processes, including their process identification numbers (PIDs).

A process, also referred to as a task, is an executing (i.e., running) instance of a program.


Every process is assigned a unique PID by the system.

The basic syntax of ps is

ps [options]

When ps is used without any options, it sends to standard output, which is the display
monitor by default, four items of information for at least two processes currently on the
system: the shell and ps. A shell is a program that provides the traditional, text-only user
interface in Unix-like operating systems for issuing commands and interacting with the
system, and it is bash by default on Linux. ps itself is a process and it dies (i.e., is
terminated) as soon as its output is displayed.

The four items are labeled PID, TTY, TIME and CMD. TIME is the amount of CPU (central
processing unit) time in minutes and seconds that the process has been running. CMD is
the name of the command that launched the process.

Fork()-

System call fork() is used to create processes. It takes no arguments and returns a
process ID. The purpose of fork() is to create a new process, which becomes the child
process of the caller. After a new child process is created, both processes will execute
the next instruction following the fork() system call. Therefore, we have to distinguish
the parent from the child. This can be done by testing the returned value of fork():
 If fork() returns a negative value, the creation of a child process was
unsuccessful.
 fork() returns a zero to the newly created child process.
 fork() returns a positive value, the process ID of the child process, to
the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally,
the process ID is an integer. Moreover, a process
can use function getpid() to retrieve the process ID assigned to this
process.

CODE:

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int status=system("ps");
for(int i=0;i<5;i++) // loop will run 5 times
{
if(fork() == 0)
{
printf("[Child] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
for(int i=0;i<5;i++) // loop will run 5 times
wait(NULL);
return 0;
}
Result :
Banwari@banwari~VirtualBox:~/home$ gcc assignment2.c
Banwari@banwari~VirtualBox:~/home$ ./a.out

Finding and Learning :


We have learned about process and forking of processes in this experiment. We have
learnt how the process gets forked in linux system and how the parent process keeps
track of resources allocated to a child process. We learnt the diffrences between
commands and system calls. If the call to fork() is executed successfully, Unix will
make two identical copies of address spaces, one for the parent and the other for the
child. Both processes will start their execution at the next statement following
the fork() call.

Anda mungkin juga menyukai