Linux some important header files __linux

Source: Internet
Author: User
Tags message queue mutex semaphore
1. Operation function of system call---file

#inlclude <fcntl.h>

int open (char *name,int how) file Open

#include <unistd.h>

int close (int fd) file closed

size_t Read (int fd,void *buf, size_t count)

size_t write (int fd,const void *buf,size_t count)

Sleep (1) The system sleeps for one second, and the smallest unit is one second.

#include <stdio.h>

Perror ("Function name with Error")

#include <string.h>

char* strerror (int errnum) finds the error reason string based on the error code errnum

char* strcpy (char *dest,const char *src)

int strcmp (char *s1,const char *s2) s1 Returns a value of 0 if equal to S2

int strncmp (char *s1,const char *s2,int N) First n string comparison

2. Process Control function

#include <unistd.h>

pid_t fork (void) subprocess Returns a child process ID error returned in 0 parent process-1

pid_t getpid (void) Get process number
pid_t getppid (void)

pid_t vfork (void)

exec function Family

The process PID type is the pid_t type, which is included in #include<sys/types.h> if you define a process PID variable, you need to include this header file

Exit (n) The end process parent process can be obtained by the wait function to get the child process end mounted state.

At the end of the process, the file descriptor is closed, some cleanup is done, and only information such as process return status is preserved.

Call exit (), the subprocess closes all open process descriptor exit to clean up, for example, freeing up memory (the destructor is actively invoked in C + +), closing the work of the file handle, including refreshing the IO stream.

_exit (n) exits directly, does not do some cleanup work, and does not close the file descriptor.

#include <sys/wait.h>

pid_t Wait (int *status) waits for any child process to end. The child process end state value is returned by the status.

such as Wexitstatus (status) can get the value returned in exit (2), status=2, so you can know which process is waiting for. If you do not use this macro conversion, then status=512.

pid_t waitpid (pid_t pid,int status,int options) can specify a process that waits for a process number PID to end

The PID parameter is used when using the Waitpid function, so add the #include<sys/types.h>

About the process wait function there are many macros that convert status to the desired value and need to be understood.

3. Inter-process communication-pipelines

#include <unistd.h>

int pipe (int filedes[2])

4. Inter-process communication-Named pipes

#include <sys/types.h>
#include <sys/stat.h>

int Mkfifo (const char *pathname,mode_t mode)

Operations with normal files for named pipes

5. Message Queuing

The data type key_t is defined in the header file Sys/types.h, which is a long shaping data.

Key=ftok (".", ' A ') #include <sys/types.h> #include <sys/ipc.h>

Owning header file: #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>

Intmsgid;

Msgid=msgget (Key,ipc_creat | Ipc_excl | 0666);

Structmsg

{

Long Mtype;

Char mtext[50];

}MSG1,MSG2; Message Queuing buffers

intrcvtype=1;

MSGSND (msgid,&msg1,6,0) is 6 bytes, and the last parameter is filled with 0, which means that the function call blocks until the condition is met.

MSGRCV (msgid,&msg2,6,rcvtype,0) The last parameter can also be ipc_nowait, no message returned-1

Msgctl (Msgid,ipc_rmid,null); To delete a message queue

6. Inter-process communication-signal

#include <signal.h>

int Kill (pid_t pid,int Sig)

int raise (int signo); process sends signal to itself
Raise (Signo) is equivalent to Kill (Getpid (), signo);

Alarm (2) timed two seconds to produce SIGALRM signal, the system default processing is the end process.

The int pause (void)//pause function causes the calling process to suspend until a signal is caught.

Signal (Sigint,ctrl_c) Ctrl_c function name: Signal processing function is invoked after signal is emitted

7. Signal Set function Group blocking signal

The definition of the data structure of the signal set, sigset_t is the type of the structural body

sigset_t Intmask;

Sigemptyset (&intmask);

Sigaddset (&intmask,sigint);

Sigdelset (&intmask,sigint);

Sigprocmask (inthow,const sigset_t *set,sigset_t *oset)

Sigpromask (Sigblock,&intmask,null)

8. Thread

#include <pthread.h>

Thread ID type is pthread_t as struct type

Ways to get thread IDs
pthread_t Tid;
Tid=pthread_self ();

Pthread_create (&tid,null,thread,null) The second parameter is a thread property and the third parameter is a thread.   The fourth argument is that you can pass parameters to the thread. thread void *thread (void *arg)

The difference between pthread_exit ((void*) 2)///And return ((void*) 2). Who can answer ...

Void*tret;

Pthread_join (Tid,&tret);

Pthread_cancel (TID);

Pthread_cleanup_push (Fun,null); The first parameter is a cleanup function, the second is a pass parameter, and the thread cleanup handler

Pthread_cleanup_pop (0) 0 indicates that the thread ends with no cleanup function, not 0 execution, and two functions paired.

When Pthread_exit is invoked, or when the response is canceled, even if it is pthread_cleanup_pop (0), the cleanup function is also executed.

Mutual exclusion Lock

The mutex type is pthread_mutex_t mutex1

The Pthread_mutex_init (&mutex1,null) mutex is created, and the second parameter is NULL, which represents the default property.

Pthread_mutex_destory (&MUTEX1) clears a mutual exclusion lock

Pthread_mutex_lock (&MUTEX1)

Pthread_mutex_unlock (&MUTEX1)

Signal Volume

#include <semaphore.h>

The type of semaphore is sem_t sem1;

Initialization of Sem_init (&sem1,0,n) semaphore, the second parameter Linux failed to achieve the sharing of semaphore between processes, so the value is 0.

The third parameter is an unsigned integral type, and n represents the value of the semaphore initialization

Sem_wait (&SEM1) p operation

Sem_post (&sem1) v operation

Using PV to realize the mutual exclusion and synchronization function between threads

Intsem_getvalue (sem_t *sem) to obtain the value of the semaphore

Intsem_destroy (sem_t *sem) Delete semaphore

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.