system calls and library functions under Linux

Source: Internet
Author: User

Library functions are based on system calls, eg:printf (library functions), and write (System calls). After redirection, the contents of fwrite were printed two times in the file because: 1. Buffer mode changed (from row buffer to full buffer) 2. The library function has a buffer, and the system call has no buffer.

Write prototype: ssize_t write (int fd, const void *buf, size_t count);

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h>int main () {        pid_t id;         printf ("Hello world");         const  char* msg= "I love you";         write (1,msg,strlen (msg));         id=fork ();         if ( id<0)         {                 perror ("fork");                 exit (1);         }        else if (id==0)          {                printf ("pid: %d, Ppid: %d ", Getpid (), Getppid ());        }         else        {                  printf ("pid: %d,ppid: %d", Getpid (), Getppid ());         }          return 0;}

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/B4/wKiom1cHYvfCiq7PAAAUks-Kdsc393.png "title=" QQ picture 20160408154918.png "alt=" Wkiom1chyvfciq7paaauks-kdsc393.png "/>

REDIRECT output to file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/B0/wKioL1cHZD_Ri0bCAAAdMZVpSoo684.png "title=" 10160408155156.png "alt=" Wkiol1chzd_ri0bcaaadmzvpsoo684.png "/>

Modify the above code as: int main () {pid_t id;       printf ("Hello World");       Const char* msg= "I Love You";       Close (1);       file* fp=fopen ("File", "w+");       Write (1,msg,strlen (msg));       Id=fork (); ...}

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/B0/wKioL1cHZmmDCkKCAAAkoTsY-MY548.png "title=" QQ picture 20160408155943.png "alt=" Wkiol1chzmmdckkcaaakotsy-my548.png "/>

Close file descriptor: Standard output, the next file descriptor will start from the front empty small integer subscript, at which time file occupies the descriptor 1.

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1761786

system calls and library functions under Linux

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.