Use fork to create a sub-process.
Code:
- # Include <unistd. h>
- # Include <stdio. h>
- Int main (INT argc, char ** argv)
- {
- Int A = 10;
- Pid_t PID;
- Printf ("before fork pid = % d/N", getpid ());
- PID = fork ();
- If (PID <0 ){
- Printf ("fork failed! /N ");
- Return-1;
- }
- If (pid = 0 ){
- A ++;
- Printf ("this is child process! Parent pid = % d, my pid = % d, pid = % d, A = % d/N ", getppid (), getpid (), PID, );
- }
- Else {
- Printf ("this is parent process! My pid = % d, child pid = % d, A = % d/N ", getpid (), PID, );
- }
- Return 0;
- }
Compile: [root @ localhost/] # gcc-O fork. c
Result:
[Root @ localhost/] #./fork
Before fork pid = 23703
This is child process! Parent pid = 23703, my pid = 23704, pid = 0, A = 11
This is parent process! My pid = 23703, child pid = 23704, A = 10
Next, clone the system call.
Code:
- # Include <stdlib. h>
- # Include <sched. h>
- # Include <signal. h>
- # Include <unistd. h>
- # Include <malloc. h>
- # Include <fcntl. h>
- # Include <sys/types. h>
- # Include <sys/STAT. h>
- # Include <sys/Wait. H>
- Char * prog_argv [4];
- Int Foo;
- Int FD;
- # Define child_stack (1024*64)
- Int thread_function (void * argument)
- {
- Printf ("Child: Child thread begin.../N ");
- Fool = 2008;
- Close (FD );
- Execvp (prog_argv [0], prog_argv );
- Printf ("Child: Child thread exit, this line won't print out./N ");
- Return 0;
- }
- Int main ()
- {
- Char C;
- Char * stack;
- Pid_t PID;
- Fool = 2007;
- FD = open ("/etc/passwd", o_rdonly );
- If (FD <0 ){
- Perror ("open ");
- Exit (-1 );
- }
- Printf ("parient: The variable Foo was: % d/N", foo );
- If (read (FD, & C, 1) <1 ){
- Perror ("parient: File Read error/N ");
- Exit (1 );
- }
- Else
- Printf ("parient: We cocould read from the file: % C/N", C );
- Prog_argv [0] = "/bin/ls ";
- Prog_argv [1] = "-1 ";
- Prog_argv [2] = "/";
- Prog_argv [3] = "null ";
- Stack = (char *) malloc (child_stack );
- If (stack = NULL ){
- Perror ("malloc: cocould not allocate stack ");
- Exit (2 );
- }
- Printf ("parent: creating child thread/N ");
- PID = clone (thread_function, (void *) (stack + child_stack ),
- Sigchld | clone_fs | clone_sighand | clone_vm, null );
- If (pid =-1 ){
- Perror ("clone ");
- Exit (3 );
- }
- Printf ("parent: waiting for the finish of child thread: % d/N", pid );
- PID = waitpid (PID, 0, 0 );
- If (pid =-1 ){
- Perror ("wait ");
- Exit (4 );
- }
- Free (stack );
- Printf ("parent: Child thread returned and stack free./N ");
- Printf ("parent: The variable Foo now is % d/N", foo );
- If (read (FD, & C, 1) <1 ){
- Perror ("parent: File Read error/N ");
- Exit (5 );
- }
- }
- Else
- Printf ("parent: We cocould read from file: % C/N", C );
- Return 0;
- }
Compile and execute:
[Root @ localhost/] # gcc-O Clone. C-lpthread
[Root @ localhost/] #./clone
Parient: The variable Foo was: 2007
Parient: We cocould read from the file: R
Parent: creating child thread
Parent: waiting for the finish of child thread: 24951
Child: Child thread begin...
Child: Child thread exit, this line won't print out.
Parent: Child thread returned and stack free.
Parent: The variable Foo now is 2008
Parent: We cocould read from file: O