作者在 2010-10-13 11:48:07 发布以下内容
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/sysmacros.h>
#include<unistd.h>
/*
* As for the fifo we can creat it use both
* mkfifo(const char *pathname,mode_t mode);
* and
* mknod(); to realize it
*/
void usage(void)
{
fprintf(stderr,"usage: mknod <path> [b|c|u|p]\n ","<major> <minor>\n");
exit(1);
}
int main(int argc,const char **argv)
{
int major=0,minor=0;
const char *path;
int mode=0666;
char *end;
int args;
if(argc<3)
usage();
path=argv[1];
if(!strcmp(argv[2],"b"))
{
mode|=S_IFBLK;
args=5;
}
else if(!strcmp(argv[2],"p"))
{
mode|=S_IFIFO; /*************if this file is a named pipe********/
args=3;
}
else
{
fprintf(stderr,"unknown node type %s\n",argv[2]);
return 1;
}
if(args==5)
{
major=strtol(argv[3],&end,0);
if(*end)
{
fprintf(stderr,"bad major number %s\n",argv[3]);
return 1;
}
minor=strtol(argv[4],&end,0);
if(*end)
{
fprintf(stderr,"bad minor number %s\n",argv[4]);
return 1;
}
}
if(mknod(path,mode,makedev(major,minor)))
{
fprintf(stderr,"mknod failed: %s \n",strerror(errno));
return 1;
}
return 0;
}
/*程序2.命名管道的测试通过上面的程序我们已经建立了一个tes 的有名管道*/
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main(void)
{
int fd;
char buf[100];
fd=open("/home/program/mknod/tes",O_RDWR);
if(fd<0)
{
perror("Open");
exit(1);
}
if(write(fd,"Hello world!\n",13)!=13)
{
perror("write");
exit(1);
}
if(read(fd,buf,13)!=13)
{
perror("read");
exit(1);
}
printf("%s",buf);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/sysmacros.h>
#include<unistd.h>
/*
* As for the fifo we can creat it use both
* mkfifo(const char *pathname,mode_t mode);
* and
* mknod(); to realize it
*/
void usage(void)
{
fprintf(stderr,"usage: mknod <path> [b|c|u|p]\n ","<major> <minor>\n");
exit(1);
}
int main(int argc,const char **argv)
{
int major=0,minor=0;
const char *path;
int mode=0666;
char *end;
int args;
if(argc<3)
usage();
path=argv[1];
if(!strcmp(argv[2],"b"))
{
mode|=S_IFBLK;
args=5;
}
else if(!strcmp(argv[2],"p"))
{
mode|=S_IFIFO; /*************if this file is a named pipe********/
args=3;
}
else
{
fprintf(stderr,"unknown node type %s\n",argv[2]);
return 1;
}
if(args==5)
{
major=strtol(argv[3],&end,0);
if(*end)
{
fprintf(stderr,"bad major number %s\n",argv[3]);
return 1;
}
minor=strtol(argv[4],&end,0);
if(*end)
{
fprintf(stderr,"bad minor number %s\n",argv[4]);
return 1;
}
}
if(mknod(path,mode,makedev(major,minor)))
{
fprintf(stderr,"mknod failed: %s \n",strerror(errno));
return 1;
}
return 0;
}
/*程序2.命名管道的测试通过上面的程序我们已经建立了一个tes 的有名管道*/
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main(void)
{
int fd;
char buf[100];
fd=open("/home/program/mknod/tes",O_RDWR);
if(fd<0)
{
perror("Open");
exit(1);
}
if(write(fd,"Hello world!\n",13)!=13)
{
perror("write");
exit(1);
}
if(read(fd,buf,13)!=13)
{
perror("read");
exit(1);
}
printf("%s",buf);
return 0;
}