Linux Pen Test __linux

Source: Internet
Author: User
Tags dba

Linux application: One or two questions, test time: 1.5 hours

Linux driver Programmer: do one or two (optional), three questions, test time: 2 hours

A. C + + language

1. What is the Ifndef/define/endif in the header file?

A: Prevent the header file from being repeatedly referenced

2. Data type judgment

Typedefint (*test) (float *, float*)
Test tmp;

The type of TMP is: ___c___.

(a) A pointer to a function with two pointers (pointer) pointing to floating-point numbers (float) as an argument (arguments), and the return value type of the function is an integer pointer

(b) Integer pointer

(c) A pointer to a function with two pointers (pointer) pointing to floating-point numbers (float) as an argument (arguments), and the return value type of the function is an integral type

(d) None of the above

3. C + + class and C inside the struct what is the difference.

The members in the struct are public by default, and the default in class is private

Class has default constructs, destructors, struct not

Class can have virtual functions, struct not

Class can be inherited, struct not

4. Read and Answer

What the following code output is, and why.

void Test (void)
{
unsigned int a = 6;
int b =-20;
(A+b > 6)? Puts ("> 6"): Puts ("<=6");
}

A: ">6", because the signed and unsigned mixed operations, the number of symbols automatically converted to unsigned numeric values for operation

5. Read and Answer

int counter (int i)

{

static int count = 0;

Count = Count +i;

return (count);

}

Main ()

{

int I, J;

For (i=0 i <=5; i++)

j = counter (i);

}

This program executes to the last, the value of J is: __b___.

(a) 10

(b) 15

(c) 6

(d) 7

6. Read and Answer

Main ()
{
int a[][3] = {1,2,3, 4,5,6};
int (*PTR) [3] =a;
printf ("%d%d", (*PTR) [1], (*PTR) [2]);
++ptr;
printf ("%d%d", (*PTR) [1], (*PTR) [2]);
}

The output of this program is: __a___.

(a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) Not all of the above

7. The following expression conforms to the specification of ___d__.
A. while (P && *p)//p is a pointer variable
B. if (1 = flag)//Flag is a Boolean variable
C. if (0.0 = = x)//x is a floating-point variable
D. if (strlen (strName)!= 0)//strName As String variable

8. Given a 4-byte integer variable A, to bit0~bit31 identify bits, write two pieces of code, the first set a bit 3, the second to clear a bit 3. In the above two operations, keep the other bits unchanged. #define BIT3 (1<<3)

a |= BIT3;

a ^= ~bit3;

9. Code implementation of the following several small functions

(1) to write two macros to achieve a byte unsigned integer 16 binary and compressed BCD to convert each other. Assuming a value of not more than 99

For example: "0x12" is a 16-in notation, a 10-digit number is "18", and is recorded as "0x18"

#define BYT_HEX2BCD (x) ((X/10) <<4) + (X%10)

#define BYT_BCD2HEX (x) ((x>>4) *10) + (x&0x0f)

(2) Write a function to convert the ASCII code string to a 16-binary array

Example: ASCII string is converted to "8E349BCD45" to

0x8e,0x34,0x9b,0xcd,0x45

Intstrasc2hex (unsigned char *dst,const char * src, int len)

{

int i;

unsigned char dtemp,stemp;

Char *ptr;

Ptr=const_cast<char *> (SRC);

if (len%2) return 0;

len/=2;

for (i=0;i<len;i++) {

if ((*ptr) >= ' 0 ' && (*ptr) <= ' 9 ') stemp=*ptr-' 0 ';

if ((*ptr) >= ' a ' && (*ptr) <= ' F ') stemp=*ptr-' a ' +0x0a;

if ((*ptr) >= ' a ' && (*ptr) <= ' F ') stemp=*ptr-' a ' +0x0a;

dtemp=stemp<<4;

ptr++;

if ((*ptr) >= ' 0 ' && (*ptr) <= ' 9 ') stemp=*ptr-' 0 ';

if ((*ptr) >= ' a ' && (*ptr) <= ' F ') stemp=*ptr-' a ' +0x0a;

if ((*ptr) >= ' a ' && (*ptr) <= ' F ') stemp=*ptr-' a ' +0x0a;

*dst++=dtemp|stemp;

ptr++;

}

return Len;

}

