Linux programming final review [super practical]

If it helps you, remember to like and follow me!

Textbooks used: Xu Qingui, Xu Zhigen, Huang Peican, Xie Weipeng (editors) Publisher: Tsinghua University Press

A good book that combines practical and theoretical, but also very obscure and abstract. The original exam questions are below!

Frequently Asked Questions: The header files (such as wrapper.h) of some Linux-C program examples in the book are "unable to find" when compiling?

Solution: Which functions are used in the program, use [man command + function name] to check the header file containing this function (to replace wrapper.h)

1. Short answer and programming

1. Write 20 commands. P24

su  ls  touch  tar  gcc  mkdir  rmdir  chmod  apt  wc  cd  pwd  cp  kill  mv  grep  cat  more  less  find  remove  read  ln  rm

cd: change directory    

Is: List files in a directory    

mv: move/rename files/directories rm: delete files 

mkdir: create directory rmdir: delete directory (but cannot be executed when the folder is not empty)    

cat: Display the contents of the file (Concatenate)

find: find files in the specified directory    

pwd: Displays the user's current working directory as an absolute path    

rm - parameter: remove N files/whole directories

touch: create new empty file    

cp: copy files/directories    

vi: modify file content    

echo: create/overwrite file    

tar: file packing/unpacking   

scp: remote copy file (Secure copy)

2. Write out the execution method of the shell script. P27

3. Explain what parts are included in the execution time of a linux program. P110

User mode: execute instructions in user address space
Kernel mode: execute instructions in kernel address space
Sleep: time to execute other processes
Practical time: user mode + kernel mode
Real time: user mode + kernel mode + sleep
       In the Linux system, The process shares the CPU in the form of time slices; at the same time, when the process is scheduled to enter the running state, the execution of the process has two running modes, user mode and kernel mode. When a process executes code in the user address space, we call the process running in user mode; when a process enters a system call or gets stuck in a hardware interrupt, it is said to be in kernel mode. Therefore, the process can be timed from different angles.
       Processes are not running all the time, but switch between user mode, kernel mode, and sleep mode.

4. Shell script programming.

(1) Write a script to calculate the sum of integers 1 to 1000

#!/bin/bash
sum=0
for i in `seq 1 1000`
do
    sum=$[$i+$sum]
done
echo $sum

operation result:

bash oneto1000add.sh 
500500

(2) Write a script to calculate the product of integers 1 to 1000

Tested multiplying to a larger number, overflowed the maximum value, and the result is displayed as 0. Test the product of 1 to 10, the result is correct

#!/bin/bash
var=1
for i in {1..10}
do
 var=$[$var*$i]
done
echo $var

operation result:

bash oneto1000mi.sh 
3628800

5. What are the types of library functions that come with Linux? P68

Many system functions including input and output, mathematical operations, string processing, time and date, environment control, memory allocation, multi-threaded concurrency, data structure algorithms
(1) Mathematical functions (math.h, libm.so, libm.a )
pow(x,y), sqrt(x), exp(x), log(x), log10(x), ceil(x), floor(x), fabs(x), sin, cos, tan, ctan , cosh, tanh, cosh
(2) Environment control function
getenv\setenv\unsetenv
(3) String processing function
strcat,strcpy,strncpy,bcopy,memcpy,strcmp,strncmp,strcasecmp,strncasecmp,bzero,memset,index,strchr, rindex,strrchr,memchr,memrchr,strstr,strcasestr,strtok,strupr,atoi,strtol,strtod
(4) time function
time, asctime, ctime
(5) data structure algorithm function
binary search: bsearch; linear search: lfind,lsearch ;Quicksort: qsort;Binary tree algorithm: tsearch,tfind,twalk,tdelete,tdestroy

6. List the types of files in the linux operating system. P13

Linux systems use the following characters in file directory listings to represent these files:
-, regular file d, directory file c, character device file b, block device file p, pipe file l, symbolic link file s, socket file

9 common attributes of files or directories

Structure of Linux file types and access bits

7. Write out several mechanisms for the linux system to send signals to processes. P188

(1) Send a signal with /bin/kill
(2) Send a signal from the keyboard
(3) Send a signal with the kill and raise functions
(4) Send a signal with the alarm function

2. Briefly describe the application programming interface

1. List the application programming interfaces related to file I/O operations.

①open

int fd = open(char* filename,int flags,mode_t mode);

②close

int close(int fd);

③lseek

off_set lseek(int fd,off_t offset,int whence);

④read

ssize_t read(int fd,void *buf,size_t n);

⑤write

ssize write(int fd,const void *buf,size_t n);

2. List the application programming interfaces for interprocess communication.

(1) Pipeline

①mkfifo

mkfifo [OPTION]... NAME...

②pipe

int pipe(int pipefd[2]);

(2) Message queue

①msgget

int msgget(key_t key, int msgflg);

②msgsnd

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

③msgrcv

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

④msgctl

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

(3) Shared memory

①shmget

