Linux Programming Learning notes (7-15)

Source: Internet
Author: User
Tags flock fread message queue semaphore server memory

VII. Data Management

Memory management

Malloc,free,realloc and windows are the same, both ANSI C.

In fact, the application does not have direct access to physical memory, or it can obtain much larger memory space than actual memory through malloc, because the system uses swap space (swap spaces, which can be understood as virtual memory for Windows), if the requested memory is larger than the physical memory and swap space, Then the system will terminate this process prematurely.
If the process pointer operation is in the requested memory, the system will automatically alarm when the mobile such as + + is out of the allocated memory range.
If you copy data to a null pointer, an out-of-bounds alarm is emitted.

Allocation of memory structure definitions
struct Mem_control_block {
int is_available;
int size;
};

Detailed content can read this article "Memory Management Insider"
http://www-128.ibm.com/developerworks/cn/linux/l-memory/


File lock

Using o_creat| O_EXCL features can achieve the effect of lock file, the book is used to do the coordination between the process.
But I think it is useful to use a program to start only one instance, but the effect is not good, because the method of deleting the file, once encountered the exception of the previous process exits, the new process will be more troublesome later.
So it's still used
Fd=open (filename,o_wronly| O_creat, 0644);
if (Flock (fd,lock_ex| LOCK_NB)) {}
Can better implement a program with only one instance

fcntl and LOCKF use different underlying implementations, they cannot work at the same time, so they cannot be mixed. The
Fcntl can lock some areas of a file.
int fcntl (int fd,int cmd,struct flock * lock);
Fcntl () is used to manipulate some of the characteristics of a file descriptor. The parameter fd represents the file descriptor to be set, and the parameter cmd represents the command to be manipulated, and the lock information, which specifies the lock type and the specific file area. The
F_getlk gets the status of the file lock. The
F_setlk sets the state of the file lock. At this point the L_type value of the FLCOK structure must be f_rdlck, F_wrlck, or F_unlck. If the lock cannot be established, 1 is returned, and the error code is eacces or Eagain.
F_SETLKW F_SETLK works the same, but when a lock cannot be established, the call waits until the lock action succeeds. If the signal is interrupted while waiting for the lock, it returns 1 immediately and the error code is EINTR. The ll_type of the
Flcok structure has three states:
F_rdlck establishes a lock for reading, that is, a shared lock, where multiple processes can share a lock on the same zone, as long as a shared lock on a zone is set, and no exclusive lock can be set, with a value of 0.
F_wrlck establishes a lock for write purposes, that is, an exclusive lock, once set, the area can no longer set any locks, the value is 1. The
F_unlck deletes the previously established lock, which is the unlock.
Other parameters see P223, mainly to set the specific location and lock the process number.  
returns 0 if the return value succeeds, or 1 if there is an error, and the reason for the error is errno.

Note After the file area is locked, one is called with Read and write instead of Fread and fwrite, because Fread and fwrite have their own buffers.

Another function, LOCKF, is similar, but note that these 2 functions are recommended locks and do not really prevent us from reading from or writing data to a file. The detection of locks is the responsibility of the application. Fcntl and LOCKF's lock is not the same, mixed with the words will not be mutually.

Eight, MySQL

Some of the MySQL database operations can be seen in the "scattered things."

If an error is obtained using the function mysql_error (MySQL m_connection) to return the error code, Mysql_errno (MySQL m_connection) return the string is the error message.
Specific correspondence can be errmsg.h mysqld_error.h. You can see here "MySQL error code and error message control Daquan"
Http://www.e-gov.org.cn/xuexiyuandi/cunchu/200703/51606.html

Note that Mysql_affected_rows is the number of rows that return the change of this mysql_query operation, not how many rows are matched by where, in fact, the number of rows that we display after the direct operation of MySQL. If all data is delete, then 0 is returned.

Mysql_field_count can return the number of columns this query obtains, so that it does not cross the line in the sqlrow[] array.

