Information Security System Design Foundation 12th Week study Summary

Source: Internet
Author: User

Process.tar Code

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;
}

EXECVP function: From the directory referred to by the PATH environment variable to find the file name that conforms to the parameter, the file is found and executed, and then the second parameter is argv passed to the file to be executed.

If the execution succeeds, the function does not return, and the execution fails to return 1 directly, and the reason for the failure is stored in errno.

To run the example:

As can be seen, the EXEVP function call succeeds without returning, so there is no print out "* * * ls is done. Bye "this sentence.

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 ");
}

The difference between it and EXEC1 is that the first parameter of the EXEVP function, EXEC1 is the LS,EXEC2 directly used arglist[0], can be defined by the two equivalent, the result is the same.

To run the example:

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: From the directory referred to by the PATH environment variable to find the file name that conforms to the parameter, the file is found, then executes the second parameter as the file's argv[0], argv[1] ...., the last argument must end with a null pointer (NULL). If a null pointer is represented by a constant zero, it must be cast to a character pointer, otherwise it will be interpreted as an shaping parameter, and if the length of an integer is different from the length of the char *, then the actual parameters of the EXEC function will be faulted. If the function call succeeds, the process's own execution code becomes the loader's code, and the code behind EXECLP () will not execute.

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

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

To run the example:

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;
}

Print the process PID first, 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.

To run the example:

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;
}

The code calls two fork, altogether producing four sub-processes, and prints out four aftre outputs.

To run the example:

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 ();/* Create New process*/
if (FORK_RV = =-1)/* Check for error*/
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;
}

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 will output the sentence.

To run the example:

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;
}

First print the process PID, 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 returned 0, so the output of the children and the following sentence.

To run the example:

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;
}

The parent process prints two sentences, then sleeps for a 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.

To run the example:

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;
}

This code is equivalent to the command 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.

To run the example:

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;
}

Compared with the psh1, more circular judgment, do not exit the words will always want you to enter the instructions, and for the existence of the sub-program state conditions

To run the example:

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

The effect is to output the Hello first and then wrap the line. Do not exit afterwards.

To run the example:

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

The effect is as above. Conclusion: Fflush (stdout) and newline characters \ n have the same effect.

To run the example:

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

The content is formatted to output to the standard error, the output stream.

To run the example:

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;
}

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

To run the example:

Testpp
#include <stdio.h>
#include <stdlib.h>
int main ()
{
Char **pp;
Pp[0] = malloc (20);
return 0;
}

To run the example:

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

System function: Executes the shell command, which is to send a command to DOS. Here is the following can be followed by two parameters, and then sent to DOS these two commands, executed separately.

Run Example: Enter LS and dir two instructions and execute 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;
}

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

To run the example:

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);
}

This is more a sub-process than the state of the distinction, the state split into three blocks, Exit,sig and core.

To run the example:

Sigdemo1
#include <stdio.h>
#include <signal.h>
VOIDF (int);
int main ()
{
Inti
Signal (SIGINT, F);
for (i=0; i<5; i++) {
printf ("hello\n");
Sleep (2);
}
return 0;
}
void f (int signum)
{
printf ("ouch!\n");
}

To run the example:

Sigdemo2
#include <stdio.h>
#include <signal.h>
Main ()
{
Signal (SIGINT, sig_ign);
printf ("You can ' t stop me!\n");
while (1)
{
Sleep (1);
printf ("haha\n");
}
}

Always output haha ...

To run the example:

Sigdemo3
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#defineINPUTLEN100
int main (int argc, char *argv[])
{
void Inthandler (int);
void Quithandler (int);
Char Input[inputlen];
int nchars;
Signal (SIGINT, inthandler);//^c
Signal (Sigquit, quithandler);//^\
do {
printf ("\ntype a message\n");
Nchars = Read (0, input, (INPUTLEN-1));
if (nchars = =-1)
Perror ("read returned an error");
else {
Input[nchars] = ' + ';
printf ("You typed:%s", input);
}
}
while (strncmp (input, "quit", 4)! = 0);
return 0;
}
void Inthandler (int s)
{
printf ("Received signal%d.. Waiting\n ", s);
Sleep (2);
printf (" leaving Inthandler \ n");
}
void Quithandler (int s)
{
printf ("Received signal%d.. Waiting\n ", s);
Sleep (3);
printf (" leaving Quithandler \ n");
}

Output your input to the screen again, enter quit to end.

To run the example:

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.