The 12th week summary of the Design foundation of information security system

Source: Internet
Author: User
Tags define function

EXEC1
#include <stdio.h>
#include <unistd.h>
int main ()
{
CHAR*ARGLIST[3];
ARGLIST[0] = "ls";
ARGLIST[1] = "-L";
ARGLIST[2] = 0;//null
printf ("* * * ~ to exec ls-l\n");
EXECVP ("ls", arglist);
printf ("* * * * ls is done. Bye ");
return 0;
}

The EXEC system call clears the current program's machine instructions from the current process, then loads the program code specified at the time of the call in the empty process, and finally runs the new program.

EXECVP function: From the directory referred to by the PATH environment variable to find the file name that conforms to the parameter files, and then execute the file, and then pass the second parameter argv to the file to execute, if the execution succeeds the function will not return, execution failure will return 1 directly, the failure reason is stored in errno.

Required header File #include <unistd.h>
Function prototype EXECVP (const char *file,char *const argv[])
function return value 1: Error

EXEC2
#include <stdio.h>
#include <unistd.h>
int main ()
{
CHAR*ARGLIST[3];
ARGLIST[0] = "ls";
ARGLIST[1] = "-L";
ARGLIST[2] = 0;
printf ("* * * ~ to exec ls-l\n");
EXECVP (Arglist[0], arglist);
printf ("* * * * ls is done. Bye\n ");
}

Difference: The first parameter of the EXEVP function, EXEC1 is ls,exec2 directly with the arglist[0], but the two are equivalent, the result is the same.

EXEC3
#include <stdio.h>
#include <unistd.h>
int main ()
{
CHAR*ARGLIST[3];
Char *myenv[3];
Myenv[0] = "Path=:/bin:";
MYENV[1] = NULL;
ARGLIST[0] = "ls";
ARGLIST[1] = "-L";
ARGLIST[2] = 0;
printf ("* * * ~ to exec ls-l\n");
EXECV ("/bin/ls", arglist);
EXECVP ("ls", arglist);
EXECVPE ("LS", arglist, myenv);
EXECLP ("ls", "ls", "-l", NULL);
printf ("* * * * ls is done. Bye\n ");
}

EXECLP function: Call the EXECLP function to execute the command, the EXECLP function is a variadic function, the first parameter needs to set the system environment variables can be obtained in the command file, or set the absolute path of the command file, the last parameter must be set to NULL, So that the parameters of this function are set to complete, all the parameters in the middle are set to the parameters of the first command, and the output redirection is done.

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.

In summary, the name of the EXEC function and its corresponding meaning

Forkdemo1
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
Intret_from_fork, Mypid;
Mypid = Getpid ();
printf ("Before:my pid is%d\n", mypid);
Ret_from_fork = fork ();
Sleep (1);
printf ("After:my pid is%d, fork () said%d\n",
Getpid (), ret_from_fork);
return 0;
}

Effect: Print the process PID first, and then call the fork function to generate the child process, hibernate one second after the process ID printing, the parent process to print the child process PID, the child process returned 0

Forkdemo2
#include <stdio.h>
#include <unistd.h>
int main ()
{
printf ("Before:my pid is%d\n", getpid ());
Fork ();
Fork ();
printf ("Aftre:my pid is%d\n", getpid ());
return 0;
}

Effect: Calls two times fork, produces four sub-processes, prints out four aftre and outputs

Forkdemo3
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ()
{
INTFORK_RV;
printf ("Before:my pid is%d\n", getpid ());
FORK_RV = fork ();
if (FORK_RV = =-1)
Perror ("fork");

printf ("I am the Child. ") My pid=%d\n ", Getpid ());

Exit (0);
}
else{
printf ("I am the parent.") My child is%d\n ", FORK_RV);
Exit (0);
}
return 0;
}

Effect: Fork produces a child process, the parent process returns the child process PID, not 0, so the output of the parent process of the sentence, the child process returns 0, so the child process is output

Forkdemo4
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ()
{
INTFORK_RV;
printf ("Before:my pid is%d\n", getpid ());
FORK_RV = fork ();/* Create New process*/
if (FORK_RV = =-1)/* Check for error*/
Perror ("fork");

printf ("I am the Child. ") My pid=%d\n ", Getpid ());
printf ("Parent pid=%d, my pid=%d\n", Getppid (), Getpid ());
Exit (0);
}
else{
printf ("I am the parent.") My child is%d\n ", FORK_RV);
Sleep (10);
Exit (0);
}
return 0;
}

