Archive for the ‘GNU/linux’ Category.

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

/***********************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);
}
}
}

相关日志

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

下面一切皆文件这个概念因该深入 程序员或者将要成为 程序员的人心中。所以在下面对文件的操作是很重要的,下面是我做的一个例程用的是标准 I/O
对配置文件的改写是写程序很重要的一点,但是改写一个文件的时候,我们没有办法像用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的功能*/
}

相关日志

时隔几个月,没想到我又用上linux了。。。

下面写东西总是感觉很不错,带给我的是一种感觉,一种久违的宁静。在纸醉金迷的widows下面,让我这个不自觉的人变的晕头转向。
在2个月前,我去了一家用AutoCAD做电气的公司,完完全全的windows,心想这辈子成为历史,成为爱好了,可惜我连真正的 的真谛都说不出。但是今天我有又装上了我的fedora ,我用fedora的历史可以追溯到fedora core2 ,那年我硬是花了50多买了套安装盘,当时整夜不睡的装系统,装好没有图形界面,不能挂载windows盘,更别说NTFS了。。。安静的夜 晚,美好的回忆。用过ubuntu,没什么感觉,讨厌橘红色,喜欢蓝色,这也许就是我用fedora的原因把, ubuntu给我的只是另类的windows,这样挺好,但是我不算喜欢。有心去尝尝拥有,自由 最高精神的debian,还有根据自己机器编译二进制文件的gentoo,还有Unix的freeBSD,当然少不了MacOS,有时间把。。我还是喜欢 fedora
蓝色的宁静,希望能给我无限的“自由”。

相关日志

哎,操作系统这东西啊

最近不知道windows怎么了,开机超慢。也真是懒的重装。。于是就装了个fedora 7至于问我为什么不装ubuntu或者SUSE呢,我只能说这是习惯问题。。这段时间看来ubuntu在方面确实是出尽风头了,关键在于他像 windows一样好用,(可能对我来说吧),这样以来没什么兴趣了。。最重要的原因就是ubuntu的开发库不是很全(c/c++的)python的还 可以把。至于SUSE么和windows有染就不想说什么了,不过NOVELL也算办了件好事他有Uinx的版权,这样就不会牵扯侵Uinx的 权了(毕竟一个公司不会和自己过不去把)。 反正就是喜欢redhat没办法。。。我就用fedora7 ubuntu能做到的fedora一样能做到的,而且会更好的。
大家看看我的beryl的桌面把,这可不是ubuntu的专利阿!

可惜了动态效果看不见了。。。反正比windows强了。。呵呵我的笔记本是跑不起来vsita所以就不想了。不过这里什么都有阿 什么qq阿 gtalk阿 msn阿 ps阿等等吧 都有代替的软件 感觉很不错阿 。fedora 7 moonshine 谁用谁知道!

相关日志