Linux environment Programming Process (V): competition conditions and EXEC functions

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.