linux--System call Note 1

Source: Internet
Author: User
Tags bitwise

Underlying file access:
Process: A running program that has a number of file descriptors associated with the value, and how many file descriptors depend on the configuration of the system.
When a program starts running, three file descriptors are typically opened:
0: Standard input
1: Standard output
2: Standard error
Other file descriptors can be associated with files and devices through system calls (open).

Write system call:
Function: Writes the first nbytes bytes of a buffer (BUF) to the file associated with the file descriptor (fildes).
Write returns the number of bytes actually written, if there is an error in the file descriptor or if the driver for the underlying device is sensitive to the length of the data, indicating that a
Error, the return value may be less than nbytes. If the function returns 0, no data is written, and 1 indicates an error occurred in the write call, and the error code is saved in the
The global variable errno.

#include <stdio.h>#include<unistd.h>//the header file where the write calls the prototypeintMainvoid)    {      if(Write (1,"Here is some data\n", -)) != -) Write (2,"A Write error has occurred on file descriptor 1\n", $); return 0; }

[Email protected]:/home/linlin/linlin/c_code#./simple_write.out
Here is some data


Write may report less bytes written than required, which is not necessarily an error, in the program, you need to check the errno to find the error, and then call write again the left
Residual data

Read system call:
Function: Reads nbytes bytes of data from a file that is associated with a file descriptor (Fildes) and puts them in the data area buf.
Read returns the number of bytes actually read, which may be less than the number of bytes requested, if the read call returns 0, indicating that no data has been read, the end of the file has been reached;
1, which indicates that an error occurred in the read call.

#include <stdio.h>#include<unistd.h>    intMainvoid)    {      Charbuffer[ -]; intnread; Nread= Read (0, Buffer, -); if(-1==nread) Write (2,"A Read error has occurred\n", -); if(Write (1, buffer, nread))! =nread) Write (2,"A Write error has occurred\n", -); return 0; }


[Email protected]:/home/linlin/linlin/c_code# echo Hello there |./simple_read.out
Hello there

Open Call:
Role: Create a new file descriptor.

        #include <sys/types.h><sys/stat.h><fcntl.h>int open (  Constcharintint open (constcharint Flags, mode_t mode);

Open creates a access path to a file or device, and if the call succeeds, it can return a file descriptor that can be used by read,write and other system calls
This file descriptor is unique, and it is not shared with any other running process.
Prepares the name of the file or device to be opened as the parameter path passed to the function, the Oflags parameter specifies the action taken to open the file.
The Oflags parameter is specified by the way that the file access mode is combined with other optional modes:
Mode description
O_rdonly Open with read-only mode
O_wronly Open in write-only mode
O_rdwr Open in read and write mode

The open call can also include a combination of optional modes (bitwise OR operation) in the Oflags parameter:
O_append: Append write data to the end of file
O_trunc: Set the file length to 0, discard the existing content
O_creat: If required, create a file according to the access mode given in the parameter mode
O_EXCL: Used with o_creat to ensure that the caller creates the file. The open call is an atomic operation that executes only one function call. Use this optional mode
Can prevent two programs from creating the same file at the same time. If the file already exists, the open call fails.


Initial value of access rights:
When you use the alternate O_creat flag to create a file, you must use an open call with three parameter formats, and the third parameter mode is a bitwise OR of several flags
To, these flags are defined in Sys/stat.h:
S_IRUSR: Read permission, file owner
S_IWUSR: Write permission, file owner
S_IXUSR: Execute permissions, file owner
S_IRGRP: Read permission, file group
S_IWGRP: Write permission, file group
S_ixgrp: Execute permissions, file group
S_iroth: Read permissions, other users
S_iwoth: Write permissions, other users
S_ixoth: Execute permissions, other users

   #include <stdio.h><fcntl.h><sys/types.h><sys/stat.h>   int main (void) {open ("test", O_creat, S_irus r|     return0; }




Impact factors for file access:
The specified file permission is used only when the file is created
The Umask (user mask) affects access to the file being created, and the mode value given in the open call will be in and operation with the counter value of the user mask at the time
For example, umask specifies the S_ixoth mode flag for the 001,open call, and the file created does not have write execution permissions on other users.

The flag in the open and creat calls is actually a request to set the file access permission, and whether the requested permission will be set depends on the Umask value at that time.

Two different implementations of copying files:

#include <unistd.h>#include<sys/stat.h>#include<fcntl.h>    intMainvoid)    {      Charblock[1024x768]; int inch, out; intnread; inch= Open ("file.in", o_rdonly);  out= Open ("File.m.out", o_wronly| O_creat, s_irusr|s_iwusr);  while(Nread = Read (inch, block,sizeof(block))) >0) Write ( out, block, nread); return 0; }

#include <unistd.h>#include<sys/stat.h>#include<fcntl.h>    intMainvoid)    {      CharC; int inch, out; inch= Open ("file.in", o_rdonly);  out= Open ("File.out", o_wronly| O_creat, s_irusr|s_iwusr);  while(Read (inch, &c,1) ==1) Write ( out, &c,1); return 0; }



Close system call:
Use the close call to terminate the association between the file descriptor Fildes and its corresponding file. File Description RP is released and can be reused.
The close call returns 0 when it succeeds, and returns 1 when an error occurs.

#include <unistd.h>   int close (int fildes);



It is important to check the return results of the close call, and some file systems, especially the network file system, may not close the file before you report the file write operation
The error occurs because the data may not be written correctly when the operation is performed.


IOCTL system call:
Provides an interface for controlling the behavior of devices and their descriptors and for configuring the underlying services.
Terminals, file descriptors, socket settings tape drives can all have the ioctl defined for them.

#include <unistd.h>    int ioctl (intint cmd, ...);



The IOCTL executes the action given in the CMD parameter on the object referenced by the descriptor Fildes. Depending on the specific device support operation, it may have an optional third parameter.

linux--System call Note 1

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.