Note the difference between Mysql_store_result and Mysql_use_result: The result of
1, Mysql_use_result () must be "disposable", that is, after using it to get a result, it must be used repeatedly mysql_ Fetch_row () reads the result until the function returns NULL, otherwise if you do a MySQL query again, you get "Commands out of sync; You can ' t run the This command now "error. and Mysql_store_result () results are saved, you do not have to finish reading all the rows, you can make another query. For example, you make a query, get a series of records, and then based on these results, with a loop and then the database query, you can only use Mysql_store_result (). The results of
2, Mysql_store_result queries using mysql_num_rows are the total number of rows. However, the result of the Mysql_use_result query using Mysql_num_rows is the current line line number . (It almost made me head, it was strange how not to get the total number of rows)
3, the remote Access database uses a different memory. The Mysql_store_result does not occupy the server memory, but the client's memory is sharply increased and client memory is freed after Mysql_free_result is executed. The Mysql_use_result does not occupy client memory and is not a small footprint for server-side memory, but the server-side memory is significantly increased after using Mysql_use_result.

It's all very simple, in the mysql_query directly using the MySQL statement on the line.

IX. Development Tools

I am currently using kdevelop, slightly.

About KDevelop, many of the settings are very strange, such as to the project to add existing files, need to be in the very hidden right column in the Automake to choose to add files, just the beginning, I want to add the existing files, have to create a new empty file name, and then overwrite.

For specific settings, you can read this article:
Http://blog.csdn.net/linux2linux/archive/2007/12/07/1923231.aspx
However, this tool has a problem, debugging time with no parameters, only the parameters set by the runtime can be brought into the program, really depressed.

Ten, commissioning

I am currently using kdevelop, slightly.
Like VC is very convenient, inside can also use GBD command, kdevelop inside no special look memory tools, so had to use GBD command, parameter is x\ digital sh 0x111111 number is generally 1, display to the string, if this area can have 0, That's going to increase the numbers.

Xi. Processes and Signals

Each process is numbered with its own unique number, which becomes the PID, the code obtains is the Getpid, the command line obtains is the PS aux
PID value range is 2 to 32768, the new process selection than the largest PID large 1 of the number to do their own PID, over the maximum value again starting from 2.
The number 1 is init-specific, set the background process (not run in the background, with & parameters run program, when disconnected, will stop running) is the zombie process, it needs to hang on the Init, the general way is to set up a child process, the parent process and then kill the parent process, The process hangs on the init.

EXECX is a series of substitution functions, and the actual function is to replace the code of the new application with the current one.
(Unlike the system, the system calls an external command in a blocking way, actually starting a shell and then invoking the application with the shell, unless the parameter & is not finished, it will not be returned.) It's just the same as script programming. )
I also found that the EXECX series uses a program that cannot execute Python (Python is, of course, an explanatory language, where the middle process may not be right)

Fork is the copy that generates a child process itself, that is, using fork to start a new process for the same program.
The difference is that the parent process calls the fork to return the child process number, which is the fork of the child process, and returns 0. This is what we often use, the return value is 0, indicating that it is a child process, the return value is not 0, the parent process of the description.

Wait this function causes the parent process to pause until the child process ends and returns the PID of the stopped subprocess. See examples of P391.

Attention! Even if the child process exits first, the main process then wait, you can also get the process number of the child process in the return value of wait, and also use wifexited to get the exit return value of the child process.
The reason is simple: the child process does not run when the parent process has exited, but does not actually quit, it still exists in the system until the parent process exits gracefully or the parent process calls wait. The child process can also be called a zombie process, but this zombie process is useless.
If the child process enters the zombie state and the parent process exits abnormally, the child process takes init as its own parent process until it is discovered and freed by Init.

There is also a command waitpid has a similar effect, and it can develop a subprocess number and set up a variety of processing behavior. Periodic check whether the child process ends can be used Waitpid (child_pid, (int *) 0, Wnohang); The child process does not end up returning 0, has finished returning the PID, failed to return 1, and the error message is placed in the errno.

The book says that process writing is simpler than threading, writing multiple collaborative processes is easier than threading, maybe I'm moving from windows, I'm not feeling it at all, I still like threading, and I don't know if continuing learning will change.

Signal: It feels a bit like a message in Windows, can send a signal to a particular process, and can send a custom signal (a custom signal value from 32 to 64) and throw a custom function. However, it will interrupt sleep, the main line is the main function, even if you write the sign call in the sub-thread, and most of all, the custom function has not finished processing the message, and then send the new message will be discarded, than the Windows message is far away, and can not take parameters, and is executed in the main thread, God knows will not have any problem.

