Experiment 0 use the Linux User Interface to create and execute Experiment 1 processes concurrently

Source: Internet
Author: User

Tutorial 0 use of the Linux User Interface

Experiment 1 Process Creation and concurrent execution

 

Purpose:

(1) familiar with Linux operating environment and GCC tools

(2) deepen understanding of the process concept and clarify the differences between processes and procedures

(3) further understanding of the essence and features of concurrent processes

 

Experimental instruments and materials:

Micro Computer, Red Hat Linux System

 

Lab content:

Task 1: Process Creation

Write a program to let the parent process generate two child processes. The parent process displays the characters "A" and "C" respectively ".

 

# Include <stdio. h>

Main (){

Int P1, P2;

While (p1 = fork () =-1);/* the parent process creates the first process until it is successful */

If (p1 = 0)/* 0 is returned to the sub-process 1 */

Putchar ('B');/* P1 processing process */

Else {/* positive number returned to the parent process (sub-process number )*/

While (P2 = fork () =-1);/* the parent process creates the second process until it is successful */

If (P2 = 0)/* 0 is returned to the sub-process 2 */

Putchar ('C');/* P2 processing process */

Else putchar ('A');/* processing of the parent process after P2 is created */

}

}

 

Run the program, observe and analyze the running results.

 

Task 2: Change the output character to a long string, observe the concurrent execution of the process, and analyze the execution result.

 

# Include <stdio. h>

Main (){

Int P1, P2, I;

While (p1 = fork () =-1);/* the parent process creates the first process until it is successful */

If (p1 = 0)

For (I = 0; I <500; I ++)

Printf ("childa % d \ n", I)

Else {

While (P2 = fork () =-1);/* the parent process creates the second process until it is successful */

If (P2 = 0)

For (I = 0; I <500; I ++)

Printf ("childb % d \ n", I)

Processing of the parent process after else/* P2 is created */

For (I = 0; I <500; I ++)

Printf ("Parent % d \ n", I );

}

}

 

Run the program, observe and analyze the running results.

 

System calls involved in this experiment:

Fork ()To create a new process.

System Call format:

PID = fork ()

The meaning of Fork () return value is as follows:

0: in a sub-process, the fork () returned value saved by the PID variable is 0, indicating that the current process is a sub-process.

> 0: In the parent process, the fork () value saved by the PID Variable returns the id value of the child process (Unique Identifier of the process ).

-1: creation failed.

If fork () is called successfully, it returns the PID of the child process to the parent process, and returns 0 to the child process. That is, fork () is called once, but returns twice. At this time, the OS creates a new process in the memory, and the new process is a copy of its parent process, called child process ). A child process inherits many features of the parent process and has a user-level context identical to that of the parent process. Concurrent execution of parent and child processes.

 

Questions: (the answer is written in the lab report)

1. Where is the starting position of the command pointer when a newly created process starts to run?

2. If you change Task 1 to: write a program, let the parent process create a child process, and then the parent process displays the character "A". The child process creates a child process, the character "B" is displayed, and the sub-process displays the character "C ". How to rewrite the source program? (Complete source code is provided in the lab report and runs on the machine .)

 

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.