10. Read and Answer

Reading questions.

(1) voidgetmemory (char *p)

{

p = (char *) malloc (100);

}

Voidtest (void)

{

char *str = NULL;

GetMemory (str);

strcpy (str, "HelloWorld");

printf (str);

}

What would be the result of running the test function, please? Why.

Program error.

Because GetMemory is not able to deliver dynamic memory,

STR in the test function is always NULL.

strcpy (str, "Hello World"); The program will make an error.

The memory allocated in the GetMemory is not released and memory leaks

(2) char*getmemory (void)

{

Char p[] = "HelloWorld";

return p;

}

Voidtest (void)

{

char *str = NULL;

str = GetMemory ();

printf (str);

}

What would be the result of running the test function, please? Why.

may be garbled.

Because GetMemory returns a pointer to "stack memory," The pointer's address is not NULL, but its original content is cleared and the new content is unknown.

(3) VoidGetMemory2 (char **p, int num)

{

*p = (char *) malloc (num);

}

Voidtest (void)

{

char *str = NULL;

GetMemory (&STR, 100);

strcpy (str, "Hello World");

printf (str);

}

What would be the result of running the test function, please? Why.

Ability to output hello, but memory leaks

(4) void Test (void)

{

Char *str = (char *) malloc (100);

strcpy (str, "Hello");

Free (str);

if (str!= NULL)

{

strcpy (str, "World");

printf (str);

}

}

What would be the result of running the test function, please? Why.

Tampering with the contents of a dynamic memory area has unpredictable consequences. Because of free (str); then STR becomes a wild pointer.

11. Snap to Memory

There are the following definitions:

#pragmapack (8)

StructtagS1

{

char c;

int i;

};

#pragmapack ()

#pragmapack (2)

StructtagS2

{

char c;

int i;

};

#pragmapack ()

If the compiler supports the specified alignment, the calculation:

sizeof (TAGS1) = 8

sizeof (TAGS2) = 6

12. Read and Answer

Indicates that the following code may be wrong to run, or to write in a place that is not spec

int func (int par1, char par2)

{

int i;

i + +;

if (i = = 1) {

....................

Par1= 100/par1;

...................

Char C = par2 + +;

..................

}

return par1;

}

(1) I, J definition is not initialized

(2) if () I ==1 writing is not standard, if mistakenly written as I=1, compilation still pass, but logic is not expected

(3) When 100/par1, do not perform a non-0 check on the divisor

(4) The input parameters of the function, preferably written as v_ipar1 and V_cpar2, to represent the function parameters and their respective types. Recommendations

(5) If after the curly braces, preferably a single line. Recommendations

13. Big End mode/small terminal/network byte order

(1) What is the big end mode, the small terminal mode, the network byte order.

Embedded system developers should have a good understanding of Little-endian and Big-endian patterns. The CPU in the Littleendian mode is stored in the operand from the low byte to the high byte, the lower address low byte, and the Big-endian mode to the operand storage way from high byte to low byte, is low address high byte pull.

(2) Please write a function to determine whether the processor is a big or small end mode

INTCHECKCPU ()

{

Union test

{

int A;

Char b;

}c;

C.A = 1;

return (1 = c.b)

}

The big end returns 0, and the small ends return 1.

14. Linked List

The following list nodes are defined:

Structnode
{
int data;
Node *next;
};
typedef struct node node;

The head of the linked list is known, write a function to reverse the list (Intel)

second, Linux application development

Linux operating system

(1) Explain the common commands under Linux:

RM Delete

CP Replication

Mount Mount Mounted

chmod Change Permissions

LS Output directory information

(2) If you encounter unfamiliar orders, you will.

Use the man command to find help

16. Section Error Debugging

(1) What is a paragraph error.

The so-called segment error means that access to the memory is beyond the system to the memory space of the program, once the program has a cross-border access, the CPU will produce corresponding abnormal protection, so segmentationfault appeared.

(2) An example of what is commonly encountered in programming where there is a section error.

Example 1

Write data to the memory address that is protected by the system (such as the kernel occupies or is in use by another program)

#include <stdio.h>

Intmain ()

