h>VcgLinux联盟 main()VcgLinux联盟 {VcgLinux联盟 system(“ls -al /etc/passwd /etc/shadow”);VcgLinux联盟 }VcgLinux联盟
|
执行 | -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwdVcgLinux联盟 -r--------- 1 root root 572 Sep 2 15 :34 /etc/shadowVcgLinux联盟
|
| |
VcgLinux联盟
| VcgLinux联盟 wait(等待子进程中断或结束) |
相关函数 | waitpid,forkVcgLinux联盟
|
表头文件 | #include<sys/types.h>VcgLinux联盟 #include<sys/wait.h>VcgLinux联盟
|
定义函数 | pid_t wait (int * status);VcgLinux联盟
|
函数说明 | wait()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status 返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则VcgLinux联盟
|
参数 | status可以设成NULL。子进程的结束状态值请参考waitpid()。VcgLinux联盟
|
返回值 | 如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。失败原因存于errno中。VcgLinux联盟
|
附加说明 | VcgLinux联盟
|
范例 | #include<stdlib.h>VcgLinux联盟 #include<unistd.h>VcgLinux联盟 #include<sys/types.h>VcgLinux联盟 #include<sys/wait.h>VcgLinux联盟 main()VcgLinux联盟 {VcgLinux联盟 pid_t pid;VcgLinux联盟 int status,i;VcgLinux联盟 if(fork()= =0){VcgLinux联盟 printf(“This is the child process .pid =%dn”,getpid());VcgLinux联盟 exit(5);VcgLinux联盟 }else{VcgLinux联盟 sleep(1);VcgLinux联盟 printf(“This is the parent process ,wait for child...n”;VcgLinux联盟 pid=wait(&status);VcgLinux联盟 i=WEXITSTATUS(status);VcgLinux联盟 printf(“child’s pid =%d .exit status=^dn”,pid,i);VcgLinux联盟 }VcgLinux联盟 }VcgLinux联盟
|
执行 | This is the child process.pid=1501VcgLinux联盟 This is the parent process .wait for child...VcgLinux联盟 child’s pid =1501,exit status =5VcgLinux联盟
|
| |
VcgLinux联盟
| VcgLinux联盟 waitpid(等待子进程中断或结束) |
相关函数 | wait,forkVcgLinux联盟
|
表头文件 | #include<sys/types.h>VcgLinux联盟 #include<sys/wait.h>VcgLinux联盟
|
定义函数 | pid_t waitpid(pid_t pid,int * status,int options);VcgLinux联盟
|
函数说明 | waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status可以设成NULL。参数pid为欲等待的子进程识别码,其他数值意义如下:VcgLinux联盟 pid<-1 等待进程组识别码为pid绝对值的任何子进程。VcgLinux联盟 pid=-1 等待任何子进程,相当于wait()。VcgLinux联盟 pid=0 等待进程组识别码与目前进程相同的任何子进程。VcgLinux联盟 pid>0 等待任何子进程识别码为pid的子进程。VcgLinux联盟 参数option可以为0 或下面的OR 组合VcgLinux联盟 WNOHANG 如果没有任何已经结束的子进程则马上返回,不予以等待。VcgLinux联盟 WUNTRACED 如果子进程进入暂停执行情况则马上返回,但结束状态不予以理会。VcgLinux联盟 子进程的结束状态返回后存于status,底下有几个宏可判别结束情况VcgLinux联盟 WIFEXITED(status)如果子进程正常结束则为非0值。VcgLinux联盟 WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。VcgLinux联盟 WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真VcgLinux联盟 WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。VcgLinux联盟 WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。VcgLinux联盟 WSTOPSIG(status)取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。VcgLinux联盟
|
返回值 | 如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。失败原因存于errno中。VcgLinux联盟
|
范例 | 参考wait()。VcgLinux联盟
|
| |
VcgLinux联盟
| VcgLinux联盟 fprintf(格式化输出数据至文件) |
相关函数 | printf,fscanf,vfprintfVcgLinux联盟
|
表头文件 | #include<stdio.h>VcgLinux联盟
|
定义函数 | int fprintf(FILE * stream, const char * format,.......);VcgLinux联盟
|
函数说明 | fprintf()会根据参数format字符串来转换并格式化数据,然后将结果输出到参数stream指定的文件中,直到出现字符串结束(' ')为止。VcgLinux联盟
|
返回值 | 关于参数format字符串的格式请参考printf()。成功则返回实际输出的字符数,失败则返回-1,错误原因存于errno中。VcgLinux联盟
|
范例 | #include<stdio.h>VcgLinux联盟 main()VcgLinux联盟 {VcgLinux联盟 int i = 150;VcgLinux联盟 int j = -100;VcgLinux联盟 double k = 3.14159;VcgLinux联盟 fprintf(stdout,”%d %f %x n”,j,k,i);VcgLinux联盟 fprintf(stdout,”%2d %*dn”,i,2,i);VcgLinux联盟 }VcgLinux联盟
|
执行 | -100 3.141590 96VcgLinux联盟 150 150VcgLinux联盟
|
| |
VcgLinux联盟
| VcgLinux联盟 fscanf(格式化字符串输入) |
相关函数 | scanf,sscanfVcgLinux联盟
|
表头文件 | #include<stdio.h>VcgLinux联盟
|
定义函数 | int fscanf(FILE * stream ,const char *format,....);VcgLinux联盟
|
函数说明 | fscanf()会自参数stream的文件流中读取字符串,再根据参数format字符串来转换并格式化数据。格式转换形式请参考scanf()。转换后的结构存于对应的参数内。VcgLinux联盟
|
返回值 | 成功则返回参数数目,失败则返回-1,错误原因存于errno中。VcgLinux联盟
|
附加说明 | VcgLinux联盟
|
范例 | #include<stdio.
|
|
|