linux多线程 & IPC【11】《unix网络编程》的系统日志demo

来源:互联网 发布:淘宝推广员业绩不到 编辑:程序博客网 时间:2024/06/06 13:55

    上一篇自己写的基于共享内存的系统日志服务,有一个缺点就是槽满之后,客户端就阻塞在那里了。当然对于客户端来说,要紧的是去干正事,不是为了打印一行log神经病一样阻塞在这里。所以阻塞不是好的解决方案,应该:

【1】分配较大的NMSG

【2】服务器响应及时,这样即使消息多,也能很快处理完,不至于积累下来。

【3】在槽满时,再有消息来的话,给出overflow

    书本上就是用了overflow,这部分还没有细看。

服务器端:s.c



#include "a.h"int main(int argc, char *argv[]){        int fd, index, lastnoverflow, temp;    long offset;    struct shmst *ptr;    if(argc!=2)    {        printf("Usage: ser <name>");        exit(2);    }        shm_unlink(argv[1]);    fd=shm_open(argv[1], O_RDWR|O_CREAT|O_EXCL,S_IRUSR | S_IWUSR);    ptr=mmap(NULL,sizeof(struct shmst),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);    ftruncate(fd,sizeof(struct shmst));    close(fd);        for(index=0;index<NMSG;index++)    {        ptr->msgoff[index]=index*MSGSIZE;    }        sem_init(&ptr->mutex,1,1);    sem_init(&ptr->nempty,1,NMSG);    sem_init(&ptr->nstored,1,0);    sem_init(&ptr->noverflowmutex,1,1);        index=0;    lastnoverflow=0;        while(1)    {        sem_wait(&ptr->nstored);        sem_wait(&ptr->mutex);        offset=ptr->msgoff[index];        printf("index= %d :%s\n", index, &ptr->msgdata[offset]);        if(++index>=NMSG)        index=0;                sem_post(&ptr->mutex);        sem_post(&ptr->nempty);                sem_wait(&ptr->noverflowmutex);        temp=ptr->noverflow;        sem_post(&ptr->noverflowmutex);        if(temp!=lastnoverflow)        {            printf("noverflow = %d\n",temp);            lastnoverflow=temp;        }    }        exit(0);}

客户端:

#include "a.h"int main(int argc, char *argv[]){        int fd,i,nloop=50,nusec=0;    pid_t pid;    char msg[MSGSIZE];    long offset;    struct shmst *ptr;        if(argc!=2)    {        printf("Usage: cc <name>\n");        exit(2);    }    fd=shm_open(argv[1], O_RDWR, S_IRUSR | S_IWUSR);    ptr=mmap(NULL,sizeof(struct shmst),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);    close(fd);    pid=getpid();    for(i=0;i<nloop;i++)    {        usleep(100);        snprintf(msg,MSGSIZE,"pid %d: msg %d", (long)pid, i);                if(sem_trywait(&ptr->nempty)==-1)        {            if(errno==EAGAIN)            {                sem_wait(&ptr->noverflowmutex);                ptr->noverflow++;                sem_post(&ptr->noverflowmutex);                continue;            }            else            {                printf("sem_trywait error");            }        }        sem_wait(&ptr->mutex);        offset=ptr->msgoff[ptr->nput];        if(++(ptr->nput)>=NMSG)            ptr->nput=0;        sem_post(&ptr->mutex);        strcpy(&ptr->msgdata[offset],msg);        sem_post(&ptr->nstored);    }        exit(0);}

头文件:

#ifndef _A_H_#define _A_H_#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/mman.h>#include <semaphore.h> #define MSGSIZE 256 #define NMSG 16 #define FILE_MODE 777  struct shmst {    sem_t mutex;    sem_t nempty;    sem_t nstored;    int nput;    long noverflow;    sem_t noverflowmutex;    long msgoff[NMSG];    char msgdata[NMSG * MSGSIZE]; };  #endif 

编译脚本:

#!/bin/shgcc c.c -o cc -lrt -g -lpthreadgcc s.c -o ss -lrt -g -lpthread

运行:

administrator@ubuntu:~/test/po$ ./ss name &[2] 3821administrator@ubuntu:~/test/po$ jobs[1]-  运行中               ./log_server &  (工作目录:~/test/log_test)[2]+  运行中               ./ss name &administrator@ubuntu:~/test/po$ ./cc nameindex= 0 :pid 3822: msg 0index= 1 :pid 3822: msg 1index= 2 :pid 3822: msg 2index= 3 :pid 3822: msg 3index= 4 :pid 3822: msg 4index= 5 :pid 3822: msg 5index= 6 :pid 3822: msg 6index= 7 :pid 3822: msg 7index= 8 :pid 3822: msg 8index= 9 :pid 3822: msg 9index= 10 :pid 3822: msg 10index= 11 :pid 3822: msg 11index= 12 :pid 3822: msg 12index= 13 :pid 3822: msg 13index= 14 :pid 3822: msg 14index= 15 :pid 3822: msg 15index= 0 :pid 3822: msg 16index= 1 :pid 3822: msg 17index= 2 :pid 3822: msg 18index= 3 :pid 3822: msg 19index= 4 :pid 3822: msg 20index= 5 :pid 3822: msg 21index= 6 :pid 3822: msg 22index= 7 :pid 3822: msg 23index= 8 :pid 3822: msg 24index= 9 :pid 3822: msg 25index= 10 :pid 3822: msg 26index= 11 :pid 3822: msg 27index= 12 :pid 3822: msg 28index= 13 :pid 3822: msg 29index= 14 :pid 3822: msg 30index= 15 :pid 3822: msg 31index= 0 :pid 3822: msg 32index= 1 :pid 3822: msg 33index= 2 :pid 3822: msg 34index= 3 :pid 3822: msg 35index= 4 :pid 3822: msg 36index= 5 :pid 3822: msg 37index= 6 :pid 3822: msg 38index= 7 :pid 3822: msg 39index= 8 :pid 3822: msg 40index= 9 :pid 3822: msg 41index= 10 :pid 3822: msg 42index= 11 :pid 3822: msg 43index= 12 :pid 3822: msg 44index= 13 :pid 3822: msg 45index= 14 :pid 3822: msg 46index= 15 :pid 3822: msg 47index= 0 :pid 3822: msg 48index= 1 :pid 3822: msg 49administrator@ubuntu:~/test/po$ 

下载:



原创粉丝点击