Experiment - 1: Compiler GNU Project GNU Toolchain
Experiment - 1: Compiler GNU Project GNU Toolchain
Experiment - 1: Compiler GNU Project GNU Toolchain
Aim: Write a C program to perform bubble sort in linux operaring system and compile it
using gcc and use gdb debugger.
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.
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.
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?
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:
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).
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