当前位置:首页 > 操作系统 > Linux

Linux 应用程序 之 IO编程(一)

  我的linux 环境是windows8.1 + VMware6.5.1+  Fedora14,参考书籍:第六章

      链接:Linux应用程序开发详解(1-11).pdf

 

我利用一个SSH软件SSH Secure File Transfer Client 来从Linux传输文件

技术分享

来张虚拟机运行Fedora的图:

技术分享 

 

下面步入正题:

IO最基本操作:

 

             1
            //
            hello.c
             2 #include <unistd.h>
 3 #include <sys/types.h>
 4 #include <sys/stat.h>
 5 #include <fcntl.h>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 #include <stdio.h>
 9#define MAXSIZE
10int main(void)
11{
12int i,fd,size,len;
13char *buf ="hello! I‘m wirting to this file!";
14char buf_r[10];
15     len= strlen(buf);
16if((fd = open("/tmp/hello2.txt",O_CREAT |O_TRUNC | O_RDWR,0666))<0)//打开文件
17    {
18         perror("open:");
19         exit(1);
20     }else21    {
22      printf("open file :hello2.txt %dn",fd);
23    }    
24if((size =  write( fd, buf,len))<0)//写固定长度数据,buf
25    {
26         perror("write:");
27         exit(1);
28     }else29    {
30         printf("Wirte %s size=%dn",buf,size);
31    }
32     lseek(fd,0,SEEK_SET);//文件指针定位到文件头
33if((size = read(fd,buf_r,10))<0)//读10个字节的数据,这里没有考虑字符串最后一个字节为‘‘,要完善
34    {    
35         perror("read:");
36         exit(1);
37     }else38    {
39         printf("read from file:%s size=%dn",buf_r,size);
40           } 
41if( close(fd) < 0)//别忘记关闭
42    {
43         perror("Close:");
44         exit(1);
45     }else46    {
47         printf("Close hello2.txtn");
48    }
49     exit(0);
50 }

文件的访问涉及到进程间同步、互斥问题,采用Linux的fcntl()函数来加锁。

             1
            //
            function lock_set 
             2
            void lock_set(int fd,int type)
 3{
 4struct flock lock;
 5lock.l_whence = SEEK_SET;
 6lock.l_start = 0;
 7lock.l_len = 0;
 8while(1)
 9    {
10lock.l_type = type;
11if(fcntl(fd, F_SETLK,&lock) == 0) //如果要操作的文件已经加"写入锁"则fcntl() 返回非0值
12        {
13if(lock.l_type == F_RDLCK)
14                 printf("read lock set by %dn",getpid());
15elseif (lock.l_type == F_WRLCK)
16                 printf("write lock set by %dn",getpid());
17elseif (lock.l_type == F_UNLCK)
18                 printf("release lock by %dn",getpid());
19return ;
20        }
21         fcntl(fd, F_GETLK,&lock);//上锁失败说明文件被锁住,则用"F_GETLK"来获取该锁的类型,包括上锁线程ID
22if(lock.l_type != F_UNLCK)
23        {
24if(lock.l_type == F_RDLCK)
25                 printf("read lock already set by %dn",
26lock.l_pid);
27elseif(lock.l_type == F_WRLCK)
28                 printf("write lock already set by %dn",
29lock.l_pid);
30             getchar();
31        }
32    }
33 }

下面演示A进程给文件上"写入锁",然后B进程给文件上"读锁"的实验现象:
Souce Code  of A :

             1
            //
            hello3.c 即A 进程
             2 #include <unistd.h>
 3 #include <sys/file.h>
 4 #include <sys/types.h>
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include <fcntl.h>
 8//function lock_set 
 9//...10int main(void)
11{
12int fd;
13     fd=open("/tmp/hello3.txt",O_RDWR | O_CREAT,0666);
14if(fd < 0)
15    {
16         perror("open");
17         exit(1);
18    }
19//lock "write"20    lock_set(fd, F_WRLCK);//给"hello3.txt"文件上"写入锁"
21    getchar();//等待,(按回车继续执行程序)
22//then unlock23    lock_set(fd,F_UNLCK);//去锁
24    getchar(); 
25    close(fd);
26     exit(0);
27 }

Souce Code  of B:

             1
            //
            hello4.c   即B进程
             2 #include <unistd.h>
 3 #include <sys/file.h>
 4 #include <sys/types.h>
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include <fcntl.h>
 8//function lock_set 
 9//...10int main(void)
11{
12int fd;
13     fd=open("/tmp/hello3.txt",O_RDWR | O_CREAT,0666);
14if(fd < 0)
15    {
16         perror("open");
17         exit(1);
18    }
19//lock "read"20    lock_set(fd,F_RDLCK);//给我文件hello3.txt上"读锁"
21    getchar();
22//then unlock23    lock_set(fd,F_UNLCK);
24    getchar(); 
25    close(fd);
26     exit(0);
27 }

 

运行效果,

 Step1:运行A进程(./hello3):

技术分享

Step2:打开另一终端,运行B进程(./hello4):

技术分享

Step3:A进程对应终端中按下回车键:

技术分享

Step4:B进程终端中按下回车键:

技术分享

OK,读写锁实验结束。

下面再了解下IO多路复用(用select),这个对我来说有点难理解,贴上代码和效果图,以后斟酌。

             1
            //
            select.c
             2 #include <fcntl.h>
 3 #include <stdio.h>
 4 #include <unistd.h>
 5 #include <stdlib.h>
 6 #include <time.h>
 7int main(void)
 8{
 9int fds[2];
10char buf[7];
11int i,rc,maxfd;
12    fd_set inset1,inset2;
13struct timeval tv;
14if((fds[0] = open("/tmp/hello5.txt",O_RDWR | O_CREAT,0666))<0)
15         perror("open hello5");
16if((fds[1] = open("/tmp/hello50.txt",O_RDWR | O_CREAT,0666))<0)
17         perror("open hello50");
18if((rc = write(fds[0],"hello!n",7)))
19         printf("rc=%dn",rc);
20     lseek(fds[0],0,SEEK_SET);
21     maxfd = fds[0]>fds[1] ? fds[0] : fds[1];
22     FD_ZERO(&inset1);
23     FD_SET(fds[0],&inset1);
24     FD_ZERO(&inset2);
25     FD_SET(fds[1],&inset2);
26     tv.tv_sec=2;
27     tv.tv_usec=0;
28while(FD_ISSET(fds[0],&inset1) || FD_ISSET(fds[1],&inset2))
29    {
30if(select(maxfd+1,&inset1,&inset2,NULL,&tv)<0)
31             perror("select");
32else33         {
34if(FD_ISSET(fds[0],&inset1))
35            {
36                 rc = read(fds[0],buf,7);
37if(rc>0)
38                {
39                  buf[rc]=;
40                  printf("read %sn",buf);
41                 }else42                  perror("read");
43            }
44if(FD_ISSET(fds[1],&inset2))
45            {
46                 rc = write(fds[1],buf,7);
47if(rc>0)
48                {
49                  buf[rc]=;
50                  printf("rc=%d,write:%sn",rc,buf);
51                 }else52                     perror("write");
53                 sleep(10);
54            }
55         }
56    }
57     exit(0);
58 }

效果图:

技术分享

感谢您的阅读,boyang987 。转载请注明出处。

 

原文:http://www.cnblogs.com/boyang987/p/4226856.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