printf(\ lseek(fd,0,SEEK_SET); ftruncate(fd,8); num=read(fd,buf2,8); if(num!=8) printf(\ write(1,buf2,8); close(fd); }
4£®ÊµÏÖ¡°catÎļþÃû¡±ÏÔʾÎļþÄÚÈÝ #include
main(int argc,char *argv[]) {
int fd; int num; char buf[10]; if(argc!=2) {
printf(\ exit(1); }
fd=open(argv[1],O_RDONLY); if(fd==-1) {
perror(\ exit(1); }
while((num=read(fd,buf,10))!=0) write(1,buf,num); close(fd); }
5£®ÊµÏÖ¡°cp ÔÎļþ Ä¿±êÎļþ¡± #include
main(int argc,char *argv[]) {
int from,to; int num; char buf[10]; if(argc!=3) {
printf(\ exit(1); }
from=open(argv[1],O_RDONLY);
to=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644); num=read(from,buf,10); while(num!=0) { write(to,buf,num); num=read(from,buf,10); }
close(from); close(to); }
6£®±àд³ÌÐòpro3.c£¬½«×Ö·û´®¡°hello world¡±Í¨¹ýÊä³öÖØ¶¨Ïò·½Ê½Ð´ÈëÎļþf1ÖÐ #include
7£®Ê¹ÓÃfork´´½¨½ø³Ì£¬ÔÚ×Ó½ø³ÌÖдòÓ¡¡°I am the child¡±ºÍ×Ó½ø³Ìpid£¬ÔÚ¸¸½ø³ÌÖдòÓ¡¡°I am the father¡±ºÍ¸¸½ø³Ìpid #include
pid_t pid; pid = fork(); if(pid < 0) {
perror(\ exit(1); }
else if(pid == 0)
printf(\ else
printf(\ exit(0); }
8£®´´½¨×Ó½ø³Ì£¬ÔÚ×Ó½ø³ÌÖÐÖ´ÐС°ps -A¡±ÃüÁ¸¸½ø³ÌµÈ´ý×Ó½ø³Ì½áÊøºó´òÓ¡¡°child over¡± ¼°Ëù´¦ÀíµÄ×Ó½ø³Ì½ø³ÌºÅ #include
9£®±àд³ÌÐò´¦ÀíSIGINTÐźţ¬µ±³ÌÐò½ÓÊÕµ½SIGINTÐźźóÊä³ö¡°SIGINT is caught¡± #include
void signal_handler(int signum) {
switch(signum) {
case SIGINT:
printf(\ break; } }
int main() {
signal(SIGINT,signal_handler); pause(); return 0; }
10£®Ê¹ÓÃPIPEʱÏÞ¸¸×Ó½ø³ÌÏò×Ó½ø³Ì·¢ËÍ1234567890£¬×Ó½ø³Ì½ÓÊÕ²¢ÏÔʾ #include
int pfd[2]; char buf[32]; pid_t pid; pipe(pfd);
if((pid=fork())<0) perror(\ else if(pid>0) {
close(pfd[0]);
write(pfd[1],\ } else {
close(pfd[1]);
read(pfd[0],buf,11);
printf(\ } }