You are on page 1of 39

Advanced Topics in C Programming

Programming under Linux

By: Mohamed Ahmed Saad

Mohamed Saad

Day1

1/2

1- System and Process Information.


our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Day1
2- Process Control.

2/2

Process )den"i*iers# Dup'ica"ing a Process )mage# S"ar"ing ,e- Processes# .ep'acing a Process )mage#

Mohamed Saad

Day1
1- System and Process Information.

our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=1/127

Your First Linux C Program


//1 //2

(1/ !.

)n *i'e he''o#c /inc'ude 0s"dio#h1 8 prin"*29he''o3 -or'd:n97; exi"2=7; ?


Mohamed Saad 5

in" main2in" argc3 char 4argv567


//<

//>

Sys"em @ Process )n*orma"ion

2=2/127

Com"ile and #un Program$ Agcc $o he''o#exe he''o#c A#/he''o#exe


he''o3 -or'd

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=</127

Your First Linux C Program


//1

(%/ !.

/inc'ude 0s"dio#h1

)nc'uded *i'e -here *unc"ions 'iBe 2prin"*7 is de*ined#

in" main2in" argc3 char 4argv567

//2

int : re"urned va'ue o* "he program 2represen"s program s"a"us3 =: succeed3 o"her-ise: *ai'ed#
To prin" re"urn resu'" o* your program Aecho AC

&rgc : argumen" coun" 2 num&er o* argumen"s passed "o "he program7# &rg' : an array o* charac"er poin"ers#
!irs" en"ry is a poin"er "o program name# Su&seDuen" en"ries poin" "o su&seDuen" s"rings on "he command 'ine#
Mohamed Saad 7

Sys"em @ Process )n*orma"ion

2=>/127

Your First Linux C Program prin"*29he''o3 -or'd:n97;


//<

( / !.

Prin"s Ehe''o3 -or'dF on "he screen

exi"2=7;

//>

)ndica"es "ha" "he program execu"ed success*u''y#

Mohamed Saad

Day1
1- System and Process Information.

our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=J/127

Command-Line &rguments
in *i'e Eechoarg#cF