1. The signal can interrupt sleep in main, no matter how many seconds it sleeps.
2, even if the alarm and signal in the child thread definition and invocation, all processing or in main is the main thread to deal with, the child line thread sleep will not be interrupted.
3,. Sig_ign ignores this signal and SIG_DFL restores the default handling of this signal.
4, most of the Linux signal is provided to the kernel, only a few signals can be sent between the signals, but 32 to 64 in the definition is empty, can be left to the user to use.
5, the use of kill can send a semaphore to the development process, where you can send a custom semaphore, in another process for this semaphore to the custom function.
6. Pause () pauses the current process (goes to sleep) until it is interrupted by the signal (signal).
7, note that if it is passed in 2 processes, then the effect is a non-blocking postmessage message. In addition, the message is not saved, that is, if the custom function has not finished processing the message, then the new message will be discarded, kill will not return 1, will return 0, but the actual code, the subsequent signal is not accepted.
8. Killall can send a signal to the process that is started by the same application.
9, if the signal is sent, the process is in the Select Wait, then return-1.
10, if the process does not handle this signal, whether it is the system signal or user-defined signal, will immediately kill the process. If Ctrl + Z turns into background processing, these signals are ignored. However, if you add & Start, you will still be killed by these undefined signals.

Some specific code for the signal, you can go to see "Linux function Collation and miscellaneous"

12. Threads

Remember when I was in college, we used the "operating system" book also said that Linux is not a thread, thread is the concept of Windows. The times have grown so fast.
However, if Linux does not have threads, it is not entirely unreasonable, at least Linux requires a dedicated line libraries to use the thread (pthread.h-lpthread).
It is because of the improvement, so the code of this book has several functions on our CS5 still have problems, but only a tiny an overall picture, you can see the Linux thread in the gradual specification.

Pthread_exit (void* retval) programming found that there can be no constants such as strings, and book inconsistencies, but can be null, in addition if 2 sub-threads with the same global variable (note that the local variables can not be given, the child thread exits, the stack is emptied), and 2 threads use it and set it to different values when calling Pthread_exit, then there is a definite conflict, that is, the results obtained in 2 Pthread_join 2 show the same value, even if their exit time is inconsistent.
If the child thread does not exit, the main thread is finished first, or the main thread calls Pthread_exit, the entire process exits directly, and the child thread ends automatically.
Pthread_join is the end of the child thread waiting to be drawn, otherwise it is blocked.

Using semaphores and mutexes to synchronize, the names are different, but the functionality is almost the same as on Windows.
The semaphore synchronization Sem_post is an atomic operation to a semaphore of +1, atomic operation, which cannot be interrupted by a higher level of the preemptive operation, only if it executes, the other high-priority to run. Other operations can be interrupted by higher priority, such as a signal that interrupts sleep, and so on.
Sem_wait is an atomic operation to the semaphore-1, if the current value is found to be 0, then will not be reduced operation, but into the waiting state, until there is a sem_post to the semaphore plus 1, if the current value is not 0, then-1 continue to run.
When 2 threads are synchronized, it is often necessary to have 2 semaphores, one on the other.

Pthread_mutex_lock will not be locked for the first time, equivalent to the initial value given by Sem_init is 1. When the second thread calls Pthread_mutex_lock, it blocks, and then waits for other threads to Pthread_mutex_unlock.
Actual coding time found that the original stdin standard input buffer is can store multiple input, example P420, if sleep time to change long, continuous input, every time gets the value is correct.

If 2 threads are locked for 3 times, then the deadlock can be changed by modifying the second parameter of the Pthread_mutex_init, such as return error or recursive lock, usually only empty, it is felt in the interface with other people do not transparent cooperation will be used, Because those people will call other interfaces in their callback function, and then create a deadlock, I previously handled the method is to recommend the partner with PostMessage to deal with, but if the use of recursive locking method, it is actually quite troublesome.

In fact, there is a possibility of causing deadlocks, such as a, A and a 2 method entry has the same lock, then a method calls the B method can cause a deadlock. You can use Pthread_mutexattr_settype to set a return error (Pthread_mutex_errorcheck) or a recursive locking (pthread_mutex_recursive).

