Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58478
  • 博文数量: 8
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 88
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-13 16:18
个人简介

不积跬步无以至千里,不积小流无以成江河。

文章分类
文章存档

2014年(8)

我的朋友

分类: 其他UNIX

2014-11-01 14:02:01


点击(此处)折叠或打开

  1. //
  2. // practice10.c
  3. // unix_test
  4. //
  5. // Created by 逄洪江 on 14/10/31.
  6. // Copyright (c) 2014年 isee. All rights reserved.
  7. //

  8. #include <stdio.h>
  9. #include <sys/signal.h>
  10. #include <signal.h>

  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <signal.h>
  16. #include <termios.h>
  17. #include <errno.h>
  18. #include <pwd.h>
  19. #include <string.h>
  20. #include <setjmp.h>
  21. #include <time.h>
  22. #include <string.h>



  23. void sig_alrmp10_12(int signo) {
  24.     printf("\nalrm\n");
  25. }
  26. int practice10_12(){
  27.     ssize_t n = 0;
  28.     char * buffer = NULL;
  29.     signal(SIGALRM, sig_alrmp10_12);
  30.     buffer = malloc(sizeof(char) * 1024 * 1024 * 1024);
  31.     
  32.     for (n = 0 ; n < 1024 * 1024 * 1024 - 1; n++) {
  33.         buffer[n] = rand()%26 + 'A';
  34.         if ( !(n % 100)) {
  35.             n++;
  36.             buffer[n] = '\n';
  37.         }
  38.     }
  39.     alarm(1);
  40.     printf("start\n");
  41.     n = fwrite(buffer,1 , 1024*1024*1024,fopen("/Users/panghongjiang/123.txt", "wb"));
  42.     printf("end\n");
  43.     if (n < 0 ) {
  44.         if ( errno == EINTR ) {
  45.             printf("INTR\n");
  46.         }
  47.     }
  48.     printf("n={%zd}\n",n);
  49.     
  50.    
  51.     return 0;
  52. }
  53. #define BUFFERSIZE 100
  54. char buffer10_11[1024]={0};
  55. void sig_xf(int signo ){
  56.     sprintf(buffer10_11,"\n\n sig_xf\n\n");
  57.     write(STDOUT_FILENO, buffer10_11, strlen(buffer10_11));
  58. }
  59. //10.11中没有如答案中说说的在当前文件偏移量处(文件尾端)尝试一次write,将收到SIGXFSZ信号。gcc -o ./t 文件名 ./t < input.txt
  60. int practice10_11(){
  61.     
  62.     char buffer[BUFFERSIZE];
  63.     ssize_t n = 0;
  64.     signal(SIGXFSZ, sig_xf);
  65.     
  66.     struct rlimit oldr,newr;
  67.     getrlimit(RLIMIT_FSIZE, &oldr);
  68.     newr = oldr;
  69.     newr.rlim_cur = 1024;
  70.     setrlimit(RLIMIT_FSIZE, &newr);
  71.     
  72.     
  73.     sprintf(buffer10_11,"rc%llu,rm%llu\n",oldr.rlim_cur,oldr.rlim_max);
  74.     write(STDOUT_FILENO, buffer10_11, strlen(buffer10_11));
  75.     sprintf(buffer10_11,"rc%llu,rm%llu\n",newr.rlim_cur,newr.rlim_max);
  76.     write(STDOUT_FILENO, buffer10_11, strlen(buffer10_11));
  77.     
  78.     
  79.     while ( 0 < (n = read(STDIN_FILENO,buffer,BUFFERSIZE))) {
  80.         if (BUFFERSIZE > (n = write(STDOUT_FILENO, buffer, n) )) {
  81.             sprintf(buffer10_11,"\nwrite error [%zd]\n",n);
  82.             write(STDOUT_FILENO, buffer10_11, strlen(buffer10_11));
  83.         }
  84.     }
  85.     write(STDOUT_FILENO, buffer, 100);
  86.     
  87.     
  88.     sprintf(buffer10_11,"\nread error [%zd]\n",n);
  89.     write(STDOUT_FILENO, buffer10_11, strlen(buffer10_11));
  90.     return 0;
  91. }
  92. int practice10_10(){
  93.     
  94.     
  95.     pid_t pid ;
  96.     
  97.     if (0 > (pid = fork())) {
  98.         printf("fork error\n");
  99.     }else if (0 == pid ) {
  100.         sigset_t newsig,oldsig;
  101.         int i = 0;
  102.         sigfillset(&newsig);
  103.         sigdelset(&newsig, SIGINT);
  104.         sigprocmask(SIG_SETMASK, &newsig, &oldsig);
  105.         
  106.         while (1) {
  107.             if ( 1 == getppid()) {
  108.                 break;
  109.             }
  110.             
  111.             sleep(5);
  112.         }
  113.         
  114.         while (1) {
  115.             if (i > 4) {
  116.                 i = 0;
  117.                 system("date");
  118.                 printf("ppid%d,pid%d\n",getppid(),getpid());
  119.             }
  120.             sleep(60);
  121.             i++;
  122.         }
  123.         _exit(0);
  124.     }
  125.     
  126.     return 0;
  127. }

  128. volatile sig_atomic_t flag = 0;
  129. sigset_t wtzero,newsig,oldsig;
  130. void sig_func_practice10(int signo){
  131.     flag = 1;
  132. }

  133. void initset(){
  134.     sigemptyset(&wtzero);
  135.     sigemptyset(&newsig);
  136.     sigemptyset(&oldsig);
  137.     sigaddset(&newsig, SIGUSR1);
  138.     signal(SIGUSR1, sig_func_practice10);
  139.     sigprocmask(SIG_BLOCK, &newsig, &oldsig);
  140. }

  141. void tell_practice10(pid_t pid){
  142.     kill(pid, SIGUSR1);
  143. }
  144. void wait_practice10(){
  145.     while (0 == flag) {
  146.         sigsuspend(&wtzero);
  147.     }
  148.     flag = 0;
  149. }

  150. void unlock_practice10(){
  151.     sigprocmask(SIG_UNBLOCK, &oldsig, NULL);
  152. }

  153. void read_write_file(const char * pathname,const char * printinfo){
  154.     int buffer = 0 ,buffer1 = 0 ;
  155.     ssize_t errorno = 0;
  156.     int fd1 = 0;
  157.     fd1 = open(pathname,O_RDWR);
  158.     if (0 > fd1) {
  159.         printf("open error [%d]\n",fd1);
  160.         _exit(121);
  161.     }
  162.     if( 0 > (errorno = read(fd1,&buffer1,sizeof(int)) )){
  163.         printf("read error [%zd]\n",errorno);
  164.         _exit(122);
  165.     }
  166.     
  167.     close(fd1);
  168.     printf("%-10s+read:%-10d,pid:%-10d\n",printinfo,buffer1,getpid());
  169.     buffer = buffer1 + 1;
  170.     fd1 = open(pathname,O_RDWR);
  171.     if (0 > fd1) {
  172.         printf("open error [%d]\n",fd1);
  173.         _exit(123);
  174.     }
  175.     if( 0 > (errorno = write(fd1,&buffer,sizeof(int)) )){
  176.         printf("write error [%zd]\n",errorno);
  177.         _exit(124);
  178.     }
  179.     close(fd1);


  180. }
  181. int practice10_6(){
  182.     
  183.     
  184.     pid_t pid;
  185.     int fd = open("/Users/panghongjiang/123.txt",O_RDWR);
  186.     int i = 10;
  187.     int buffer = 0;
  188.     printf("fd:[%d]\n",fd);
  189.     printf("write:%zd\n",write(fd,&buffer, sizeof(int)));
  190.     close(fd);
  191.     
  192.     
  193.     
  194.     
  195.     initset();
  196.     
  197.     if (0 > (pid = fork())) {
  198.         printf("fork error\n");
  199.     }else if ( 0 == pid){
  200.         tell_practice10(getppid());
  201.         while (i--) {
  202.             wait_practice10();
  203.             read_write_file("/Users/panghongjiang/123.txt","child");
  204.             tell_practice10(getppid());
  205.         }
  206.         _exit(0);
  207.     }
  208.     while (i--) {
  209.         wait_practice10();
  210.         read_write_file("/Users/panghongjiang/123.txt","fahter");
  211.         sleep(1);
  212.         tell_practice10(pid);
  213.     }
  214.     return 0;
  215. }

  216. #define SIGNAME_MAX 7
  217. static const char * sigstr[] = {
  218. "NONE",
  219. "HUP",
  220. "INT",
  221. "QUIT",
  222. "ILL",
  223. "TRAP",
  224. "ABRT",
  225. "NONE",
  226. "FPE",
  227. "KILL",
  228. "BUS",
  229. "SEGV",
  230. "SYS",
  231. "PIPE",
  232. "ALRM",
  233. "TERM",
  234. "URG",
  235. "STOP",
  236. "TSTP",
  237. "CONT",
  238. "CHLD",
  239. "TTIN",
  240. "TTOU",
  241. "IO",
  242. "XCPU",
  243. "XFSZ",
  244. "VTALRM",
  245. "PROF",
  246. "WINCH",
  247. "INFO",
  248. "USR1",
  249. "USR2"};

  250. #define SIGMAX 32
  251. int str2sig(const char * signame,int * signo) {
  252.     if (signame) {
  253.         int i = 0;
  254.         size_t len = strlen(signame);
  255.         if (len < SIGNAME_MAX ) {
  256.             for (i = 1; i < SIGMAX; ++i) {
  257.                 if (0 == strncmp(signame, sigstr[i], SIGNAME_MAX)) {
  258.                     *signo = i;
  259.                     return 0;
  260.                 }
  261.             }
  262.             //没有找到
  263.             *signo = 0;
  264.         }
  265.     }
  266.     //error
  267.     return -1;
  268. }

  269. //课后习题10.2
  270. void practice10_2(){
  271.     int i = 0 , signo;
  272.     for(i = 0 ; i < 32 ; i++){
  273.         printf("%7s:",sigstr[i]);
  274.         str2sig(sigstr[i],&signo);
  275.         printf("[%d]\n",signo);
  276.     }
  277.     
  278. }

阅读(1933) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~