?? action.c~
字號:
#include "server.h"/* 開燈 */int turn_on(int led_num) { int res; char *history_str; time_t timeval; LOG_REC log_record; HIST_REC hist_record; FILE *log_fp; FILE *hist_fp; void *shared_memory_log=(void *)0; int shmid_log; int *log_writing; void *shared_memory_hist=(void *)0; int shmid_hist; int *hist_writing; /* shared memory for log */ shmid_log=shmget((key_t)1234,sizeof(int),0666 | IPC_CREAT); if(shmid_log==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_log=shmat(shmid_log,(void *)0,0); if(shared_memory_log==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } log_writing=(int *)shared_memory_log; /* shared memory for hist */ shmid_hist=shmget((key_t)1235,sizeof(int),0666 | IPC_CREAT); if(shmid_hist==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_hist=shmat(shmid_hist,(void *)0,0); if(shared_memory_hist==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } hist_writing=(int *)shared_memory_hist; /* check the status of led */ res=led_status(led_num); /* system time */ (void)time(&timeval); if(res==LED_TRUE) { //當前燈已亮 /* log struct */ memset(&log_record,'\0',sizeof(LOG_REC)); log_record.led_no=led_num; sprintf(log_record.led_log,"led_%d has already been turned on, date is %s\n",led_num,ctime(&timeval)); printf("led_no1 %d\n",log_record.led_no); printf("led_log1 %s\n",log_record.led_log); /* hist struct*/ memset(&hist_record,'\0',sizeof(HIST_REC)); sprintf(hist_record.op_content,"User trys to turn on led_%d which has already been turned on, date is %s\n",led_num,ctime(&timeval)); } else if(res==LED_FALSE) { /*turn on the led light */ res=led_on(led_num); if(res==-1) { perror("turn on failed\n"); exit(EXIT_FAILURE); } /* log struct */ memset(&log_record,'\0',sizeof(LOG_REC)); log_record.led_no=led_num; sprintf(log_record.led_log,"led_%d has been turned on, date is %s\n",led_num,ctime(&timeval)); printf("led_no1 %d\n",log_record.led_no); printf("led_log1 %s\n",log_record.led_log); /* hist struct*/ memset(&hist_record,'\0',sizeof(HIST_REC)); sprintf(hist_record.op_content,"User turns on led_%d, date is %s\n",led_num,ctime(&timeval)); } /* 寫入日志文件 */ while(*log_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *log_writing=1;//表明當前進程要對文件進行讀寫 log_fp=fopen("led.log","a+"); if(log_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } fwrite(&log_record,sizeof(LOG_REC),1,log_fp); fclose(log_fp); *log_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_log)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } /* 寫入歷史文件 */ while(*hist_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *hist_writing=1;//表明當前進程要對文件進行讀寫 hist_fp=fopen("operation.hist","a+"); if(hist_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } fwrite(&hist_record,sizeof(HIST_REC),1,hist_fp); fclose(hist_fp); *hist_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_hist)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } return 1;}int turn_off(int led_num) { int res; char *history_str; time_t timeval; LOG_REC log_record; HIST_REC hist_record; FILE *log_fp; FILE *hist_fp; void *shared_memory_log=(void *)0; int shmid_log; int *log_writing; void *shared_memory_hist=(void *)0; int shmid_hist; int *hist_writing; /* shared memory for log */ shmid_log=shmget((key_t)1234,sizeof(int),0666 | IPC_CREAT); if(shmid_log==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_log=shmat(shmid_log,(void *)0,0); if(shared_memory_log==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } log_writing=(int *)shared_memory_log; /* shared memory for hist */ shmid_hist=shmget((key_t)1235,sizeof(int),0666 | IPC_CREAT); if(shmid_hist==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_hist=shmat(shmid_hist,(void *)0,0); if(shared_memory_hist==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } hist_writing=(int *)shared_memory_hist; /* check the status of led */ res=led_status(led_num); /* system time */ (void)time(&timeval); if(res==LED_FALSE) { //當前燈已滅 /* log struct */ memset(&log_record,'\0',sizeof(LOG_REC)); log_record.led_no=led_num; sprintf(log_record.led_log,"led_%d has already been turned off, date is %s\n",led_num,ctime(&timeval)); /* hist struct*/ memset(&hist_record,'\0',sizeof(HIST_REC)); sprintf(hist_record.op_content,"User trys to turn off led_%d which has already been turned off, date is %s\n",led_num,ctime(&timeval)); } else if(res==LED_TRUE) { /*turn on the led light */ res=led_off(led_num); if(res==-1) { perror("turn off failed\n"); exit(EXIT_FAILURE); } /* log struct */ memset(&log_record,'\0',sizeof(LOG_REC)); log_record.led_no=led_num; sprintf(log_record.led_log,"led_%d has been turned off, date is %s\n",led_num,ctime(&timeval)); /* hist struct*/ memset(&hist_record,'\0',sizeof(HIST_REC)); sprintf(hist_record.op_content,"User turns off led_%d, date is %s\n",led_num,ctime(&timeval)); } /* 寫入日志文件 */ while(*log_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *log_writing=1;//表明當前進程要對文件進行讀寫 log_fp=fopen("led.log","a+"); if(log_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } fwrite(&log_record,sizeof(LOG_REC),1,log_fp); fclose(log_fp); *log_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_log)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } /* 寫入歷史文件 */ while(*hist_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *hist_writing=1;//表明當前進程要對文件進行讀寫 hist_fp=fopen("operation.hist","a+"); if(hist_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } fwrite(&hist_record,sizeof(HIST_REC),1,hist_fp); fclose(hist_fp); *hist_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_hist)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } return 1;}int get_status(int *status_res) { int i=0; int res; for(i=0;i<LED_NUM;i++) { res=led_status(i+1); if(res==LED_TRUE) { status_res[i]=1; } else { status_res[i]=0; } } return 1;}int get_history(char *text_content) { int i=0; FILE *hist_fp; HIST_REC hist_record; void *shared_memory_hist=(void *)0; int shmid_hist; int *hist_writing; /* shared memory for hist */ shmid_hist=shmget((key_t)1235,sizeof(int),0666 | IPC_CREAT); if(shmid_hist==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_hist=shmat(shmid_hist,(void *)0,0); if(shared_memory_hist==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } hist_writing=(int *)shared_memory_hist; /* 讀取歷史文件 */ while(*hist_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *hist_writing=1;//表明當前進程要對文件進行讀寫 hist_fp=fopen("operation.hist","r"); if(hist_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } memset(text_content,'\0',TEXT_SIZE); while(feof(hist_fp)!=1) { fseek(hist_fp,i*sizeof(HIST_REC),SEEK_SET); fread(&hist_record,sizeof(HIST_REC),1,hist_fp); strcat(text_content,hist_record.op_content); i++; fseek(hist_fp,i*sizeof(HIST_REC),SEEK_SET); fread(&hist_record,sizeof(HIST_REC),1,hist_fp); } fclose(hist_fp); *hist_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_hist)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } return 1;}int get_log(char *text_content) { int i=0; FILE *log_fp; LOG_REC log_record; void *shared_memory_log=(void *)0; int shmid_log; int *log_writing; /* shared memory for hist */ shmid_log=shmget((key_t)1234,sizeof(int),0666 | IPC_CREAT); if(shmid_log==-1) { perror("shmget failed\n"); exit(EXIT_FAILURE); } shared_memory_log=shmat(shmid_log,(void *)0,0); if(shared_memory_log==(void *)-1) { perror("shmat failed\n"); exit(EXIT_FAILURE); } log_writing=(int *)shared_memory_log; /* 讀取歷史文件 */ while(*log_writing==1) { //說明當前有進程正對文件進行讀寫 sleep(1); } /* critical area begin */ *log_writing=1;//表明當前進程要對文件進行讀寫 log_fp=fopen("led.log","r"); if(log_fp==NULL) { perror("open log file failed\n"); exit(EXIT_FAILURE); } memset(text_content,'\0',TEXT_SIZE); while(feof(log_fp)!=1) { fseek(log_fp,i*sizeof(LOG_REC),SEEK_SET); fread(&log_record,sizeof(LOG_REC),1,log_fp); strcat(text_content,log_record.led_log); i++; fseek(log_fp,i*sizeof(LOG_REC),SEEK_SET); fread(&log_record,sizeof(LOG_REC),1,log_fp); } fclose(log_fp); *log_writing=0;//表明當前進程對文件讀寫完畢 /* critival area end */ if(shmdt(shared_memory_log)==-1) { fprintf(stderr,"shmdt falied\n"); exit(EXIT_FAILURE); } return 1;}int command(char *params) { char num=0; while(*params!='\0') { switch(*params) { case '1': num=1; break; case '2': num=2; break; case '3': num=3; break; case '4': num=4; break; case '5': num=5; break; case '6': num=6; break; case '7': num=7; break; case '8': num=8; break; } turn_on(num); params++; } return 1;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -