Beruflich Dokumente
Kultur Dokumente
{ 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
}
int pid ;
char *args[2] ;
pid = fork() ;
Parent 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 .);
Parent Child
printf(OOOpppssss.\n) ; printf(OOOpppssss.\n) ;
} }
else else
printf(Not .); printf(Not .);
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 .);
These slides show how to parse input from the
command line.
#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 ;
{ char *ptr ;
char input[512] ;
int i ;
while(1)
{ i = read(0, input, 512) ; //read from stdin
ptr = input ;
ptr = 140 T h i s i s
%d: integer
%s: string
%l: long int
%f: float/double
EXAMPLE
These slides show how to use the fork() and exec()
system calls.
Simple program to fork a new process
#include <stdio.h>
main (int argc, char *argv[])
{ int pid ;
char *args[2] ;
printf("Ready to FORK\n") ;
pid = fork() ;
if (pid ==0)
else
printf("I AM THE PARENT!!!\n") ;
}
Simple program to start a new process
executing
#include <stdio.h>
main (int argc, char *argv[])
{ int pid ;
char *args[2] ;
printf("Ready to FORK\n") ;
pid = fork() ;
if (pid ==0)
else
printf("I AM THE PARENT!!!\n") ;
wait(NULL) ;
}
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)
else
printf("I AM THE PARENT!!!\n") ;
//wait(NULL) ;
}