Effect: 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, hibernate 10 seconds, the child process returns 0, so the output children and the next sentence.

Forkgdb
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int  gi=0;
int main ()
{
int li=0;
static int si=0;
int i=0;
pid_t pid = fork ();
if (PID = =-1) {
Exit (-1);
}
else if (PID = = 0) {
for (i=0; i<5; i++) {
printf ("Child li:%d\n", li++);
Sleep (1);
printf ("Child gi:%d\n", gi++);
printf ("Child si:%d\n", si++);
}
Exit (0);

}
else{
for (i=0; i<5; i++) {
printf ("Parent li:%d\n", li++);
printf ("Parent gi:%d\n", gi++);
Sleep (1);
printf ("Parent si:%d\n", si++);
}
Exit (0);

}
return 0;
}

Effect: The parent process prints two sentences first, then sleeps for one second, then prints a sentence, the child process first prints a sentence, then sleeps for a second, and then prints two sentences. And these two threads are concurrent, so you can see that one thread sleeps in the second, another thread is executing, and the threads are independent of each other.

In conclusion, the fork () function creates a process that is almost identical to the original process through a system call, that is, two processes can do exactly the same thing, but two processes can do different things if the initial parameters or the variables passed in are different. After a process calls the fork () function, the system assigns resources to the new process, such as the empty room where the data and code are stored. All the values of the original process are then copied to the new new process, with only a few values that are different from the value of the original process. The equivalent of cloning a self.

Fork Function Property: called once, but returns two times, it may have three different return values:
1) In the parent process, fork returns the process ID of the newly created child process;
2) in the sub-process, fork returns 0;
3) If an error occurs, fork returns a negative value;

After the fork function finishes executing, if the new process is created successfully, two processes are present, one is a child process and one is the parent process. In the subprocess, the fork function returns 0, and in the parent process, fork returns the process ID of the newly created child process. We can determine whether the current process is a child process or a parent process by the value returned by the fork.

Psh1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#defineMAXARGS20
#defineARGLEN100
int execute (char *arglist[])
{
EXECVP (Arglist[0], arglist);
Perror ("EXECVP failed");
Exit (1);
}
char * makestring (char *buf)
{
CHAR*CP;
Buf[strlen (BUF)-1] = ' + ';
CP = malloc (strlen (BUF) +1);
if (cp = = NULL) {
fprintf (stderr, "no memory\n");
Exit (1);
}
strcpy (CP, BUF);
return CP;
}
int main ()
{
CHAR*ARGLIST[MAXARGS+1];
Intnumargs;
Charargbuf[arglen];
Numargs = 0;
while (Numargs < Maxargs)
{
printf ("arg[%d", Numargs);
if (Fgets (Argbuf, Arglen, stdin) && *argbuf! = ' \ n ')
arglist[numargs++] = makestring (ARGBUF);
Else
{
if (Numargs > 0) {
Arglist[numargs]=null;
Execute (arglist);
Numargs = 0;
}
}
}
return 0;
}

Function: The code corresponds to the instruction you enter 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.

Psh2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#defineMAXARGS20
#defineARGLEN100
Char *makestring (char *buf)
{
CHAR*CP;
Buf[strlen (BUF)-1] = ' + ';
CP = malloc (strlen (BUF) +1);
if (cp = = NULL) {
fprintf (stderr, "no memory\n");
Exit (1);
}
strcpy (CP, BUF);
return CP;
}
void execute (char *arglist[])
{
Intpid,exitstatus;
PID = fork ();
Switch (PID) {
Case-1:
Perror ("fork failed");
Exit (1);
Case 0:
EXECVP (Arglist[0], arglist);
Perror ("EXECVP failed");
Exit (1);
Default
while (Wait (&exitstatus)! = pid)
;
printf ("Child exited with status%d,%d\n",
Exitstatus>>8, exitstatus&0377);
}
}
int main ()
{
CHAR*ARGLIST[MAXARGS+1];
Intnumargs;
Charargbuf[arglen];
Numargs = 0;
while (Numargs < Maxargs)
{
printf ("arg[%d", Numargs);
if (Fgets (Argbuf, Arglen, stdin) && *argbuf! = ' \ n ')
arglist[numargs++] = makestring (ARGBUF);
Else
{
if (Numargs > 0) {
Arglist[numargs]=null;
Execute (arglist);
Numargs = 0;
}
}
}
return 0;
}

