You are on page 1of 3

The ‘exec( )’ and’ suid bit function LAB # 10

LAB#10
LAB # 2
OBJECTIVE
Study the features of the ‘exec( )’ Function.

THEORY
Nowadays we have PC’s, which comes with over 64MB of RAM. But this has not
stopped people from desiring more and more memory. There never seems to be enough
of it. And if we are going to run a program that is tens of thousands lines long, we are
going to reach the limit sooner or later.
In C, there is a way to work around this. UNIX believes that small and simple is
beautiful. And that small program can be built that handle only one task. That these in
turn can call others and those others will call others and so on till we have a synergy of
small processes that come to life, execute, bring another process to life and die in this
process of creation.

So, at a point in time we have only one process in memory. Since a copy of program is
not made like with the fork( ) function, won’t this help in freeing memory.
There is a family of functions that enables us to create another process. But once the
other process is initiated, the parent process can never be given control again since its
memory space is overwritten by the new process.
This new process however has the same PID as the original process. Not only the PID but
also the parent process ID, current directory, and file descriptor tables – if any are open –
also remain the same.
There are two examples listed below. Which will explain the concept of the exec( )
function adequately.
Compile the following programs in this manner:
gcc-o<object filename> <source filename>

gcc-oex1 ex1.c
gcc-oex2 ex2.c

This is necessary because when we compiled normally, we did so by just saying

gcc <sourcefilename.c>. And the resultant file was always a.out. But to explain the
exec( ) family we need to resort to two programs since one is calling another. If we were
to resort to old method of compiling we would always find the program ex1.c which
becomes a.out being overwritten by the a.out got from compiling ex2.c. By compiling in
the new manner shown we will get two executable files, ex1 and ex2. For reference
consider Program 1and Program 2.

Submitted By: Ali Murad


Submitted To: Muhib Khan
The ‘exec( )’ and’ suid bit function LAB # 10

Don’t run Program 1 till you have compiled Program 2.


Now run Program 1.
Now run Program 2.
Every process has two IDs: a real user and an effective user Id.And it is actually the
effective user ID that UNIX checks to see what permissions are valid for the user.
Let.s understand this a little moreexplixity, every file has user ID.This reflects the owners
ID.When the program or file is activated,the kernel checks the effective user ID.This
latter is always the real user ID,unless change.For refrence consider Program 1 and
Program 2
Example programs
Program 1
ex1.c

main ( )
{
printf (“Before exec my ID is %d \n”,getpid( ));
printf (“My parent process’s id is %d\n”,getppid( ));
printf(“exec starts\n”);
execl(“/usr/guest/ex2”,”ex2”,(char*)0);
printf(“this will not print\n”);
}

Program 2
ex2.c

main( )
{
printf(“After the exec my process id is %d\n”,getpid( ));
printf(“My parent process’s id is %d\n”,getppid( ));
printf(“exec ends\n”);
}

Program 3
main()
{
printf(“User Id:%d\n”,getuid( ));
printf(“Effective User Id:%d\n”,geteuid ( ));
}

Exercises

1. Run the following Programs.

Submitted By: Ali Murad


Submitted To: Muhib Khan
The ‘exec( )’ and’ suid bit function LAB # 10

Program 1
main ( )
{
printf (“Before exec my ID is %d \n”,getpid( ));
printf (“My parent process’s id is %d\n”,getppid( ));
printf(“exec starts\n”);
execl(“/usr/guest/ex2”,”ex2”,(char*)0);
printf(“this will not print\n”);
}
Output:[1] 3294
Before exec my id is 3294.
My parent process id is 2114
Exec starts
This will not print
[1] + Exit 20

Program 2
main( )
{
printf(“After the exec my process id is %d\n”,getpid( ));
printf(“My parent process’s id is %d\n”,getppid( ));
printf(“exec ends\n”);
}
Output:[1] 3251
After the exec my process id is 3251.
My parent process id is 2114.
Exec ends.
After performance second program in first program then the output will be:

Program 3
main()
{
printf(“User Id:%d\n”,getuid( ));
printf(“Effective User Id:%d\n”,geteuid ( ));
}
Output:[1] 3437
User id: 1000
Effective user id :1000
[i]+ exit 24

Submitted By: Ali Murad


Submitted To: Muhib Khan

You might also like