CE 466
Operating System Programming
Programming Under UNIX
Computer Engineering Department
Yarmouk University
9/21/2008
Steps to execute a ‘c’ program
Preprocessor
A source file
directives +
C statement
Preprocessor
An intermediate C statements
source file
C compiler
Machine codes +
An object file
Info for a linker
Lib of
object Linker
files
Machine
Executable file
program
Preparing a ‘c’ program Under UNIX
• After starting a text %gvim
editor (for example vi . . . .
working with gvim
or PICO), create and . . . .
save a source text file
(for example
myprog.c).
• Compile and link the %gcc -o myprog myprog.c
program. The result is
executable file (for
example, myprog). %myprog or
%myprog param1,param2...
• Start your program.
Preparing a ‘c’ program Under UNIX
• Create and save each
source file.
• Compile each source %gcc -c module1.c
file separately. The %gcc -c module2.c
result is object files %gcc -c module3.c
(module1.o, module2.o,
module3.o)
%gcc -o myprog module1.o
module2.o module3.o
• Link the object files.
The result is executable %myprog or
file. %myprog param1,param2...
• Start your program.
Make Utility
#possible makefile
edit : main.o kbd.o command.o display.o insert.o \
search.o files.o utils.o
gcc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
main.o : main.c defs.h
gc -c main.c
kbd.o : kbd.c defs.h command.h
gcc -c kbd.c
command.o : command.c defs.h command.h
gcc -c command.c
files.o : files.c defs.h buffer.h command.h
gcc -c files.c
…………….
utils.o : utils.c defs.h
gcc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
Make Utility
#possible makefile
prog1 : main.o kbd.o command.o display.o insert.o \
search.o files.o utils.o
cc -o prog1 main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c Executable File
command.o : command.c defs.h command.h
cc -c command.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c Target Files
……………. or Prerequisites
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm prog1 main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
Commands and C Programs
main( ) Without command line parameters
{ To start: %progname
body of the function
}
main(int argc, char *argv[]) With one or more command
{ line parameters
Body of function To start: %progname par1
par2 ..
}
• %time ↵ (No parameters)
• %mkdir NewDire ↵ (one parameter)
Understanding Arguments
main(int argc, char *argv[])
main(int argc, char **argv)
*argv [ ]
0 Command name \0
1 Parameter 1 name \0
2 Parameter 2 name \0
.
.
n Parameter n name \0
n+1 ∅
argc = n + 1
n = number of parameters
NULL pointer
Understanding Arguments
%copy text1.c text2.c
argc = 3
*argv [ ] structure
C o p y \0
0
1 T e x t 1 . c \0
T e x t 2 . c \0
2
∅
Understanding Arguments
main(int argc, char *argv[], char *envp[])
(use pointers to access envp)
*envp [ ]
0 “HOME = /home/user1”
1 “SHELL = /usr/bin/csh”
2
.
.
n Env Parameter #n \0
n+1 ∅
argc = n + 1
n = number of parameters
NULL pointer
Using Arguments
/* source myprog.c , executable myprog */
#include <stdio.h>
main(int argc, char *argv[])
{ int Num;
if ( argc < 2 )
{
cout << “Usage : “ << argv[0]
<< “parameter\n”;
exit ( 1 ) ;
}
cout << “Starting program ” << argv[0] << endl;
cout << “with “ << argc-1 << “parameter(s)\n” ;
cout << “First parameter is “ << argv[1];
Num = atoi(argv[1]);
exit ( 0 ) ;
}
Using Arguments
/* source myprog.c , executable myprog */
#include <stdio.h>
main(int argc, char *argv[])
{ int Num;
if ( argc < 2 )
{
printf( “Usage : %s parameter\n”, argv[0] ) ;
exit ( 1 ) ;
}
printf(“Starting program %s \n”, argv[0] ) ;
printf(“with %d parameter(s)\n”, argc-1 ) ;
printf(“First parameter is %s\n”, argv[1] ) ;
Num = atoi(argv[1]);
exit ( 0 ) ;
}