代码改变世界

linux 编程笔记 2

2013-11-13 11:19  zoo-code  阅读(213)  评论(0编辑  收藏  举报

1.使用create建立文件:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>


int main() {
      int res = creat("./file.txt", 0700);
    if (res == -1) {
        perror("Create File Error!");
    } else {
        printf("Create OK!\n");
    }
    return 0;
}

2.从输入到输出:

// 从stdin到sdtout

#include <stdlib.h>
#include <stdio.h>

int 
main (int argc, char *argv[])
{
    int c;
    
    while ((c = getchar()) != EOF) {
        putchar(c);
    }
    
    return 0;
}

3.简单实现who命令:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <utmp.h>
#include <unistd.h>
#include <time.h>

#define SHOWHOST

void show_info(struct utmp* user_record);
void login_time(long *num_time);

void show_info(struct utmp* user_record) {
    
    if (user_record->ut_type != USER_PROCESS) {
        return ;
    }

    printf("-------------------\n");

    printf("User Name is: %10s\n", user_record->ut_name);
    printf("User CMD is: %10s\n", user_record->ut_line);
    login_time(&(user_record->ut_time));

    #ifdef SHOWHOST
        printf("User Host is: %s\n", user_record->ut_host);
    #endif
        printf("\n");
}

void login_time(long *num_time) {
    char *timestr;
    timestr = ctime(num_time);
    printf("User login time is: %30s\n", timestr);
}

int main() {

      struct utmp current_record;
    int record_len = sizeof(current_record);
    int user_info_fd = -1; // user info in the utmp
    
    if ((user_info_fd = open(UTMP_FILE, O_RDONLY)) == -1) {
        perror("read file error!");
        exit(1);
    }

    while (record_len == (read(user_info_fd, &current_record, record_len))) {
        show_info(&current_record);
    }
    
    close(user_info_fd);
    
    return 0;
}

 

4.简单实现cp命令:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFFERSIZE 4096
#define FILEMODE 0700 // 111 000 000 User all per

void opps(char *file, char* argv);

void opps(char *file, char* argv) {
    fprintf(stderr, "Error : %s", file);
    perror(argv);
    exit(1);
}

int main(int argc, char* argv[]) {
      int in_fd = -1;
    int out_fd = -1;
    
    int n_chars = -1;
    char buf[BUFFERSIZE];

    if (argc != 3) {
        fprintf(stderr, "Usage: cp source dest\n");
        exit(1);
    }

    if ((in_fd = open(argv[1], O_RDONLY)) == -1) {
        opps("Open First File Error", argv[1]);
    }

    if ((out_fd = creat(argv[2], FILEMODE)) == -1) {
        opps("Creat Second File Error", argv[2]);
    }

    while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0) {
        if (n_chars != write(out_fd, buf, n_chars)) {
            opps("Write Error!", argv[2]);
        }
    }

    if (-1 == n_chars) {    
        opps("Read Error!", argv[1]);
    }

    if (close(in_fd) == -1 || close(out_fd)) {
        opps("Close Error!", " ");
    }

    return 0;
}

 

5.使用不同缓冲区的cp实验:

使用python得到5M多的一个文件

#!/usr/bin/env python
#-*- coding:utf-8 -*-

fd = open("./data", "w+");

for i in xrange(500000):
    fd.writelines("hello world!")

fd.close()

分别使用1 4026 20000做为buf的cp实验:

 

使用缓冲区的利弊:

利:
1. 提高磁盘I/O效率
2. 优化磁盘的写操作
利弊:
如果不及时写入磁盘,会导致数据丢失。

可以使用sync 将缓冲区数据写入磁盘 通过
man sync 来查看详细说明

6.一个进程多次打开一个文件:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main() {
      int read_fd = open("./data", O_RDONLY);
    int write_fd = open("./data", O_WRONLY);
    int read_again_fd = open("./data", O_RDWR);
    
    char buf[1024];
    read(read_fd, buf, 1024);
    buf[strlen(buf) - 1] = '\0';
    puts(buf);
    close(read_fd);

    char str[100] = "testing 123...";
    write(write_fd, str, strlen(str));
    close(write_fd);

    buf[0] = '\0';
    read(read_again_fd, buf, 1024);
    buf[strlen(buf) - 1] = '\0';
    puts(buf);
    close(read_again_fd);
    

    return 0;
}

每次都从最开始读取。