Linux C header files function belongs
When programming, sometimes not always remember the function used in which library functions. Now first function header file that he often used to sum up before. There wrong place please advise.
**
- Operating function 1, the file system calls
**
#inlclude <fcntl.h>
int open (char * name, int how) the second parameter, O_RDONLY O_WRONLY O_RDWRO_CREAT
#include <unistd.h>
int close(int fd)
size_t read(int fd,void *buf, size_t count)
size_t write(int fd,const void *buf,size_tcount)
sleep (1) system sleep one second, the minimum unit of one second. // Used
#define msleep(x) usleep(x*1000)
msleep (500); system sleep 0.5 seconds
#include <stdio.h>
perror ( "error occurs in the function name")
#include <string.h>
char * strerror (int errnum) based on the error code errnum to find the cause of the error string
char * strcpy (char * dest, const char * src)
int strcmp (char * s1, const char * s2) s1 s2 equal to a value of 0 if the return value
int strncmp (char * s1, const char * s2, int n) comparing the first n string
**
- 2, process control function
**
#include <unistd.h>
Subprocess pid_t fork (void) Returns the parent process returns 0 subprocess ID Error -1
pid_t getpid(void) pid_t getppid(void)
pid_t vfork(void)
exec family of functions
Type process pid pid_t type, comprising in #include <sys / types.h> If the definition of a process variable pid, the need to include this header file
exit (n) by the end of the process the parent can wait function subprocess ends assembled state.
At the end of the process, we will close the file descriptor number, do some cleanup work, leaving only the returns process status information
Call exit (), the child process will close all open process descriptor exit will make clean-up work, for example, the release of memory (in C ++ which will take the initiative to call the destructor), close the file handle, including refresh IO streams .
_exit (n) to exit without doing some cleanup work, it will not close the file descriptor.
#include <sys/wait.h>
pid_t wait (int * status) wait for the end of any child process. Child process end status value returned by status.
As WEXITSTATUS (status) can be obtained exit (2) return value, status = 2, so you'll know which process is awaited. If you do not have this macro conversion, the status = 512.
End pid_t waitpid (pid_t pid, int status, intoptions) can be specified to wait for a process number pid process
Shihai use waitpid function uses the pid parameters, plus #include <sys / types.h>
There are many functions on the process waiting for the macro to convert the status to the desired value, you need to know.
**
- 3, interprocess communication - Pipeline
**
#include <unistd.h>
int pipe(int filedes[2])
**
- 4-process communication - Named Pipes
**
#include <sys/types.h> #include<sys/stat.h>
int mkfifo(const char *pathname,mode_tmode)
For the named pipe operations as with regular files
**
- 5, the message queue
**
Key_t data type is defined in the header file sys / types.h, which is a long integer data.
key=ftok(".",‘A’) #include <sys/types.h> #include<sys/ipc.h>
View header: #include <sys / types.h> #include <sys / ipc.h> #include <sys / msg.h>
int msgid;
msgid=msgget(key, IPC_CREAT | IPC_EXCL |0666);
struct msg
{
long mtype;
char mtext[50];
} Msg1, msg2; // message queue buffer
int rcvtype=1;
msgsnd (msgid, & msg1,6,0) 6 bytes, the last parameter represents 0 when a function call blocks until the condition is satisfied.
msgrcv (msgid, & msg2,6, rcvtype, 0) may also be the last parameter IPC_NOWAIT, not receiving messages -1
msgctl (msgid, IPC_RMID, NULL); delete message queue
**
- 6, interprocess communication - signals
**
#include <signal.h>
int kill(pid_t pid,int sig)
int raise (int signo); transmit a signal to the process itself
raise (signo) equivalent to kill (getpid (), signo);
After the alarm (2) the timing of two seconds, SIGALRM signal is generated, the system default handling process is ended.
int pause (void) // pause function suspends the calling process until a signal is captured.
signal (SIGINT, ctrl_c) ctrl_c as a function name function: signals, signal processing function calls
**
- 7, the signal blocking signal set function group
**
Defined set of signal data structure, sigset_t data type of structure
sigset_t intmask;
sigemptyset(&intmask);
sigaddset(&intmask,SIGINT);
sigdelset(&intmask,SIGINT);
sigprocmask(int how,const sigset_t*set,sigset_t *oset)
sigpromask(SIGBLOCK,&intmask,NULL)
**
- 8, thread
**
#include <pthread.h>
Thread ID is a type of structure type pthread_t
Way to get the thread ID pthread_t tid; tid = pthread_self ();
pthread_create (& tid, NULL, thread, NULL) to the second thread attribute parameter, the third parameter for the thread. The fourth parameter is a parameter that can be passed to the thread. Thread void * thread (void * arg)
pthread_exit ((void *) 2) // with ((void *) 2) the difference between return? Who can answer. . .
void *tret;
pthread_join(tid,&tret);
pthread_cancel(tid);
pthread_cleanup_push (fun, NULL); cleanup function of the first parameter, the second parameter is passed, cleanup handler thread
pthread_cleanup_pop (0); 0 means no cleanup function at the end of the thread, non-zero execution, two functions used in pairs.
When calling pthread_exit, or cancel the response, even pthread_cleanup_pop (0); also performs cleanup function.
Mutex
Mutex type is pthread_mutex_t mutex1
pthread_mutex_init (& mutex1, NULL) mutex is created, the second parameter is null, indicating default properties.
pthread_mutex_destory (& mutex1) Clear a mutex
pthread_mutex_lock(&mutex1)
pthread_mutex_unlock(&mutex1)
signal
#include <semaphore.h>
Semaphore type sem_tsem1;
sem_init (& sem1,0, n) initializing semaphores, Linux second parameter between processes failed to achieve shared semaphore, it is zero.
The third argument is an unsigned integer, n represents a value of the semaphore initialization
sem_wait (& sem1) P operation
sem_post (& sem1) V operation
Achieve mutual exclusion and synchronization between threads with PV
int sem_getvalue (sem_t * sem) get the value of the semaphore
int sem_destroy (sem_t * sem) deletes the semaphore
Reprinted from: https://www.cnblogs.com/jing1617/p/8213872.html