{

int i = 0;

scanf ("%d", I); /* should have used &i * *

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

return 0;

}

Example 2

Memory out of bounds (array out of bounds, inconsistent variable type, etc.)

#include <stdio.h>

Intmain ()

{

int B = 10;

printf ("%s\n", b);

return 0;

}

(3) How to find out the errors in the program and dispose of it.

(a) Output information at key locations within the program to track the possible location of segment errors in the code. With this debugging method, you can use conditional compilation Directives #ifdef Debug and #endif to include printf functions, and compile with the-D debug parameter to view debugging information.
(b) with GDB to debug, in the compile time with the-G parameter, used to display debugging information, the program is running to the wrong place, will automatically stop and show the wrong line and line number
(c) using the CATCHSEGV command to catch a segment error

17. Write a makefile generic template that requires the following features:

Make: Compiling and connecting programs.
Make OBJS: Just compile the program to produce the. o target file without connecting.
Make clean: Deletes the compilation-generated target files and dependent files.
Make Cleanall: Deletes the destination file, dependent file, and executable file.
Make rebuild: recompiling and connecting programs

# The executable file name.
# It must be specified.
# program: = a.out # The executable name
Program: =

# The directories in which source files reside.
# at least one path should is specified.
# Srcdirs: =. # current Directory
Srcdirs: =

# The source file types (headers excluded).
# at least one type should is specified.
# The valid suffixes are among of. C. C,. CC,. cpp,. CPP,. C + +,. cp, or. cxx.
# srcexts: =. C # C Program
# srcexts: =. cpp # C + + program
# srcexts: =. C. cpp # C + + program
Srcexts: =

# The flags used by the CPP (Mans CPP for more).
# cppflags: =-wall-werror # Show all warnings and take them as errors
Cppflags: =

# The compiling flags used only for C.
# If It is a C + + program, no need to set these flags.
# If It is a C and C + + merging program, set this flags for the C parts.
Cflags: =
Cflags =

# The compiling flags used only for C + +.
# If It is a-C program, no need to set these flags.
# If It is a C and C + + merging program, set this flags for the C + + parts.
Cxxflags: =
Cxxflags =

# The library and the link options (c and C + + common).
Ldflags: =
Ldflags =

# # Implict Section:change The following only when necessary.
##===========================================================
# the C program compiler. Uncomment it to specify yours explicitly.
#CC = gcc

# The C + + program compiler. Uncomment it to specify yours explicitly.
#CXX = g++

# Uncomment the 2 lines to compile C programs as C + + ones.
#CC = $ (CXX)
#CFLAGS = $ (cxxflags)

# The command used to delete file.
#RM = Rm-f

16. How often do you use multiple processes and multithreaded programming in your project? Examples of using multiple processes and multithreading are briefly illustrated

third, Linux driver development

1. No need to compile the kernel of the case is (D)

A to remove device drivers that are not used by the system

B when upgrading the kernel

C when adding new hardware

D Activate the NIC

2. Regarding the I node and the Super block, the following statements are not correct (B)

A I node is a fixed-length table

B Super blocks are unique in the number of file systems

The C I node contains all the information necessary to describe a file

The D Super Block records where the I-node table and the free block table information are stored on the disk

3. USB

1 USB is a common PC interface, he has only 4 lines, USB interface definition of the 4 signal is what.

Power cord, ground, USB signal positive, USB signal negative (difference).

2 USB through a certain format of the "Letter Package" (packet) according to a certain "Protocol" (protocol) Transmission of information, and according to the nature of information (content) into 4 transmission type: control type, wait-type, break-type, block-type, these four types of transmission, which belong to the reliable transmission, Which is not reliable delivery.

The waiting type is unreliable

4. Data replication

1 What are the functions of copy_to_user and Put_user functions/macros, and what are the scenarios used in Linux programming in general?

Data replication between kernel space and user space, which is for a segment of memory address, which can only be targeted at one variable

2) Please explain the difference between Copy_to_user and __copy_to_user. A function or macro that is not underlined at the first time checks the requested linear address range for an extra period, while an underscore skips the check. When the kernel must repeatedly access the same linear area of the process address space, it is more efficient to start by checking the address only once, and then no longer have to check the process area.

5. What data is recorded by the jiffies variable in the 1inux kernel, and how to handle the overflow problem of jiffies.

Jiffies is the number of clock interrupts since boot, is a global variable in the kernel, is constantly growing and changing, its user space is not directly used. Like any int variable in C, jiffies overflows after the maximum value is exceeded

The following four macros are available in the Linux kernel to effectively address the error of program logic caused by jiffies overflow.

Time_after (A,B)

Time_before (A,B)

Time_before_eq (A,B)

6. Driver programming has the following common structure:

static struct File_operations Test_fops =

{

. Owner =this_module,//A pointer to a module that owns the structure, typically this_module

. Llseek = Test_llseek,//To modify the current read/write location of the file

. Read = Test_read,//synchronous reading of data from device

. Write = Test_write,//Send data to Device

. open = Test_open,//Open device

. IOCTL = TEST_IOCTL,//execute device I/O control commands

. Release = Test_release,//Turn off the device

};

Under what circumstances this structure is used. Explain the role of each member function in the structure and how the application implements calls to those interfaces.

The structure of the device file interface in a character device driver. After creating the appropriate device files, the application can implement calls to the above interfaces by Lseek, read, write, open, IOCTL, and close of the device files.

7. In the process of driver writing, we should pay attention to the concurrent control of the equipment.

1) Please give examples of the drivers need to pay attention to the situation of concurrency control;

Multiple execution units are executed concurrently and concurrently, while concurrent execution units access to shared resources results in competition,

Competition occurs when multiple processes open a device file and read and write at the same time.

2 What are the common technologies of concurrency in Linux? Briefly describe the differences between these technologies.

Spin Lock: Mutually exclusive, suitable for a short time, busy waiting

Semaphore: Multiple holders, also have mutex signal amount, sleep, can be applied to slow operation

8. In the CPU interface, often useful to the SPI, I2C and other devices and CPU interface, your project has used such interfaces. If so, please choose one of the first to explain its role in your system.

9. Read the following information and answer the questions

The tlv320aic23b is a high-performance stereo AUDIOCODEC the with highly integrated analog. The Analog-to-digitalconverters (ADCs) and Digital-to-analog converters (DACs) within thetlv320aic23b use Multibit sigma- Delta technology with integrated oversamplingdigital interpolation filters. Data-transfer word lengths of,, and 32bits, with the sample rates from 8 khz to khz, are supported. The Adcsigma-delta modulator features Third-order multibit architecture with up to90-dba signal-to-noise ratio (SNR) at AU Dio sampling rates up to-kHz, Enablinghigh-fidelity audio recording in a compact, power-saving design. The Dacsigma-delta modulator features a second-order multibit architecture with up TO100-DBA SNR at Audio sampling u P to-kHz, enabling high-quality digitalaudio-playback capability, while consuming less than MW during playbackonly. The tlv320aic23b is the ideal analog input/output (I/O) Choice forportable Digital Audio-player and recorder applications, such as MP3 Digitalaudio players.

The tlv320aic23b has the following set Ofregisters, which are the used to program the modes of operation.

Register Map:

Left line input channel Volume control (address:0000000)

1 Reset register, may need to which address of the register to set. Set the digital audio interface format, which address needs to use the register.

address:0001111 Registers

address:0000111 Registers

2 The current need for the tlv320aic23b chip to the left input volume control, how to set the Register value. Write a function that progressively increases volume and reduces control (each time a function call changes one volume, the operation does not affect other volume-independent bits of data, register bits are 32 bits, program runs the processor as a small-end mode).

The main needs of the operation is 1: The address of 0000000 Register Lim bit to be set to 0 normal;2:0000000 address of the register's low 5 bit d4~d0 bit to add and subtract operations, corresponding to adjust the volume size. Note the maximum volume and the minimum value of the judgment. Depending on whether the operation is appropriate for the d4~d0 operation.

10. In Linux-driven development, the knowledge involved includes driving development patterns, kernel-related knowledge, and hardware-related knowledge, and talk about your understanding of these aspects and your mastery.

What is the difference between TTL level and RS232 level. How transformations are implemented on the hardware.

18.

Node * reverselist (node *head)//List reverse order
{
if (head = = NULL | | head->next = NULL)
return head;
Node *p1 = head;
Node *p2 = P1->next;
Node *p3 = P2->next;
P1->next = NULL;
while (P3!= NULL)
{
P2->next = p1;
P1 = p2;
P2 = p3;
P3 = P3->next;
}
P2->next = p1;
head = p2;
return head;
}

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.