11 2008

火炬专递竟然从我们寝室门口过。。。。

cyher

火炬专递竟然从我们寝室门口过。。。。

2008-08-11 09:11

早上起来超级吵,出去一看,我靠天上飞这直升机。。。。
我出去一看就是着一个状态。。。。。。。。。。。。。。。。。。。。。。。
结果也没看见火炬,。。。。




相关日志


7 2008

linux环境编程之进程间通信(消息队列)

cyher

/**************************************************************
* name : server.c                          *
* author : cyher                          *
* date : 2008-8-6                          *
* description : 服务端进程向消息队列里面放消息传给客户端     *
**************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <time.h>

struct msgbuf/*消息队列结构*/
{
long mtype;
char mtext[256];
};

int
init_daemon(void);/*守护进程启动函数*/

int
main()
{
key_t key;
int msgid;
char *p;
time_t t;
struct msgbuf msg= {100,”"};/*设置消息的id为100*/
key = ftok(“/home/cyher/workspace/c/star”,’s’);/*产生一个key*/
msgid = msgget(key,IPC_CREAT | 0666);/*产生一个ipc标识*/
if(msgid == -1)
{
perror(“msgget”);
exit(1);
}

init_daemon();/*守护进程创建*/
while(1)/*进入循环执行*/
{
t = time(NULL);
p = asctime(localtime(&t));
strcpy(msg.mtext,p);/*把时间字符放入msg.mtext*/
msgsnd(msgid,&msg,strlen(msg.mtext)+1,IPC_NOWAIT);/*放入消息队列无阻塞*/
sleep(2);
}

}

/**************************************************************
* name : client.c                          *
* author : cyher                          *
* date : 2008-8-6                          *
* description : 客户端进程从消息队列里面取消息打印到屏幕上   *
**************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <time.h>

struct msgbuf
{
long mtype;
char mtext[256];
};

int
main()
{
key_t key;
int msgid;
struct msgbuf msg;
key = ftok(“/home/cyher/workspace/c/star”,’s’);
msgid = msgget(key,IPC_CREAT | 0666);
if(msgid == -1)
{
perror(“msgget”);
exit(1);
}

while(1)
{
if(msgrcv(msgid,&msg,256,100,IPC_NOWAIT) == -1)
{
perror(“msgrcv:”);
}
printf(“%s”,msg.mtext);
memset(&msg,0,sizeof(struct msgbuf));
sleep(2);
}

}

/*******************************
* name:init_daemon.c
* author:cyher
* date:2008-8-1
* description:initialize a daemon process
* */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

int
init_daemon()
{
int pid;
int i;
if( (pid=fork())<0 )
{
perror(“fork”);
exit(1);
}
else if(pid > 0) //是父进程就退出
{
exit(0);
}

if((pid=fork()) < 0)
{
perror(“fork2″);
exit(1);
}
else if (pid > 0)//是第一子进程也退出
{
exit(0);
}
//
//下面都是第二子进程运行
sleep(2);
setsid();//使子进程脱离父进程的关系,把父进程的信息改掉
//脱离终端的控制
//
for(i=0;i<1024;i++)//关闭文件描述符,节省资源
close(i);
chdir(“/tmp”);//改变工作目录
umask(0);//重设掩码,使程序工作顺畅

return 0;

}

相关日志


7 2008

linux环境编程进程间通信(共享内存)

cyher

/*****************************************
* name : shm_a.c
* author : cyher
* date : 2008-8-6
* description : 进程a,共享内存通信
*****************************************/
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int
main()
{
int oflag,id;
char *ptr;
char buf[512];
size_t length=512;
time_t t;

oflag = 0644|IPC_CREAT;
id = shmget(ftok(“/home/cyher/workspace/c/star”,89),length,oflag);
if((ptr = shmat(id,NULL,0)) == (void *) -1)
{
perror(“shmat”);
exit(1);

}
while(1)
{
t=time(NULL);
memset(buf,0,512);
memcpy(buf,ptr,512);
printf(“%s”,buf);
sprintf(ptr,”time: %s pid: [%d]\n”,asctime(localtime(&t)),getpid());
sleep(5);
}
}

/*****************************************
* name : shm_b.c
* author : cyher
* date : 2008-8-6
* description : 进程b,共享内存通信
*****************************************/

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int
main()
{
int oflag,id;
char *ptr;
char buf[512];
size_t length=512;
time_t t;

oflag = 0644|IPC_CREAT;
id = shmget(ftok(“/home/cyher/workspace/c/star”,89),length,oflag);
if((ptr = shmat(id,NULL,0)) == (void *) -1)
{
perror(“shmat”);
exit(1);

}
while(1)
{
sleep(5);
t=time(NULL);
memset(buf,0,512);
memcpy(buf,ptr,512);
printf(“%s”,buf);
sprintf(ptr,”time: %s pid: [%d]\n”,asctime(localtime(&t)),getpid());
}
}

相关日志


6 2008

linux环境编程之进程间通信(pipe & FIFO)

cyher

/***********************rw.h*******************/
#include <unistd.h>

int Read(int fd, void *buf,size_t count)
{
void *p =buf;
int totle = count;
int current = 0;
int iRet = 0;

while(current < totle)
{
iRet = read(fd,p + current,totle – current);
if(iRet < 0)
{
perror(“Read Error”);
return -1;
}
else if(iRet == 0)
{
return current;

}
else
{
current += iRet;
}
}
return current;

}

int Write(int fd,void *buf,size_t count)

{
void *p = buf;
int totle = count;
int current = 0;
int iRet = 0;

while(current < totle)
{
iRet = write(fd,p + current,totle – current);
if(iRet < 0)
{
perror(“Write Error”);
return -1;
}
else if(iRet == 0)
{
return current;
}
else
{
current += iRet;
}
}
return current;
}

/********************************************************
* name : pipe_print.c
* author : cyher
* date :2008-8-5
* descirption : 两个进程,父进程和子进程。
*               建立pipe父进程读取文件名给子进程读文件给父进程。
*               父进程打印在屏幕上。
* ********************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include “rw.h”//读管道的时候需要用这个自己写的read循环读,否则一次可能读不全
#define MAX 1024
int main (int argc,char *argv[])
{
int fd1[2];
int fd2[2];
int pipe1,pipe2;
size_t n=1;
pid_t pid;
char buf1[MAX] = {0};
char buf2[MAX] = {0};
char buf3[MAX] = {0};
int fd;
ssize_t read_num;
if(argc != 2)
{
printf(“usage:\”pipe_print name\n\”");
exit(0);
}
if((pipe1=pipe(fd1)) == -1 )//父向子传信息的管道
{
perror(“pipe1″);
exit(1);
}

if((pipe2=pipe(fd2)) == -1)//子向父传信息的管道
{
perror(“pipe2″);
exit(1);
}

if((pid=fork()) < 0)
{
perror(“fork”);
exit(1);
}
else if(pid == 0)//子进程
{
close(fd1[1]);/*关管道1写端*/
close(fd2[0]);/*关管道2读端*/
sleep(1);
if(read(fd1[0],buf2,MAX) == -1)/*读取父进程传来的文件名*/
{
exit(1);
}
if(( fd = open(buf2,O_RDONLY,0) ) < 0)/*打开文件*/
{
perror(“open error”);
exit(1);
}

while(Read(fd,buf1,MAX) > 0)/*读文件放入buf1*/
{
Write(fd2[1],buf1,MAX);/*写入管道2*/
memset(buf1,0,MAX);/*清空buf1以免出错*/
}
close(fd1[0]);
close(fd2[1]);
exit(0);
}
else//父进程
{

close(fd2[1]);/*关管道2的写端*/
close(fd1[0]);/*关管道1的读端*/
n=strlen(argv[1]);
if(Write(fd1[1],argv[1],n) == -1)/*把命令行参数发给管道1*/
{
exit(1);
}
while((read_num=Read(fd2[0],buf3,MAX)) > 0 )/*读取管道2中的信息放在buf3中*/
{
if(read_num == -1)
{
perror(“read”);
exit(1);
}

if(Write(1,buf3,MAX) < 0)/*把buf3中的信息写在表准输出上*/
{
perror(“write”);
exit(1);
}
memset(buf3,0,MAX);/*清空buf3以免出错*/
}
putchar(‘\n’);
close(fd1[1]);
close(fd2[0]);
if(waitpid(pid,NULL,0) == -1)/*父进程等待子进程关闭回收子进程资源*/
{
perror(“wait”);
exit(1);
}
}
}

