CE 466 Operating System Programming
Programming Under UNIX
Computer Engineering Department Yarmouk University
9/21/2008
Steps to execute a ‘c’ program A source file Preprocessor An intermediate source file
Preprocessor directives + C statement
C statements
C compiler An object file Lib of object files
Machine codes + Info for a linker
Linker Executable file
Machine program
Preparing a ‘c’ program Under UNIX %gvim • After starting a text 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. %gcc -c module1.c • Compile each source 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 %myprog param1,param2... file. • 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( ) { body of the function }
Without command line parameters To start: %progname
main(int argc, char *argv[]) { Body of function par2 .. } • %time ↵ • %mkdir NewDire ↵
With one or more command line parameters To start: %progname par1
(No parameters) (one parameter)
Understanding Arguments main(int argc, char *argv[]) main(int argc, char **argv) *argv [ ] 0 1
Command name \0 Parameter 1 name \0
2
Parameter 2 name \0
. .
n
Parameter n name \0
n+1 ∅
NULL pointer
argc = n + 1 n = number of parameters
Understanding Arguments %copy
text1.c
text2.c
argc = 3 *argv [ ] structure C o p y \0 T e x t 1 . c \0 T e x t 2 . c \0
0 1 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 ∅
NULL pointer
argc = n + 1 n = number of parameters
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 ) ; }