Information Security System Design Foundation 12th Week study Summary

Source: Internet
Author: User
Tags define function

Eighth chapter code test run

1:env folder -environ.c code

#include <stdio.h>

#include <stdlib.h>

int main (void)

{

printf ("path=%s\n", getenv ("PATH")),//getenv function is used to get the value of the parameter PATH environment variable, and the execution succeeds then returns a pointer to that content

Setenv ("PATH", "Hello", 1);//See below for explanations

printf ("path=%s\n", getenv ("PATH"));

#if 0

printf ("path=%s\n", getenv ("PATH"));

Setenv ("PATH", "Hellohello", 0);

printf ("path=%s\n", getenv ("PATH"));

printf ("my_ver=%s\n", getenv ("My_ver"));//Version

Setenv ("My_ver", "1.1", 0);

printf ("my_ver=%s\n", getenv ("My_ver"));

#endif

return 0;

}

EXECVP () finds the file name in the directory that the PATH environment variable refers to, executes the file after it is found, and then passes the second parameter argv to the file that you want to execute.

Return value: If the execution succeeds the function will not return, and the execution failure will return 1 directly, and the reason for failure is stored in errno.

getenv function
1. Functions to obtain the value of an environment variable
2. The parameter is the environment variable name name, such as "HOME" or "PATH". If the environment variable is present, then the GETENV function returns the value of the environment variable, the first address of value, or null if the environment variable does not exist: the getenv function returns
setenv function
1. Functions to modify or add environment variables
2. Set name to Value

2:exec 2 and 1 run the same result as EXEC1.

The difference between EXEC2 and EXEC1 is that the first parameter in EXEVP is not the same, EXEC1 is LS,EXEC2].

3:EXEC3 calls the EXECLP function, just as it does with its running results.

Header file: #include

define function: int EXECLP (const char * file,const char * arg,....);

Correlation function: FORK,EXECL,EXECLE,EXECV,EXECVE,EXECVP

Return value: If the execution succeeds the function will not return, and the execution failure will return 1 directly, and the reason for failure is stored in errno.

This code specifies the environment variable, and then still executes the ls-l instruction, and does not return after success, so the last word will not be output. The running result is the same as EXEC1.

4:FORKDEMO1: The child process PID is printed first, after resting for one second, according to fork () the nature of the return of two times, the parent process returns the child process PID, the child process returns 0.

5:forkdemo2 calls two fork and produces four sub-processes, so four aftre outputs are printed.

6:FORKDEMO3: Call fork () causes the parent process to print the child process PID, and the child process prints its own PID.

7:forkdemo4 first print the process PID, and then fork to create the child process, the parent process returns the child process PID, so output the parent sentence, sleep 10 seconds, the child process returns 0, so the output of children and the following sentence.

8:FORKGDB Two processes print a bunch of numbers sequentially, the parent process prints the first two sentences, then sleeps for a second, then prints a sentence, the child process first prints a sentence, then sleeps for a second, then prints two sentences.

9:TESTBUF1: In turn, you enter the instruction and parameters to execute, the carriage return indicates the end of the input, then each parameter entered corresponds to the function, and then the corresponding instruction is called.

The first is the program name, followed by the program parameters. A string that constructs the argument list argist, and finally adds null at the end of the array, passing arglist[0] and arglist arrays to EXECVP.

The program runs normally, the program code specified by the EXECVP command overrides the shell program code and exits after the command ends, and the shell cannot accept the new command.

TESTBUF2: The use of fflush (stdout) after printf () is the output of the content that will be output immediately.

When the printf () function is used, the system stores the content in the output buffer and outputs it when the time slice is rotated to the output program of the system.

The testbuf3:fprintf function is to format the output to a stream, which can be a file or standard input, output, and error.

fprintf (stderr, "%s", s); fprintf (stdout, "%s", s); printf ("%s", s);

The three statement effect is basically the same, different in the redirection, only the first one will be on the screen output, the rest will be output to the redirected file.

10:testsystem called the system function

Header file: #include <stdlib.h> definition function: NT system (char* command);

11:waitdemo1

Call the Wait function: header file: #include <sys/types.h>

Define function: pid_t wait (int* status);

Return value: Success returns the child process PID, the failure returns 1, the reason is stored in errno.

Waitdemo2: Similar to Waitdemo1, the difference is that the end state value of the child process is printed in three parts.

12:mkfifo function header file: #include <sys/types.h> #include <sys/stat.h>

define function: int mkfifo (const char *pathname,mode_t mode)

Return value: If the execution succeeds the function will not return, and the execution failure will return 1 directly, and the reason for failure is stored in errno.

Reference: 20135237 of Blogs

Learn the textbook to understand the computer system deeply

Information Security System Design Foundation 12th Week study Summary

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.