/********************************************************
* name : fifo.c
* author : cyher
* date :2008-8-5
* descirption : 两个进程,父进程和子进程
*               建立FIFO父进程读取文件名传给子进程读取文件传回父进程
*               父进程再打印在屏幕上
* ********************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include “rw.h”
#define MAX 1024
int main (int argc,char *argv[])
{
int fd1,fd2,fd3,fd4;
int fifo,fifo2;
size_t n=1;
pid_t pid;
char buf1[MAX] = {0};
char buf2[MAX] = {0};
char buf3[MAX] = {0};
int fd;
ssize_t read_num;
if(argc != 2)
{
printf(“usage:\”pipe_print name\n\”");
exit(0);
}
if((fifo=mkfifo(“/tmp/cyher088″,0777)) == -1)
{
perror(“fifo”);
exit(1);

}
if((fifo2=mkfifo(“/tmp/cyher089″,0777)) == -1)
{
perror(“fifo”);
exit(1);

}
if((pid=fork()) < 0)
{
perror(“fork”);
exit(1);
}
else if(pid == 0)//子进程
{
if( ( fd1 = open(“/tmp/cyher088″,O_RDONLY,0666) ) == -1)
{
perror(“open1″);
exit(1);
}
sleep(1);
if(read(fd1,buf2,MAX) == -1)
{
perror(“read”);
exit(1);
}
if(( fd = open(buf2,O_RDONLY,0) ) < 0)
{
perror(“open error”);
exit(1);
}
if( ( fd4 = open(“/tmp/cyher089″,O_WRONLY ,0666) ) == -1)
{
perror(“open1″);
exit(1);
}

while(Read(fd,buf1,MAX) > 0)
{
Write(fd4,buf1,MAX);
memset(buf1,0,MAX);
}
close(fd1);
close(fd4);
exit(0);
}
else//父进程
{

if( ( fd3 = open(“/tmp/cyher088″,O_WRONLY ,0666) ) == -1)
{
perror(“open1″);
exit(1);
}
n=strlen(argv[1]);
if(Write(fd3,argv[1],n) == -1)
{
exit(1);
}
if( ( fd2 = open(“/tmp/cyher089″,O_RDONLY,0666) ) == -1)
{
perror(“open1″);
exit(1);
}
while((read_num=Read(fd2,buf3,MAX)) > 0 )
{
if(read_num == -1)
{
perror(“read”);
exit(1);
}

if(Write(1,buf3,MAX) < 0)
{
perror(“write”);
exit(1);
}
memset(buf3,0,MAX);
}
putchar(‘\n’);
close(fd3);
close(fd2);
if(waitpid(pid,NULL,0) == -1)
{
perror(“wait”);
exit(1);
}
}
}

相关日志


5 2008

linux环境编程之文件操作(标准I/O)

cyher

在linux下面一切皆文件这个概念因该深入linux 程序员或者将要成为linux 程序员的人心中。所以在linux下面对文件的操作是很重要的,下面是我做的一个例程用的是标准 I/O
对配置文件的改写是写linux程序很重要的一点,但是改写一个文件的时候,我们没有办法像用vi或者word那样直接插入就行了,因为,就算是word 或者vi他们都是把文件copy出来一个副本,然后你该然后再改名放回去,这基本上就是我理解的原理,所以代码也体现了这个原理,用标准I/O来做

/***********************************************/

/* name:changeini.c
* author:cyher
* date :2008-7-25
* description:change a line in ss.ini
* */
#include
#include
#include
#define MAX 100
int main(int argc,char *argv[])
{
char ch[MAX]={0};
char insert[]=”cgihome”;
char *p=ch;
FILE *fp1 = fopen(“ss.ini”,”r”);//直读打开ss.ini
if(fp1 == NULL)
{
printf(“er1″);
exit(1);
}
FILE *fp2 = fopen(“ss.ini.bak”,”wa+”);//截断打开ss.ini.bak
if(fp2 == NULL)
{
printf(“er2″);
exit(1);
}
/*
*从ss.ini中一行一行读数据判断是否是要插入的行的上一行
*/
while(fgets(p,MAX,fp1))
{
fputs(p,fp2);
fflush(NULL);
ch[7]=’\0′; //截断字符串,取前7个字符作为字符串
if(!strcmp(p,insert))//如果要找的字符串出现
{
fputs(“name = serverName\n”,fp2);//插入这句
}
}
fclose(fp1);
fclose(fp2);//关闭文件指针
/*这里应该有一个把ss.ini删去,把ss.ini.bak改名为ss.ini的功能*/
}

相关日志