Apue-chapter 1 Basics of Unix

Source: Internet
Author: User

1.c++ implementing the LS command

#include <dirent.h>#include<stdlib.h>#include<iostream>#include"apue.h"using namespacestd;intMainintargcChar*argv[]) {DIR*DstructDirent *Dirp; if(argc!=2) {cout<<"Usage:ls Directory_name"<<Endl; Exit (-1); }    if((Dp=opendir (argv[1]))==NULL) {cout<<"can ' t open"<<argv[1]<<Endl; }    //Looping through catalog entries     while((Dirp=readdir (DP))! =NULL) {        //Output File namecout<<dirp->d_name<<Endl;    } closedir (DP); Exit (0);}

The Opendir function returns a pointer to the DIR structure, and we pass the pointer to the READDIR function. Call Readdir in the loop to read each directory entry. The function returns a pointer to the dirent structure, and returns a null pointer when no directory entry is readable in the directory. The name of each directory entry removed from the dirent structure is then exported in the loop.

2.c++ to implement content read from the output from the standard input

#include"apue.h"#include<iostream>using namespacestd;#defineBuffsize 4096intMain () {intN; //Buffering    CharBuf[buffsize]; //read from standard input, buffer size is buffsize, buffer corresponds to BUF     while((N=read (stdin_fileno,buf,buffsize)) >0){        //content in BUF output to standard output        if(Write (stdout_fileno,buf,n)! =N) cout<<"Wirte Error"<<Endl; }    //Error reading standard input    if(n<0) cout<<"Read Error"<<Endl; Exit (0);}

Run:

3. Use standard I/O to implement the content in Example 2

Standard I/O functions provide a buffered interface for those I/O functions that do not have buffering. There is no need to worry about choosing the best buffer size using standard I/O functions.

The functions in Example 2 are implemented with the GETC function and the PUTC function.

#include"apue.h"#include<iostream>using namespacestd;intMain () {intC;  while((C=getc (stdin))! =EOF) {        if(PUTC (c,stdout) = =EOF) cout<<"Output Error"<<Endl; }    if(Ferror (stdin)) cout<<"Input Error"<<Endl; return 0;}

3. Output Process ID

UNIX ensures that each process has a unique numeric identifier, which becomes the process ID. The process ID is a non-negative integer.

The following is a program code that outputs the process ID of this program.

" apue.h "  <iostream>usingnamespace  std; int Main () {    cout<<"Hello World from process ID"<<getpid () <<Endl;     return 0 ;}

Run two times respectively, the output is as follows:

Hello World from Process ID18357

Hello World from Process ID18385

4. Opening a new process command to execute input

Use C + + to open new processes for standard input and input commands.

#include"apue.h"#include<sys/wait.h>#include<iostream>using namespacestd;intMain () {CharBuf[maxline];//maxline->4096pid_t pid; intstatus; cout<<"%% ";/*Print prompt (printf requires% to print%)*/     while(Fgets (buf,maxline,stdin)! =NULL) {        //each line returned by the FETs function ends with a newline character, followed by a ' + ', which requires replacing the newline symbol with '        if(Buf[strlen (BUF)-1]=='\ n')//the last character is a line breakBuf[strlen (BUF)-1]=' /';//replace it with an end symbol//Call Fork () to create a new process, fork returns the ID of the child process to the parent process, and the child process returns a 0.fork call once for each call in the parent and child processes        if((Pid=fork ()) <0) {cout<<"Fork Error"<<Endl; }Else if(pid==0){//successfully created child process//executes the BUF corresponding command in the child process,EXECLP (Buf,buf, (Char*)0); cout<<"could ' t execute:"<<buf<<Endl; Exit (127); }        //parent process waits for child process to end//The waitpid () function returns the terminating state of the child process, saved in the status        /*Parent*/        if(Pid=waitpid (Pid,&status,0)) <0) cout<<"wait pid error!"<<Endl; cout<<"%% "; }    return 0;}

Execution process:

Lsbcmakefiles     hello_world      MakefileCMakeCache.txtcmake_install.cmake  hello_world.cbp%% date2016 Year Sunday, March 20 23:01:21 CST

The emphasis of the above program is function fork and function waitpid. Fork to create a new process. Fork returns the process ID (non-negative integer) of the child process to the parent process, returning 0 to the sub-process. The fork is called once, and is returned once for both the parent and child processes.

The parent process waits for the child process to terminate by Waitpid, whose parameters specify the process to wait (PID designation), returning the terminating state (status variable) of the child process.

Apue-chapter 1 Basics of Unix

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.