int shmget(key_t key, size_t size, int shmflg);

②shmat

void *shmat(int shmid, const void *shmaddr, int shmflg);

③shmdt

int shmdt(const void *shmaddr);

④shmctl

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

(4) Semaphore set
①semget

int semget(key_t key, int nsems, int semflg);

②semop

int semop(int semid, struct sembuf *sops, size_t nsops);

③semctl

int semctl(int semid, int semnum, int cmd, ...);

3. Discussion

1. Discuss which three related data structures are used by the Linux kernel to represent open files. P117

       File descriptor table (descriptor table) : Each process has an independent file descriptor table. The data type definition is struct file *fd_arrary[NR_OPEN_DEFAULT], which is an array of pointers to file objects. UNIX I/O uses the index number of the descriptor table entry as the return value of the open system call function, called the file descriptor (descriptor). Subsequent file operations use the file descriptor to locate the file object, and then locate the v-node object to obtain file attributes.
       File table (file table) : Linux stores open file information in a file object (file object), also known as file structure. The main members include open mode (f_mode), read and write pointer (f_pos), and reference count (f_count) three Field where the reference count records the number of pointers to the structure. When an open operation is performed on a file, a file object is created, and all file objects in the system form a file table.
      V-node table (v-node table) : Each open file (or device) has a v-node (v-node) structure. The v node contains the file type and pointers to functions that perform various operations on the file. The v-node also contains the information of the i-node (i-node) read from the disk. The i-node information includes the owner of the file, the length of the file, the device where the file is located, and the location of the actual data block pointing to the file on the disk. pointer, etc.

2. Discuss which variable types are in the linux multithreaded program, which address space is mapped to, and there are several running examples. P218~223

Global variable :
       a variable defined outside the function, with only one instance, mapped to the readable and writable data area (data segment, bss segment) of the process virtual memory (process address space). Any thread can reference, the most typical shared variable, example: ptr in sharing.c.
Local automatic variable :
       a variable defined inside a function but without a static attribute. When the function is called by a thread, all local instance variables of the function have a running instance in the thread stack. If multiple threads execute the same function (or example routine), the variables in the routine have multiple running instances. Example:
The local variable tid of the main function in sharing.c has a running instance tid.m in the main thread; the
function thread has a local variable myid, because the function is called by two peer threads p0 and p1, there are two variables Instance myid.p0, myid.p1.
Local static variable : A variable
       defined inside a function and has a static attribute. Even if the function is called by multiple threads, there is only one running instance, which is located in the readable and writable area (data segment, bss segment) of the process virtual memory (process address space). Example: cnt inside function thread.

4. Drawing description and analysis

1. Draw a picture to illustrate the structure of the virtual address space of the linux process. P222

        Process user address space: All threads share the user address space (or virtual address space) of the process, which consists of all storage locations accessible to the process, including:

        Text segment (text segment, program code and read-only data)
        data segment (data and bss segment, global variables)
        storage heap (dynamically allocated memory)
        stack (local variables)
        shared library code and data (user address spaces of different processes can overlap or coincident)

2. Draw a diagram of the relationship between the linux process, the linux kernel and system calls. P204

3. Draw a diagram illustrating the typical organization of the user stack when a new program is started. P179

The function prototype of main: int main(int argc, int *argv[], char *envp[])
is first the stack frame of main, which is the local variable called by the main function.
Next is the command line parameter format argc, the command line parameter list pointer argv, and the environment variable parameter list pointer envp.
Next is the command line argument list argv[] and the environment variable list envp[].
Finally, at the bottom of the stack are the command line parameter strings and environment variable strings.
* The global variable environ points to the first of these pointers, envp[0]

5. Analyzing process family relationships

1. Explain the process of creating a process using the fork system call P163~166

       After the parent process executes the fork system call, the child process is born, and the newly created child process is almost but not exactly the same as the parent process: the program code is the same as the parent process, the variable values ​​are copied from the parent process, and then also from the fork function. The call returns, and then executes down; the difference is that the return value of the fork system call is different, and the program code can judge the parent process or the child process according to the return value, and perform different processing work accordingly. The Linux system stipulates that the return value of the parent process fork system call is the PID of the child process, the fork return value of the child process is 0, and the system fills in the data set of the parent and child processes. After that, the child process starts its own life cycle as an independent process and executes it downwards.

2. Assume that the pid of the child process is 3000 when the following program is running, and the pid of the parent process is 2999. Please write the result of the program and draw a picture to illustrate the changes in the user address space when the parent and child processes are running. P163

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int glob=10;
int main(void){ 
 int local;
 pid_t pid;
 local=8;
 if((pid=fork())==0){ 
  sleep(4);
 }
 else{ 
  glob++;
  local--;
  sleep(10);
 }
 printf("pid=%d,glob=%d,local=%d\n",getpid(),glob,local);
 exit(0);
}

operation result:

pid=3000,glob=10,local=8 //子进程
pid=2999,glob=11,local=7 //父进程

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324125241&siteId=291194637