Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Operating System (22CS005)

Program 3:To illustrate the basic of GCC, compilation and execution of


a program.
SOLUTION :
GCC:The GNU Compiler Collection, commonly known as GCC, is a set of
compilers and development tools available for Linux, Windows, various
BSDs, and a wide assortment of other operating systems.It is a compiler suite
that supports various programming languages such as C, C++, Objective-C,
Fortran, and others. It is a key component of the GNU Toolchain for
developing applications and writing operating systems. The current version is
GCC 7.3, released on 2018-01-25. The GNU C and C++ compilers are called
gcc and g++, respectively. The most important option required while
compiling a source code file is the name of the source; rest every argument is
optional like a warning, debugging, linking libraries, object file, etc.

• To compile and execute a C program using GCC, we can follow these steps
as follows:
OUTPUT:
• 1. Create a C program and save the program with a “.c” extension.

2.Compile the program: Use the gcc command to compile the C program into

Operating System (22CS005)


Operating System (22CS005)

3.Execute the program: After the successful compilation(i.e. without


any error), we can run the compiled program by command “./a.out ”.

We can run the program by above command “./a.out”but it’s an executable


file ,we can also change the name of the executable file by command “gcc

These above basic steps demonstrate how to compile and execute a C program
using the GCC compiler .

Operating System (22CS005) Page 9


Operating System (22CS005)

Program 4:Implement the basic concept of process using C language.

SOLUTION: A process is the basic unit of execution in a program. Each


process has its own address space and usually one thread of control. It
executes a program and can create child processes, which inherit many of
their attributes from the parent process.Additionally, a process is a program in
execution, and when a program is run, it becomes a process, which is an
'active' entity, as opposed to a program, which is considered a 'passive' entity.
A process is stored in the Main memory while program is stored in Secondary
memory.Each process has its own address space and usually one thread of
control. Additionally, a process is named by a unique process ID (PID)
allocated to it when it is created.
There are two types of process ID (PID):
(i) Parent process ID(returns positive fork() value).
(ii) Child process ID(returns zero fork()value) .
OUTPUT:
• getpid(): The g e t p i d ( ) function in C is used to retrieve the process ID
(PID) of the calling process.

Operating System (22CS005) Page 10


Operating System (22CS005)

• getppid():The getppid() function in C is used to retrieve the parent process


ID of the calling process.

• To determine the child process id and parent process id of the program,

Operating System (22CS005) Page 11


Operating System (22CS005)

(ii) Zero value: fork() return zero value for child process.
(iii) Negative value: fork() return negative value for an unsuccessful
fork(rare chances).
OUTPUT:
• To execute a process using the fork value in C, you can use
the fork system call to create a new process,
For 1 fork():

For 2 fork():

Page 13
Operating System (22CS005)
Operating System (22CS005)

• fork() with or(||) logical operator.

Explanation:
1. It will create two process one parent P (has process ID of child process)and other
is child C1 (process ID = 0).
2. In if statement we used OR operator( || ) and in this case second condition is
evaluated when first condition is false.
3. Parent process P will return positive integer so it directly execute statement and
create two more processes (one parent P and other is child C2). Child process C1
will return 0 so it checks for second condition and second condition again create
two more processes(one parent C1 and other is child C3).
4. C1 return positive integer so it will further create two more processes (one parent
C1 and other is child C4). Child C3 return 0 so it will directly print 1.

Operating System (22CS005) Page 15


Operating System (22CS005)

• exec():The e x e c ( ) system call is a functionality of an operating system that


replaces the current process image with a new process image constructed
from a regular, executable file called the new process image file. This is
especially important in Unix-like systems, although it exists elsewhere.
When a program is invoked with one of the exec functions, the machine
code, data, heap, and stack of the process are replaced by those of the new
program. The exec call is available for many programming languages
including compilable languages and some OS command interpreters.

Operating System (22CS005) Page 16


Operating System (22CS005)

Operating System (22CS005) Page 17

You might also like