The properties of the thread can be set by pthread_attr_xxx, but it feels like nothing to move, the only estimate that needs to be stacksize, but the default value is very large, and some do not support, so it is not very useful.

Pthread_cancel can stop developing a thread, including stopping itself. This function can be used even if no specific type is set within a child thread.

13. Interprocess communication: Pipeline

Pipelines are used to interact with data between processes.

Personal feeling Linux inside the process name to find the process number is very troublesome, may be spoiled on windows, it may be I did not find such a library bar. Many times, only use Popen to execute some shell commands, and then get the output content. Finally, and the most troublesome step--collating the string. Although the obtained file pointer is processed with the function of the operation files, it is closed with pclose.

The simple pipe is called by the pipe, will be read and write in an array of 2 elements, the label 1 is write, the label 2 is read, abide by the principle of FIFO. Then a process write a process read (if no data is blocked, if the file description is closed, it will return-1)
Note You can use this pipe call for more than 3 or 3 processes, but after a process reads one data, the other can only read the next data.
But this is mostly used between parent and child processes, and it is cumbersome to use between unrelated processes unless the file descriptor is passed as a parameter to the new process.


A named pipe is a special kind of file (of course, everything in Linux is a file), you can use Mkfifo on the command line, and the same name in C.
You cannot open a named pipe with O_rdwr because it is generally a single transfer, and if you want to bidirectional, we recommend that you open 2 named pipes.
Open file can be opened with 3 parameters 4 in combination, O_rdonly,o_wronly,o_nonblock
O_rdonly is read-only and is blocked if no other process is written to open.
o_rdonly| O_nonblock must succeed and return immediately.
O_wronly only writes, if no other process is read open it will block.
o_wronly| O_nonblock returns 1 if no process is read open

14: Semaphore, shared memory, Message Queuing

The main role of collaboration between the processes, hey, no more than the Windows custom message to use, the key I have not found, do not establish any pipeline or socket or any other processing, you can directly notify other processes through the process number of processing tasks, although the signal can be, but not with parameters, and the processing function does not end again will be discarded.

Semaphores are used to coordinate processes and threads, and I think it's enough to read and write files with file locks in multiple processes, primarily to coordinate shared memory.
Note If you do not use Semctl to set the Val value to 1, the default is 0.
P operation is to the Val value minus 1, if less than 0, blocking, know that someone to operate V, in fact, is added 1.

Shared memory allows 2 unwanted programs to access the same logical memory, which is simple to pass, but there is no synchronization mechanism that requires other methods to synchronize, such as with a signal or a semaphore.

Message Queuing is interesting, first of all asynchronous messages, independent and sending and receiving processes exist, can pass data, FIFO, can be more than one process to add messages, multiple processes read messages, but after a process read, the message from the message queue disappeared.
In addition, in the sending time without the application of their own memory, and do not worry about the data is modified, the system will automatically copy a piece of data placed in their own queue.

These 3 methods are marked with a constant key so that multiple processes can share the same semaphore, shared memory, or information queue.

XV: Sockets:

Never understand the first parameter of the socket is what use, originally in addition to network communication, can also use Af_unix to communicate between local processes.
After a string is marked, it can be read and written, it feels no channel to use.

As for af_inet and Windows there is no difference.

Socket multi-connection simple processing is to open a process for each connection, the resource consumption is high. Or use the Select mechanism.

Select if you encounter a timeout that returns 0, if an error returns-1, the other value is the socket corresponding.
The first parameter is the number of sockets that need to be tested, preferably the maximum socket value of +1.
About time-out, it is best to reset each cycle again, different systems may be different processing, will automatically modify the time-out time of this parameter is the remaining period, may affect the logic.

If you want to set non-blocking, you can set it with Fcntl.
int setnonblock (int sockid)
{
int f_old;
if (Sockid < 0)
{
return-1;
}
F_old = Fcntl (sockid, F_GETFL, 0);
if ( -1 = = f_old) return sockid;
F_old |= O_nonblock;

Return (Fcntl (Sockid, F_SETFL, F_old));}


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1766846

Linux Programming Learning notes (7-15)

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.