20135213--Information Security System Design Foundation 11th Week study Summary

Source: Internet
Author: User

I. Learning Objectives

Mastering Process Control
Mastering the method of signal processing
Mastering the methods of Pipeline and FIFO for interprocess communication


Ii. Learning Tasks

Compile, run, read, and understand the code in the process.tar.gz compression pack

Iii. References and experimental environment

Reference: process.tar.gz code in a compressed package,

The textbook "in-depth understanding of computer systems",

Baidu, 20135202 Shang

Environment: Laboratory Building


Iv. learning Process

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

Run as follows:

The EXECVP function is used in this code, and the result is that the second printf statement in the source code disappears.

/*EXECVP function: Its first parameter represents the location where it executes the file, and the second parameter is the command.

EXECVP () Searches the location of the LS command in the directory specified in the PATH environment variable, while the LS command that passes the parameter is in argv. */

because when the EXECVP function is called, the kernel loads the new program into the current process to replace the current process's code and data.

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 EXEC2 and EXEC1 is that the statements EXECVP function calls become

EXECVP (Arglist[0], arglist);

The compilation runs exactly the same as exec1.c, stating that the first item of the arglist array is the name of the program to run.

Exec3

A series of EXEC function call methods are given in exec3.c, which uses the EXECLP function as follows:

Header file:

#include

To define a function:

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

Function Description:

EXECLP () finds the file name in the directory referred to by the PATH environment variable, executes the file after it is found, and then takes the second later parameter as the file's argv[0], argv[1] ...., the last parameter must be terminated 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.

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

Operation diagram:

This code first prints the process PID, and then calls the fork function to generate the child process, hibernation one second after the process ID printing, the parent process to print the child process PID, the child process returned 0.

After calling the fork function, the kernel's working process:

    • Allocating new memory blocks and kernel data structures
    • Copy the original process to a new process
    • To add a new process to a running process set
    • Return control to two processes
To create a new process

The parent process creates a new run child process by calling the fork function.

Called once and returned two times. Returns to the parent process one time, once back to the newly created child process.

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

Operation diagram:

This code calls two fork, altogether producing four sub-processes, so it prints four aftre output.

Child process creates a new process

The child process is not started from the main function, but is created from where the fork is returned.

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");
else if (Fork_rv = = 0) {
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;
}

Operation diagram:

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.

Resolving Parent and child processes

Running the pid_t fork (void) statement, the return value of the different process fork is different, the child process returns 0, the parent process returns the PID of the child process, and the error returns-1.

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

else if (Fork_rv = = 0) {
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;
}

Operation diagram:

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.

Fokgdb

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

Operation diagram:

The main difference is that the parent process prints two sentences first, 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.

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);
printf ("Done waiting for%d. Wait returned:%d\n",
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;
}

Operation diagram:

The process waits for the child process to exit by calling the wait function. Wait first pauses the process that invokes it until the child process ends, and wait gets the value passed to exit when the child process ends.

WAITDEMO1.C shows the process that the child process calls exit to trigger wait to return to the parent process. The parent process waits for the child process to exit.

It embodies two important features of wait:

    • Wait blocks the program that called it until the child process ends
    • Wait returns the PID of the end process
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);
}

Operation diagram:

Shows that the wait function tells the parent process that the child process is the same as and ends with the arguments passed to the wait. When the parent process calls wait, an integer variable address is passed to the function, and the kernel saves the child process's exit state in the variable.

V. Problems encountered and Solutions

1. Virtual machine can not run, a virtual machine computer on the card

Solution: Experiment with the environment of the experimental building.

20135213--Information Security System Design Foundation 11th 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.