Some example c programs
Download
1 / 21

Some Example C Programs - PowerPoint PPT Presentation


  • 186 Views
  • Updated On :

Some Example C Programs. These programs show how to use the exec function. exec Function calls. Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use the one most suited to needs. exec Function calls.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Some Example C Programs' - ludlow


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript


Exec function calls l.jpg
exec Function calls

Used to begin a processes execution.

Accomplished by overwriting process imaged of caller with that of called.

Several flavors, use the one most suited to needs.


Exec function calls4 l.jpg
exec Function calls

int execv( char *path, char *argv[]) ;

path: directory path to executable image. Can be your program, or system program.

argv: Array of pointers to null terminated strings. These are the arguments to the program being executed.


Exec function calls5 l.jpg
exec Function calls

two conventions with argv:

1) argv[0] should hold the name of the program to be executed.

2) The last element must be NULL.


Slide6 l.jpg

main (int argc, *argv[])

{ int pid ;

char *args[2] ;

pid = fork() ;

if (pid ==0)

{ args[0] = “./a.out” ; All executed by

args[1] = NULL ; child process

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not a problem!\n”) ; Executed by parent

}


Slide7 l.jpg

int pid ;

char *args[2] ;

pid = fork() ;


Slide8 l.jpg

Parent

Child

int pid ;

char *args[2] ;

pid = 0

int pid ;

char *args[2] ;

pid = ?

fork


Slide9 l.jpg

Child

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0)

{args[0] = “./a.out” ;

args[1] = NULL ;

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not ….”);


Slide10 l.jpg

Parent

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0)

{args[0] = “./a.out” ;

args[1] = NULL ;

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not ….”);


Slide11 l.jpg

Child

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0)

{args[0] = “./a.out” ;

args[1] = NULL ;

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not ….”);

Parent

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0)

{args[0] = “./a.out” ;

args[1] = NULL ;

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not ….”);


Slide12 l.jpg

Parent

a.out

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0)

{args[0] = “./a.out” ;

args[1] = NULL ;

i = execv(“./aout”, args) ;

printf(“OOOpppssss.\n”) ;

}

else

printf(“Not ….”);



Slide14 l.jpg

#include <unistd.h>

#include <stdio.h>

main()

{ char *ptr ;

char input[1024] ;

int i ;

while(1)

{ i = read(0, input, 512) ; //read from stdin

ptr = input ;

while (*ptr != '\n')

{ if (*ptr == ' ')

printf("Space!\n") ;

else

printf("Read %c\n", *ptr) ;

ptr++ ;

}

}

}


Slide15 l.jpg

#include <unistd.h>

#include <stdio.h>

main()

{ char *ptr ;

char input[512] ;

int i ;

while(1)

{ i = read(0, input, 512) ; //read from stdin

ptr = input ;

In C, the name of an array is the base address of the array.

Now ptr points to the first element of array input.


Slide16 l.jpg

#include <unistd.h>

#include <stdio.h>

main()

{ char *ptr ;

char input[512] ;

int i ;

while(1)

{ i = read(0, input, 512) ; //read from stdin

ptr = input ;

while (*ptr != '\n')

ptr is the address of a character.

*ptr dereferences the pointer to give the

actual value at that address.

T h i s i s

ptr = 140

*ptr = ‘T’

140 141 142 ............................


Slide17 l.jpg

{ if (*ptr == ' ')

printf("Space!\n") ;

else

printf("Read %c\n", *ptr) ;

ptr++ ;

}

printf statement prints string to terminal.

Uses control string and variables.

printf(“Read %c\n”, *ptr) ;

Other control characters are

%d: integer

%s: string

%l: long int

%f: float/double

EXAMPLE



Slide19 l.jpg

Simple program to fork a new process calls.

#include <stdio.h>

main (int argc, char *argv[])

{ int pid ;

char *args[2] ;

printf("Ready to FORK\n") ;

pid = fork() ;

if (pid ==0)

printf("I AM THE CHILD!!\n") ;

else

printf("I AM THE PARENT!!!\n") ;

}


Slide20 l.jpg

Simple program to start a new process executing calls.

#include <stdio.h>

main (int argc, char *argv[])

{ int pid ;

char *args[2] ;

printf("Ready to FORK\n") ;

pid = fork() ;

if (pid ==0)

{ printf("I AM THE CHILD!!\n") ;

args[0] = "./a.out" ;

args[1] = NULL ;

execv("./a.out", args) ;

printf("OPPSSSS\n") ;

}

else

printf("I AM THE PARENT!!!\n") ;

wait(NULL) ;

}


Slide21 l.jpg

Simple program to show how children get out of control if not monitored by the parent.

#include <stdio.h>

main (int argc, char *argv[])

{ int pid ;

char *args[2] ;

printf("Ready to FORK\n") ;

pid = fork() ;

if (pid ==0)

{ printf("I AM THE CHILD!!\n") ;

args[0] = "./a.out" ;

args[1] = NULL ;

execv("./a.out", args) ;

printf("OPPSSSS\n") ;

}

else

printf("I AM THE PARENT!!!\n") ;

//wait(NULL) ;

}


ad