Linux programming-memoir 6: A memoir of linux Programming

Source: Internet
Author: User

Linux programming-memoir 6: A memoir of linux Programming

==== IPC ====
# Include <unistd. h>
Int pipe (int filedes [2]);
Create PIPIE, filedes [0] For read, filedes [1] for write, succeeded
Returns 0,-1 failed; generally used for interaction between processes, such as between shell
PIPE is a one-way stream with limited capacity.

# Include <stdio. h>
FILE * popen (const char * command, const char * mode );
Create a sub-process to execute SHELL commands, and establish a pipeline with it, and return
An error occurred while returning NULL for a file stream
Int pclose (FILE * stream );
Close the PIPE file stream and return the sub-process status.-1 error

Include <sys/stat. h>
Int mkfifo (const char * pathname, mode_t mode );
Create a FIFO pipeline and return 0,-1 error
Usually two unrelated programs are associated to share data, the same as PIPE.
Limited capacity transmission, cannot be used for a large number of byte streams

# Include <sys/ipc. h>
Key_t ftok (char * pathname, int proj );
A unique ipc key is created, and 0 is returned successfully. An error occurs in-1.

# Include <sys/types. h>/* For portability */# include <sys/msg. h>
Int msgget (key_t key, int msgflg );
If the message is returned, it indicates the idnumber. if the message is returned,-1 indicates an error.

# Include <sys/types. h>/* For portability */# include <sys/msg. h>
Int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg );
Message sent. 0 is returned successfully.-1 error

# Include <sys/types. h>/* For portability */# include <sys/msg. h>
Ssize_t msgrcv (int msqid, void * msgp, size_t maxmsgsz, long msgtyp, int msgflg );
Receive message, return the number of copied bytes, return-1 error

# Include <sys/types. h>/* For portability */# include <sys/msg. h>
Int msgctl (int msqid, int cmd, struct msqid_ds * buf );
Set the control option of the ipc msg queue, and return 0 success,-1 error

# Include <sys/types. h>/* For portability */# include <sys/sem. h>
Int semget (key_t key, int nsems, int semflg );
Returns an error in-1 when a semaphore ID value is obtained.

# Include <sys/types. h>/* For portability */# include <sys/sem. h>
Int semctl (int semid, int semnum, int cmd,.../* union semun arg */);
Set the semaphore option value and return-1 error

# Include <sys/types. h>/* For portability */# include <sys/sem. h>
Int semop (int semid, struct sembuf * sops, unsigned int nsops );
Set the semaphore operation. 0 is returned successfully, and an error occurs in-1.

# Include <sys/types. h>/* For portability */# include <sys/shm. h>
Int shmget (key_t key, size_t size, int shmflg );
Get the ID of the shared memory.-1 indicates an error.

# Include <sys/types. h>/* For portability */# include <sys/shm. h>
Void * shmat (int shmid, const void * shmaddr, int shmflg );
Get the address of the shared memory.-1 indicates an error.

# Include <sys/types. h>/* For portability */# include <sys/shm. h>
Int shmdt (const void * shmaddr );
Information about removing shared memory is not in use.-1 error is returned. 0 is successful.

# Include <sys/types. h>/* For portability */# include <sys/shm. h>
Int shmctl (int shmid, int cmd, struct shmid_ds * buf );
Setting the shared memory configuration option returns 0 success,-1 error

# Include <sys/mman. h>
Void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t offset );
Creates a memory ing and returns MAP_FAILED to indicate an error.

# Include <sys/mman. h>
Int munmap (void * addr, size_t length );
Memory ing removed. 0,-1 error returned successfully

# Include <fcntl. h>/* Defines O _ * constants */# include <sys/stat. h>/* Defines mode constants */# include <sys/mman. h>
Int shm_open (const char * name, int oflag, mode_t mode );
Creates or opens an existing shared memory object and returns the file descriptor.-1 error occurs.

# Include <sys/mman. h>
Int shm_unlink (const char * name );
If the shared memory object is removed, 0 is returned, and-1 fails.

