📄 server.c
字号:
#include <stddef.h>#include <signal.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <unistd.h>#include <string.h>#include "../include/structs.h"#include "../include/stddef.h"#include "../include/tools.h"#define SHM_MODE (SHM_R | SHM_W) void alloc_buss(int,siginfo_t*,void*);//void alloc_buss(int);void save();void trans();void save(int);void trans_(int);pid_t savepid;pid_t transpid;USER *user;int main(int argc , char **argv){ key_t key; int shmid; pid_t pid; SHM *shm; pid_t serv_pid; if(pid = fork() != 0){ printf("main process exit...\n"); exit(1); } if(setsid() < 0){ printf("new session error !\n"); printf("in daemon's father's father , getpid() : %d\n" , getpid()); exit(1); } if(pid = fork() != 0){ printf("sub process exit....\n"); printf("in daemon's father , getpid() : %d\n" , getpid()); exit(1); } /************************* DEAMON **************************/ int sg = 0; //for(;sg <= 63;sg ++){ // signal(sg , SIG_IGN); //} signal(SIGTTOU,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGTSTP,SIG_IGN); signal(SIGHUP ,SIG_IGN); //signal(SIGRQST , alloc_buss); struct sigaction act; act.sa_flags=SA_SIGINFO; act.sa_sigaction=alloc_buss; if(sigaction(SIGRQST,&act,NULL)<0) printf("sigaction wrong\n"); //close(0); close(1); close(2); key = ftok("/root/test.c" , 0); if( ( shmid = shmget(key , SHM_SIZE , 0600|IPC_CREAT) ) == -1 ){ printf("SHM create error !\n"); exit(1); } // inital to 0 void *p; void *tmp; shm = shmat(shmid , NULL , 0); printf("in server , shm : %p\n" ,shm); if(!p){ printf("shmat error !\n"); shmctl(shmid , IPC_RMID , NULL); exit(1); } tmp = p;// memset(p , 0 , SHM_SIZE);/* p += 2*sizeof(int); while(1){ if(p - tmp >= SHM_SIZE)break; ((SHM *)p)->spid = getpid(); printf("shm->spid : %d int server....\n" , getpid()); printf("address of p : %p\n" , p); p += (sizeof(SHM) + sizeof(int)); }*/ shm->flag = 0; int index; for(index = 0;index < MAX_CLIENT;index ++){ (shm->swap_arr)[index].swap.spid = getpid(); } shmdt(shm); while(1){ pause(); }};void alloc_buss(int signum,siginfo_t *info,void *myact){ if(fork()==0){ save(info->si_int); exit(1); }else{ return; }};void save(int index){ int shmid; key_t key; SHM *shm; key = ftok("/root/test.c" , 0); shmid = shmget(key , SHM_SIZE , 0600); shm = (SHM *)shmat(shmid , NULL , 0); /************/ // here to process bussniss // access SHM to get data , and write the result to SHM /************/ (shm->swap_arr)[index].swap.ok = 1; //kill((shm->swap_arr)[index].swap.cpid,SIGRSPN); exit(1);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -