Linux system function Open,read,write

Source: Internet
Author: User

Implementation code

#include <sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<unistd.h>#include<stdio.h>//perror ()#include <stdlib.h>//exit ()#defineBuffer_size 1024intMainintargcChar*argv[]) {    intfrom_fd; intto_fd; unsignedintFile_len; intRET =1; CharBuf[buffer_size]; Char*ptr; if(ARGC! =3) {printf ("usage:%s fromfile tofile\n", argv[0]); Exit (1); }    //Open source FileFROM_FD = open (argv[1],o_rdonly); if(FROM_FD = =-1) {printf ("Open%s failed.\n", argv[1]); Exit (1); }    //open a file in a write-only manner, without creating a fileTO_FD = open (argv[2],o_wronly| O_creat| O_append,0666);//using 0666    if(TO_FD = =-1) {printf ("Open%s failed.\n", argv[2]); Exit (1); }    //the size of the source fileFile_len =Lseek (FROM_FD,0, Seek_set);//offset must be repositionedprintf"%s size is%d bytes.\n", argv[1],file_len);  while(ret) {ret=read (from_fd,buf,buffer_size); if(ret = =-1) {perror ("Read error.\n");        } write (To_fd,buf,ret); File_len-=ret; //bzero (buf,buffer_size); } printf ("there is%d bytes data left without copy in%s\n", file_len,argv[1]);    Close (FROM_FD);    Close (TO_FD); return 0;}

1. Note that permissions must use 0666, preferably a macro

2. More efficient operation with mmap

  

#include <sys/mman.h>#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainvoid){    intLen; //open a file and open it in a read-write manner    intFD; FD= Open ("Hello", O_RDWR);//Hello file exists    if(FD = =-1) {perror ("Open"); return-1; } Len= Lseek (FD,0L, seek_end);//Lseek (Fd,0,seek_set); //mapping files to the virtual address space of a process    void*p = Mmap (null,len,prot_read| prot_write,map_shared,fd,0); if(p==map_failed) {Perror ("mmap"); return-1; }    int*q = (int*) p; q[0] =0x20202020; q[1] =0x41414141;    Munmap (P,len); Close (FD);}

Use Mmap to map physical memory to the virtual address space of a process

Mmap (2) system call, implementing code in kernel, kernel state

#include <sys/mman.h>
void *mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset);
mapping files and devices to the virtual address space of a process
All Files under Linux
Parameters:
Addr: It specifies the start address, in the virtual address space of the process, the value is null, the kernel determines the specific address
Length: Specifies the size of the mapped area
Prot
Prot_exec Pages may executed.

Prot_read Pages may be READ.

Prot_write Pages may written.

Prot_none Pages may isn't accessed.
Flags
map_shared Share this mapping. Changes to the mapped area are reflected in other processes, synchronized to the underlying underlying file
Map_private Create a PRIVATE copy-on-write mapping. Mappings for mapped areas are not reflected in other processes and are not synchronized to the file unless Msync
Map_anonymous Anonymous mappings, and file-independent, mapped areas are all cleared to 0
FD:-1
Offset: File offsets 0
return value:
Correct: insinuate Region first Address
Error: map_failed (void*)-1 errno is set
int Munmap (void *addr, size_t length);
de-mapping
return value of Addr:mmap ()
Length: Specifies how long the map area is to be unmapped
Return value: Success is 0, error -1,errno is set

Linux system function Open,read,write

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.