Deeply convinced that the Linux software development face test paper finishing __linux

Source: Internet
Author: User
Tags message queue mutex posix semaphore
1, the structure can be compared


int memcmp (const void * PTR1, const void * ptr2, size_t num);
Compare two blocks of memory
Compares the ' bytes ' of the ' the ' of memory pointed by PTR1 to the ' the ' of the ' the ', Bytes pointed by PTR2, zero If they all match or a value different from zero representing which are greater if they do not.


Notice that, unlike strcmp, the function does not stop comparing after finding a null character.


Returns an integral value indicating the relationship between the content of the memory blocks:
A zero value indicates that the contents of both memory blocks are.
A value greater than zero indicates that the ' the ' of the ' the ', ' does not match ' both memory blocks has a greater value in P TR1 than in PTR2 as if evaluated as unsigned char values; and a value less than zero indicates the opposite.

#include <stdio.h>
#include <string.h>

struct suo{
  char name[40];
  int age;
} person, person_copy;

int main ()
{
  int n;
  Char myname[] = "Pierre de Fermat";

  /* using memcpy to copy string:
  /* memcpy (Person.name, MyName, strlen (myname) +1);
  Person.age = +;

  /* Using memcpy to copy structure: * *
  memcpy (&person_copy, &person, sizeof (person));

  printf ("Person_copy:%s,%d \ n", Person_copy.name, person_copy.age);

n = memcmp (&person_copy, &person, sizeof (struct suo));
if (n > 0)
printf ("greater");
else if (n < 0)
printf ("smaller");
else
printf ("=\n");

  return 0;
}

2, maximum number of open files to view and set


To view the maximum number of open files at the system level # Cat/proc/sys/fs/file-max
View the maximum number of open files for the current user # Ulimit-hn//view hard limit
# ULIMIT-SN//view soft limit


System-Level Settings # vi/etc/sysctl.conf
Add: Fs.file-max = 100000
Immediate effect: # Sysctl-p


User-level settings # vi/etc/security/limits.conf
The settings are as follows:


httpd Soft Nofile 4096
httpd Hard Nofile 10240
HTTPD is a user, you can use the wildcard * to represent all users.
For the limits.conf file configuration to take effect, you must make sure that the pam_limits.so file is added to the startup file.
See the/etc/pam.d/login file for:


Session required/lib/security/pam_limits.so
You can also add ulimit-n 10240 to the back of the/etc/profile.
Takes effect immediately using the following command:


# SU-HTTPD
$ ulimit-hn 10240
$ ulimit-sn 4096


Hard limits can be set at any time in any process but hard limits can only be lifted by Superuser
Soft limits are limitations that the kernel actually executes, and any process can set the soft limit to any value that is less than or equal to the hard limit of the process limit


The C language file pointer (fopen) and the file descriptor (open) can be converted to each other:


int Fileno (FILE *stream);
FILE *fdopen (int fd, const char *mode);


3. Inter-process communication mode


A brief introduction to several main means of interprocess communication under Linux:
Pipelines (Pipe) and well-known pipelines (named Pipe): Pipelines can be used for communication between relational processes, which overcomes the limitations of pipe without name, and therefore allows communication between unrelated processes, in addition to the functionality of a pipe;
Signal (Signal): A signal is a more complex form of communication used to inform the receiving process that an event occurs, in addition to communication between processes, the process can send signals to the process itself; Linux in addition to supporting the UNIX early signal semantic function Sigal, It also supports the signal function sigaction of semantics conforming to POSIX.1 standard (in fact, the function is based on BSD, BSD in order to achieve reliable signal mechanism, but also can unify the external interface, with the Sigaction function to realize the signal function);
Message queue (Message Queuing): The messages queue is a linked table of messages, including POSIX Message Queuing system V Message Queuing. A process with sufficient permissions can add messages to the queue, and processes that are given Read permission can read messages in the queue. Message Queuing overcomes the lack of information load, the pipeline can only host unformatted byte streams and buffer size limitations.
Shared memory: Enables multiple processes to access the same memory space, which is the fastest available form of IPC. is designed for the low efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
Semaphore (semaphore): primarily as a means of synchronization between processes and between different threads of the same process.
Socket: A more general interprocess communication mechanism that can be used for interprocess communication between different machines. Originally developed by the BSD branch of the UNIX system, it is now generally possible to migrate to other Unix-like systems: Linux and System V variants support sockets.


http://www.ibm.com/developerworks/cn/linux/l-ipc/


In general, the process under Linux consists of several key elements:
There is an enforceable procedure;
There is a dedicated system stack space;
The kernel has its control block (Process Control block), describes the resources occupied by the process, so that the process can accept the kernel scheduling;
has separate storage space


4, Linux multithreaded synchronization of several ways


1 Mutual exclusion Lock (mutex)
Synchronization between threads is achieved by locking mechanism. Only one thread is allowed to execute a critical part of the code at the same time.
int Pthread_mutex_init (pthread_mutex_t *mutex,const pthread_mutex_attr_t *mutexattr);
int Pthread_mutex_lock (Pthread_mutex *mutex);
int Pthread_mutex_unlock (Pthread_mutex *mutex);
int Pthread_mutex_destroy (Pthread_mutex *mutex);


Mutual-exclusion lock static assignment pthread_mutex_t Mutex=pthread_mutex_initialier
attr_t are:
PTHREAD_MUTEX_TIMED_NP: Remaining threads waiting for queues
PTHREAD_MUTEX_RECURSIVE_NP: Nested locks allow threads to lock multiple times, different threads, and then compete again after unlocking
PTHREAD_MUTEX_ERRORCHECK_NP: Check the wrong, together with the thread request has been used to lock, return edeadlk;
PTHREAD_MUTEX_ADAPTIVE_NP: Adapt to lock, unlock and compete again


2) condition variable (cond)
A mechanism for synchronizing with global variables shared between threads. The basic operations on a condition variable are the trigger condition (when the condition becomes true), the wait condition, and the thread to suspend until another thread triggers the condition.
int Pthread_cond_init (pthread_cond_t *cond,pthread_condattr_t *cond_attr);
int pthread_cond_wait (pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timewait (pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
int pthread_cond_signal (pthread_cond_t *cond);
int Pthread_cond_broadcast (pthread_cond_t *cond); Unblocking All threads
int Pthread_cond_destroy (pthread_cond_t *cond);


pthread_cond_t Cond=pthread_cond_initialier


void Pthread_cleanup_push (void (*RTN) (void *), void *arg);
void Pthread_cleanup_pop (int execute);
Pthread_cleanup_push to register the cleanup function Rtn, this function has a parameter arg. The registered cleanup function is executed when one of the following three scenarios occurs:
1) Call Pthread_exit.
2) as a response to the cancellation thread request (Pthread_cancel).
3) Call Pthread_cleanup_pop with a non-0 parameter.
Attention:


1 if the thread terminates only because of a simple return, the purge function is not invoked.
2 If the Pthread_cleanup_pop is passed a 0 parameter, the Purge function is not invoked, but clears the cleanup function at the top of the stack.


3) Signal Volume (SEM)
#include <semaphore.h>
int Sem_init (sem_t *sem, int pshared, unsigned int value);
int sem_wait (sem_t *sem); -1
int Sem_post (sem_t *sem); +1
int Sem_destroy (sem_t *sem);


5. Small end
Big-endian Byte (Big-endian): Higher valid bytes are stored in lower memory addresses, and lower valid bytes are stored at higher memory addresses.
Small-byte (Big-endian): The high byte of the word data is stored in a high address, while the low byte of the word data is stored in a low address.
/******** big end return 0; small ends return 1*******/
int Checkcpu ()
{
Union W
{
int x;
Char y;
}c;
c.x = 1;


return (c.y==1);
}


6, the structure of the median field alignment
Because the bit field is not allowed to span two bytes, the bit field length does not exceed 8.


Storage principles:
The size of the entire bit-domain structure is an integer multiple of its widest base type member size;
If a bit field field is interspersed between fields with a non bit field, compression is not performed;
If the adjacent two-bit field fields are of different types, the specific implementations of each compiler differ, VC6 adopts the compression method, and GCC and dev-c++ are compressed;
struct BFA
{
unsigned char a:2;
unsigned int b;
};//GCC 8 bytes


struct BFB
{
unsigned char a:2;
unsigned char b:3;
unsigned char c:3;
unsigned int d:4; More out of this bit field field;
};//GCC 4 bytes
The address operator & cannot be applied to the domain field;
A bit field field cannot be a static member of a class;
The position of the bit field field in memory is placed in the order of low to high;
struct Bitfield
{
unsigned char a:2; Minimum position;
unsigned char b:3;
unsigned char c:3; Highest position;
};
Union Union
{
struct Bitfield BF;
unsigned int n;
};


Union union UBF;
UBF.N = 0; Class
UBF.BF.A = 0; Binary is: 00
ubf.bf.b = 0; Binary is: 000
UBF.BF.C = 1; Binary is: 001
printf ("UBF.BF.N =%u\n", UBF.N);
Results: 32


This paragraph is derived from http://bdxnote.blog.163.com/blog/static/844423520109103132722/


7, setsockopt (), the application of the Select () function


8. TCP/UDP C/S frame
See Huaqingyuan, "Introduction to UDP and TCP programming based on sockets" blog
Http://www.embedu.org/column/column179.htm


The application of Connect function in UDP
Http://www.embedu.org/Column/Column220.htm

http://blog.csdn.net/mycoolx/article/details/6314354


9, say you know the classic sort algorithm name








Related Article

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.