Experiment 2 Linux BASIC programming __HTML5

Source: Internet
Author: User
Tags save file

Experiment 2 BASIC programming under Linux

I. Purpose of the experiment

1. Master Vim Compiler

2. Master the use of GCC compilers

3. Understanding the fork () procedure

second, experimental tools and equipment

1. Experimental equipment: computer (with CD-ROM) one.

Iii. knowledge of experimental preparation

1. Vim's editor

Using syntax: Vim < edited filename >

There are three modes of VIM: Command mode, insert mode, last line mode.

Command mode:

After VIM has just started, it is in this mode. In this mode, the life of vim is allowed

Order to edit the file or move to another mode. Such as:

X command: Deletes the character above the cursor.

Arrow keys: Move the cursor.

I command: Into insert mode, where you can insert characters at the current cursor.

A command: Enter insert mode, you can insert characters after the current cursor.

R command: Replaces text at the current cursor.

R command: Replaces the character at the current cursor.

~ command: Case conversion of the letter at the current cursor.

DD Command: Deletes the line where the cursor is located.

DW command: Deletes the word at the cursor location.

H command: The cursor moves left.

L Command: The cursor moves to the right.

K Command: The cursor moves up.

J Command: The cursor moves down.

: command: Enter the last line of mode.

Insert mode:

This mode allows text to be entered, wraps with the Enter key, and ESC enters command mode.

Last line mode:

W Command: Save the file, but do not exit.

WQ command: Save the file and exit.

Q! Command: Do not save file and exit.

R command: Inserts another file content at the current cursor.

2.GCC Compiler

The C language compiler cc used on Unix, and the derivation on Linux is gcc. Write the source with vim

After the order, return to the shell, using GCC to compile the source program is:

GCC source program

In which, the "source program" is the C language source code file that you write with. c as the extension.

If the source code has no syntax errors, compiling using the command above will generate a name a.out in the current directory

The executable file. If the source code has syntax errors, no files are generated, and the GCC compiler in the shell

Tip You the wrong place and type.

You can also compile source code files by using the following methods to generate a custom executable file:

GCC source file –o a named file name

Executes the compilation-generated executable file under the current directory, using the following format:

./executable file name

3.fork () function Description

pid_t fork (void)

Fork () produces a new subprocess. The function is contained in the header file unistd.h. Its child processes are copied

The data and stack space of the parent process, and inherits the user code, group code, environment variables, and open text of the parent process

Code, working directory, resource limits, and so on. Linux uses Copy-on-write (COW) technology only if one of the

The process attempts to modify the space to be replicated before it does a real copy action, because the inherited information is replicated and

Does not mean the same memory space, so the child processes are not synchronized with the changes to these variables and the parent process. This

, the child process does not inherit file locks and unhandled signals from the parent process. Note that Linux does not guarantee that child processes will be more

The parent process executes first or late, so be aware of the deadlock or competitive condition when writing the program.

Returns the value, if the fork () call succeeds, the parent process returns the newly established subprocess Code (PID), and the new

0 is returned in the child process being established. If fork () fails, returns 1 directly, and the reason for the failure is in errno. Lost

There are three reasons to lose:

1 system memory is not enough;

2 process table full (capacity is generally 200~400);

3 The user has too many child processes (typically no more than 25).

Error code: Eagain Not enough memory, enomem not enough memory to configure the core required data structure empty

Between

Iv. Contents and steps of the experiment

Experiment One:

In the Linux environment, programming with the C language, using system call fork to create a process with multiple child processes.

(1) In the terminal input vim A.C, launches VI.

(2) Press A or I into insert mode and enter the following code inside.

#include "stdio.h"

#include "Sys/types.h"

#include "Unistd.h"

int main ()

{

pid_t Pid1;

pid_t Pid2;

PID1 = fork ();

Pid2 = fork ();

printf ("pid1:%d, pid2:%d\n", Pid1, Pid2);

}

(3) Press Esc key to enter the command mode, input: and Wq to save out vim.

(4) Compile the program with the GCC compiler: Gcc–o a A.c–ggdb

(5) Run the program you just compiled:./A

Requirements:

A. Please say that after executing this program, you will run several processes altogether.

B. Observe the results of the operation and give analysis and explanation.

After this program runs, 4 processes will be run altogether. These 4 processes do not strictly distinguish between priorities.

When the fork () function is not executed, the PID1 and PID2 of the parent process are 0, and when the first fork () function is executed, the replication parent process obtains a child process F1,f1 pid1 and Pid2 are both 0, and the Pid1=f1 ID of the parent process.

When the second fork () function is executed, continuing to replicate the parent process to get the subprocess F2, the id,f2 of Pid1=f1 id,f2 of the pid1=f1 id,pid2=f2 pid2=0 of the parent process;

The replication process F1 the process F11, the PID2=F11 and id,f11 pid1 of Pid2 of F1 pid1=0,f1 are all 0;

Experiment Two:

First of all, analysis of the code when the output of the results of what kinds of possibilities, according to the test 1 steps compiled and debugged to observe the actual output, compare the difference between the two, analysis of the reasons.

void Main (void)

{int x=5;

if (fork ())

{

x+=30;

printf ("%d\n", X);

}

Else

printf ("%d\n", X);

printf (("%d\n", X);

}

(write out possible experimental results and analysis instructions)

Possible output results:

1.35 5 35 5 The parent process executes printf in the IF statement and then the subprocess executes printf in the Else statement, the parent process executes the last printf, and the child process executes the last printf

2.5 35 35 5 subprocess executes printf in the Else statement, the parent process executes printf in the IF statement, the parent process executes the last printf, and the child process executes the last printf

3.35 5 5 35 The parent process executes printf in the IF statement and then the subprocess executes printf in the Else statement, the child process executes the last printf, the parent process executes the last printf

4.5 35 5 35 subprocess executes printf in the IF statement and then the parent executes printf in the Else statement, the parent process executes the last printf, and the child process executes the last printf

5.35 35 5 5 The parent process executes printf in the IF statement and the parent process executes the last printf, the subprocess executes printf in the Else statement, and the child executes the last printf

6.5 5 35 35 subprocess executes printf in the IF statement and then the subprocess executes the last printf, the parent process executes the printf in the Else statement, and the parent executes the last printf

v. Summary of the experiment

1. Write out the experiment report.

2. Use Vim to edit Linux text files.

3. Write fork () programs and compile, debug, and run using GCC.

 

 

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.