(1/2!

/inc'ude 0s"dio#h1 /inc'ude 0s"d'i&#h1 in" main2in" argc3 char 4argv567 8 in" i; *or 2i G =; i 0 argc; iHH7 prin"*29argv5Id6: Is:n93 i3 argv5i67; exi"2=7; ?
Mohamed Saad 1

Sys"em @ Process )n*orma"ion

2=K/127

Command-Line &rguments

(2/2!

A #/echoarg arg1 T%ST *oo argv5=6: #/echoarg argv516: arg1 argv526: T%ST argv5<6: *oo A

Mohamed Saad

Day1
1- System and Process Information.

our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=L/127

(n'ironment 'aria)les %nvironmen" Lis":

(1/ !$

%ach program is a'so passed an environmen" 'is"# LiBe "he argumen" 'is"3 "he environmen" 'is" is an array o* charac"er poin"ers3 -i"h each poin"er con"aining "he address o* a nu''$ "ermina"ed C s"ring# The address o* "he array o* poin"ers is con"ained in "he g'o&a' varia&'e environ: ex"ern char 44environ;

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=M/127

%nvironmen" Lis":
environment pointer environment List environment StrinHOME !home!msaad"0 #$%H :!&sr!'i(!mit!s(in:"0 SHELL !(in!(ash"0 )SE* msaad"0 LO+,$ME msaad"0 ,)LL

environ:

Mohamed Saad

Sys"em @ Process )n*orma"ion

2=P/127

%nvironmen" Naria&'es

(%/ !

The environmen" s"rings are usua''y o* "he *orm nameGva'ue

Oe" %nvironmen" va'ue:

/inc'ude 0s"d'i&#h1 char 4ge"env2cons" char 4name7; .e"urns: poin"er "o va'ue associa"ed -i"h name3 ,(LL i* no" *ound

Mohamed Saad

Sys"em @ Process )n*orma"ion

21=/127

%nvironmen" Naria&'es
Se" %nvironmen" va'ue:

( / !

/inc'ude 0s"d'i&#h1 in" pu"env2char 4s"r7; .e"urns: = i* QR3 nonSero on error#

Mohamed Saad

Mohamed Saad

Day1
1- System and Process Information.

our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Sys"em @ Process )n*orma"ion

211/127

*ser Information Pass-ord !i'e:

(1/1!

pass-d !i'e *ie'ds are on"ained in a pass-d s"ruc"ure "ha" is de*ined in 0p-d#h1

/inc'ude 0p-d#h1 s"ruc" pass-d 4ge"p-uid 2uidT" uid7; s"ruc" pass-d 4ge"p-nam 2cons" char 4name7;
Bo"h re"urn: poin"er i* QR3 ,(LL on error Mohamed Saad 1

Day1
1- System and Process Information.

our !irs" Linux C Program# Command$Line Argumen"s# %nvironmen" varia&'es# (ser )n*orma"ion# +os" )n*orma"ion#

Mohamed Saad

Sys"em @ Process )n*orma"ion +ost Information


/inc'ude 0sys/u"sname#h1 in" uname2s"ruc" u"sname 4name7;
.e"urns: = va'ue i* QR3 $1 on error s"ruc" u"sname 8 char sysname56; /4 name o* "he opera"ing sys"em 4/ char nodename56; /4 name o* "his node 4/ char re'ease56; /4 curren" re'ease o* opera"ing sys"em 4/ char version56; /4 curren" version o* "his re'ease 4/ char machine56; /4 name o* hard-are "ype 4/ ?; Mohamed Saad

212/127

(1/1!

Mohamed Saad

Day1
2- Process Control.

Process )den"i*iers# Dup'ica"ing a Process )mage# S"ar"ing ,e- Processes# .ep'acing a Process )mage#

Mohamed Saad

Process Con"ro'

Process )den"i*iers

(1/1!

/inc'ude 0unis"d#h1 pidT" ge"pid2void7;


.e"urns: process )D o* ca''ing process

pidT" ge"ppid2void7;
.e"urns: paren" process )D o* ca''ing process

uidT" ge"uid2void7;
.e"urns: rea' user )D o* ca''ing process

gidT" ge"gid2void7;
.e"urns: rea' group )D o* ca''ing process 2

Mohamed Saad

Day1
2- Process Control.

Process )den"i*iers# Dup'ica"ing a Process )mage# S"ar"ing ,e- Processes# .ep'acing a Process )mage#

Mohamed Saad

Process Con"ro'

Dup'ica"ing a Process )mage

(1/,!

An exis"ing process can crea"e a ne- one &y ca''ing "he *orB *unc"ion#

/inc'ude 0unis"d#h1 pidT" *orB2void7;


.e"urns: = in chi'd3 process )D o* chi'd in paren"3 $1 on error

Mohamed Saad

Process Con"ro'

Dup'ica"ing a Process )mage


.nitia' #ro/ess

(2/,!

0or123 *et&rns a /hi'd #ro/ess .d *et&rns 4ero

Ori-in pro/ess /ontin&e

5hi'd #ro/ess

Mohamed Saad

Process Con"ro'

Dup'ica"ing a Process )mage

(%/,!

An exis"ing process can crea"e a ne- one &y ca''ing "he *orB *unc"ion#

/inc'ude 0unis"d#h1 pidT" *orB2void7;


.e"urns: = in chi'd3 process )D o* chi'd in paren"3 $1 on error

Mohamed Saad

Process Con"ro'
pidT" ne-Tpid; ne-Tpid G *orB27; s-i"ch2ne-Tpid78 case $1: /4%rror4/ &reaB; case =: /4Chi'd4/ &reaB; de*au'" : /4Paren"4/ ?

Mohamed Saad

Process Con"ro'

Dup'ica"ing a Process )mage

(,/,!

/inc'ude 0sys/-ai"#h1 pidT" -ai"2in" 4s"a"'oc7;

.e"urns: process )D i* QR3 = 2see 'a"er73 or 1 on error# )*

s"a"T'oc is no" a nu'' poin"er3 "he exi" s"a"us o*

"he chi'd process -i'' &e -ri""en "o "he 'oca"ion "o -hich i" poin"s

Mohamed Saad

Mohamed Saad

Day1
2- Process Control.

Process )den"i*iers# Dup'ica"ing a Process )mage# S"ar"ing ,e- Processes# .ep'acing a Process )mage#

Mohamed Saad

Process Con"ro'

S"ar"ing ,e- Processes


inside ano"her program#

(1/1!

ou can cause a program "o run *rom /inc'ude 0s"d'i&#h1 in" sys"em 2cons" char 4cmds"ring7;

Mohamed Saad

Day1
2- Process Control.

Process )den"i*iers# Dup'ica"ing a Process )mage# S"ar"ing ,e- Processes# .ep'acing a Process )mage#

Mohamed Saad

Process Con"ro'

.ep'acing a Process )mage

(1/2!

Uhen a process ca''s one o* "he exec *unc"ions3 "ha" process is comp'e"e'y rep'aced &y "he ne- program3 and "he ne- program s"ar"s execu"ing a" i"s main *unc"ion# The process )D does no" change across an exec3 &ecause a ne- process is no" crea"ed; exec mere'y rep'aces "he curren" process i"s "ex"3 da"a3 heap3 and s"acB segmen"s -i"h a &rand ne- program *rom disB#

Mohamed Saad

Process Con"ro'

.ep'acing a Process )mage

(2/2!

/inc'ude 0unis"d#h1

in" exec'2cons" char 4pa"hname3 cons" char 4arg=3 in" execv2cons" char 4pa"hname3 char 4cons" argv 567;

### /4 2char 47= 4/ 7;

Mohamed Saad

Mohamed Saad

Mohamed Saad

Mohamed Saad

You might also like