Role: Compared with psh1, more circular judgment, do not exit the words will always want you to enter instructions.

TESTBUF1
#include <stdio.h>
#include <stdlib.h>
int main ()
{
printf ("Hello");
Fflush (stdout);
while (1);
}

Effect: Output Hello First and then wrap. Do not exit afterwards.

Testbuf2
#include <stdio.h>
int main ()
{
printf ("hello\n");
while (1);
}

Effect: Output Hello First and then wrap. Do not exit afterwards.

(Fflush (stdout) and newline characters \ n effect the same)

testbuf3
#include <stdio.h>
int main ()
{
fprintf (stdout, "1234", 5);
fprintf (stderr, "ABCD", 4);
}

Effect: Formats the content to output to standard errors, to the output stream.

Testpid
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main ()
{
printf ("My PID:%d \ n", Getpid ());
printf ("My parent ' s PID:%d \ n", Getppid ());
return 0;
}

Function: Outputs the PID of the current process PID and the parent process of the current process.

Testsystem
#include <stdlib.h>
int main (int argc, char *argv[])
{
System (argv[1]);
System (argv[2]);
return exit_success;
}/*---------- End of function main ----------*/

System (Execute shell command):
Correlation function: Fork,execve,waitpid,popen
Table header file: #i nclude<stdlib.h>
Define function: int system (const char * string);
Function Description: System () calls fork () to produce a child process that calls/bin/sh-c string to execute the command represented by the argument string string, which then returns the process that was originally called when execution is complete. The SIGCHLD signal is temporarily shelved while the system () is being called, and the SIGINT and sigquit signals are ignored.
return value: =-1: Error occurred
= 0: The call succeeds but no child processes appear
>0: ID of the child process that successfully exited
In general, the system function executes the shell command, which is to send an instruction to DOS. Here is the following can be followed by two parameters, and then sent to DOS these two commands, executed separately.

Waitdemo1
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#defineDELAY4
void Child_code (int delay)
{
printf ("Child%d here. Would sleep for%d seconds\n ", getpid (), delay);
Sleep (delay);
printf ("Child-done. About to exit\n ");
Exit (17);
}
void Parent_code (int childpid)
{
int wait_rv=0;/* return value from Wait () */
WAIT_RV = Wait (NULL);

Childpid, WAIT_RV);
}
int main ()
{
int newpid;
printf ("Before:mypid is%d\n", getpid ());
if ((Newpid = fork ()) = = =-1)
Perror ("fork");
else if (Newpid = = 0)
Child_code (DELAY);
Else
Parent_code (NEWPID);
return 0;
}

Effect: If there are child processes, the child process is terminated, and the child process PID is successfully returned.

Waitdemo2
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#defineDELAY10
void Child_code (int delay)
{
printf ("Child%d here. Would sleep for%d seconds\n ", getpid (), delay);
Sleep (delay);
printf ("Child-done. About to exit\n ");
Exit (27);
}
void Parent_code (int childpid)
{
int WAIT_RV;
int child_status;
int High_8, low_7, bit_7;
WAIT_RV = Wait (&child_status);
printf ("Do waiting for%d. Wait returned:%d\n", Childpid, WAIT_RV);
High_8 = Child_status >> 8;     /* 1111 1111 0000 0000 */
Low_7 = child_status & 0x7F; /* 0000 0000 0111 1111 */
bit_7 = child_status & 0x80; /* 0000 0000 1000 0000 */
printf ("status:exit=%d, sig=%d, core=%d\n", High_8, Low_7, bit_7);
}
int main ()
{
int newpid;
printf ("Before:mypid is%d\n", getpid ());
if ((Newpid = fork ()) = = =-1)
Perror ("fork");
else if (Newpid = = 0)
Child_code (DELAY);
Else
Parent_code (NEWPID);
}

Effect: The state of a subprocess is differentiated from 1 Laido, and the state is split into three blocks, Exit,sig, and core.

In summary, the function prototype of wait is

#include <sys/types.h>/* provides the definition of type pid_t */

#include <sys/wait.h>

pid_t Wait (int *status);

Once the process has called wait, it immediately blocks itself, and the wait automatically parses if a child process of the current process has exited, and if it finds such a child process that has become a zombie, wait will collect information about the child process and destroy it and return it, if no such child process is found. Wait will always be stuck here until one appears.

Reference Address: http://www.cnblogs.com/20135202yjx/p/5003653.html

Information Security system design basics 12th Week 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.