==== File lock ====
# Include <sys/file. h>
Int flock (int fd, int operation );
Operation Lock operation on files
1. LOCK_SH place the shared lock
2. LOCK_EX placement of recursive locks
3. Unlock LOCK_UN
4. LOCK_NB no blocking lock
If the operation succeeds, 0 is returned. If the operation fails,-1 is returned.

==== Terminal ====
# Include <termios. h>
Int tcgetattr (int fd, struct termios * termios_p );
Int tcsetattr (int fd, int optional_actions, const struct termios * termios_p );
Gets/sets the terminal configuration information, returns 0 success,-1 failed

# Include <termios. h>
Speed_t cfgetispeed (const struct termios * termios_p );
Speed_t cfgetospeed (const struct termios * termios_p );
Get the terminal baud rate
Int cfsetospeed (struct termios * termios_p, speed_t speed );
Int cfsetispeed (struct termios * termios_p, speed_t speed );
If the baud rate is set, 0 or-1 is returned.

# Include <termios. h>
Int tcsendbreak (int fd, int duration );
Int tcdrain (int fd );
Int tcflush (int fd, int queue_selector );
Int tcflow (int fd, int action );
Terminal row control function, return 0 success,-1 failed

# Include <unistd. h>
Int isatty (int fd );
Determines whether it is a terminal. 1 is returned, 0 is not

# Include <unistd. h>
Char * ttyname (int fd );
Returns the terminal name. NULL indicates an error.

# Define _ XOPEN_SOURCE 600
# Include <stdlib. h>
# Include <fcntl. h>
Int posix_openpt (int flags );
Open an unused Pseudo Terminal, return the file descriptor, and return-1 error

# Define _ XOPEN_SOURCE 500
# Include <stdlib. h>
Int grantpt (int mfd );
Authorization terminal, used to be compatible with previous modes. 0 is returned for success and-1 is returned for failure.

# Define _ XOPEN_SOURCE 500
# Include <stdlib. h>
Int unlockpt (int mfd );
Unlock the pseudo-terminal master to create the pseudo-terminal slave.
Error returned-1, success returned 0

# Define _ XOPEN_SOURCE 500
# Include <stdlib. h>
Char * ptsname (int mfd );
Obtain the terminal name of the pseudo-doterminal slave.

==== Advanced IO ====
# Include <sys/time. h>/* For portability */
# Include <sys/select. h>
Int select (int nfds, fd_set * readfds, fd_set * writefds,
Fd_set * required TFDs, struct timeval * timeout );
Multiplexing tells the kernel about the file descriptors when one or more of them
After preparation, it is returned to the caller.
Nfds maximum descriptor + 1
Readfds readable file descriptor set
Writefds writable file descriptor set
Except TFDs sets file descriptors for handling exceptions
Timeout = NULL, always wait until the signal is captured or the description of the file of interest is ready
! = NULL. If TV _sec = 0 & TV _usec = 0, return immediately after the test. Otherwise, wait for the specified time.
Returns the number of ready file descriptors. If the number of file descriptors times out, 0 is returned. If the value-1 is returned, an error occurs.

# Include <sys/select. h>
Void FD_ZERO (fd_set * fdset );
Void FD_SET (int fd, fd_set * fdset );
Void FD_CLR (int fd, fd_set * fdset );
Int FD_ISSET (int fd, fd_set * fdset );
Clear the file descriptor set
Add file descriptor to collection
Remove a file descriptor from a collection
Test whether the file descriptor exists in the set. 1 is returned and 0 does not exist.

# Include <poll. h>
Int poll (struct pollfd fds [], nfds_t nfds, int timeout );
Similar to the select function, but it is more concise. Only one file descriptor array is constructed, and each
The element specifies the file descriptor and the state of interest. The function returns the number of ready file descriptors and returns
0 indicates timeout,-1 error

Disadvantages of the above two functions:
1. All file descriptor sets/arrays must be poll
2. Each call must pass information related to the file descriptor to the kernel and modify the kernel status.
Back
3. After the result is returned, the function needs to check which file descriptors are ready.

 

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.