A
when multiple processes attempt to do some sort of processing of shared data, and the final result depends on the order in which the process runs, they are considered competitive. to avoid the conditions of competition, give a code on APUE:
#include "apue.h" static void Charatatime (char *), intmain (void) {pid_t pid; Tell_wait (); /*set things up for Tell_xxx & wait_xxx*/if ((PID = = fork ()) < 0) {Err_sys ("fork Error");} else if (PID = = 0) {wait_parent ();/*tell PARENT WR ' re done*/charatatime ("Output from child.\n");} Else{charatatime ("Output from parent.\n"); Tell_child (PID); /*tell Child we ' re done*/}exit (0);} static void Charatatime (char *str) {char *ptr;int c;setbuf (stdout, NULL); for (ptr = str; (c = *ptr++)! = 0;) putc (c, stdout);}
Two
For basic Process Control primitives, it is mentioned that fork can be used to create a new process, the Exit function and two wait functions handle termination and wait termination, and the EXEC function can execute the new program.
#include <unistd.h>
extern char **environ;
int execl (const char *path, const char *arg, ...);
int execv (const char *path, char *const argv[]);
int execle (const char *path, const char *arg, ..., char * const envp[]);
int Execve (const char *path, const char *arg, ..., char * const envp[]);
int EXECVP (const char *file, char *const argv[]);
int EXECLP (const char *file, const char *arg, ...);
The differences between these functions are:
1, the first 4 go to the path name as the parameter, the last two take the file name as the parameter.
2. It is related to the pass of the parameter table (l means list,v means vector).
3, related to the new program to pass the environment table. The two functions ending in E can pass a pointer to an array of environment string pointers. The other 4 functions use the Environ variable in the calling process to replicate the existing environment for the new program.
Only EXECVE in these 6 functions are system calls to the kernel. The other 5 are just library functions, which eventually call the system call.