Ejemplo n.º 1
0
static int
proc_usage(pid_t pid, prusage_t *pup, int *perr)
{
	int fd;

	*perr = G_NOPROC;

	if ((fd = open_usage(pid, perr)) != -1) {
		if (read(fd, pup, sizeof (prusage_t)) == sizeof (prusage_t)) {
			*perr = 0;
			(void) close(fd);
			return (0);
		}

		/*
		 * If the read failed, the process may have gone away.
		 */
		(void) close(fd);
	}
	return (-1);
}
Ejemplo n.º 2
0
int main( int argc, char *argv[] ){
	int err;
	int c;
	int ret;
	int fd;
	while(-1 != (ret = getopt(argc,argv,"r:h:")) ){
		switch(ret) {
			case 'r' :
				printf("The argument of -b is %s\n",optarg);
	
				if( strcmp("open",optarg) == 0 )   c = '1';  else
				if( strcmp("creat",optarg) == 0 )  c = '2';  else
				if( strcmp("close",optarg) == 0 )  c = '3';  else
				if( strcmp("lseek",optarg) == 0 )  c = '4';  else
				if( strcmp("read",optarg) == 0 )   c = '5';  else
				if( strcmp("write",optarg) == 0 )  c = '6';  else
				if( strcmp("fcntl",optarg) == 0 )  c = '7';  else
				if((strcmp("sync",optarg) & strcmp("fsync",optarg) & strcmp("fdatasync",optarg)) == 0) c = '8';
				

				switch(c) {
					case '1' : //r:open
						printf("R:open\n");
						fd = open("./open.txt",O_RDWR | O_CREAT | O_EXCL, S_IRWXU); //打开文件,不存在创建,设置权限 
						if( fd == -1) printf("cant open open.txt\n");
						write(fd,"open",4); //写open到文件
						close(fd); //关闭文件
						break;
					case '2' : //r:creat
						printf("R:create\n");
						err = creat("./create.txt",0777);
						if( err == -1 ) printf("cant create create.txt");
						fd = open("./create.txt",O_WRONLY);
						write(fd,"create",6);	
						close(fd);
						break;
					case '3' : //r:close
						printf("R:close\n");
						fd = open("./close.txt",O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
						write(fd,"close",5);
						close(fd);
						break;
					case '4' : //r:lseek
						printf("R:lseek\n");
						fd = open("./lseek.txt",O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
						write(fd,"lseek",5);
						close(fd);
						fd = open("./lseek.txt",O_RDWR);
						ret = lseek(fd,-4,SEEK_END);
						write(fd,"lseek",5);
						close(fd);
						break;
					case '5' : //r:read
						printf("R:read\n");
						char buf[10];
						fd = open("./read.txt",O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
						write(fd,"read",4);
						close(fd);
						fd = open("./read.txt",O_RDWR);
						ret = lseek(fd, -2, SEEK_END);
						read(fd,buf,10);
						printf("read from read.txt is %s\n",buf);
						close(fd);
						break;
					case '6' : //r:write
						printf("R:write\n");
						fd = open("./write.txt",O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
						write(fd,"write",5);
						close(fd);
						break;
					case '7' : //r:fcntl
						//fd = open("./fcntl.txt",O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
						//ret= fcntl(fd,F_GETFD); printf("current descriptor flags: %d\n",ret);
						//ret= fcntl(fd,F_SETFD,1);
						
						break;
					case '8' :

						break;
					default :
						printf("R:err::the argumetn is not included in {open,creat,close,lseek,read,write}\n");
						break;
					
				}//endl-switch(optarg)				
				break;

			case 'h' :
				printf("The argument of -b is %s\n\n",optarg);

				if( strcmp("open",optarg) == 0 )   c = '1';  else
				if( strcmp("creat",optarg) == 0 )  c = '2';  else
				if( strcmp("close",optarg) == 0 )  c = '3';  else
				if( strcmp("lseek",optarg) == 0 )  c = '4';  else
				if( strcmp("read",optarg) == 0 )   c = '5';  else
				if( strcmp("write",optarg) == 0 )  c = '6';  else
				if( strcmp("fcntl",optarg) == 0 )  c = '7';  else
				if((strcmp("sync",optarg) & strcmp("fsync",optarg) & strcmp("fdatasync",optarg)) == 0) c = '8';

				switch(c) {
					case '1' : //h:open
						open_usage();
						break;
					case '2' : //h:creat
						creat_usage();
						break;
					case '3' : //h:close
						close_usage();
						break;
					case '4' : //h:lseek
						lseek_usage();
						break;
					case '5' : //h:read
						read_usage();
						break;
					case '6' : //h:write
						write_usage();
						break;
					case '7' : //h:fcntl
						fcntl_usage();
						break;
					case '8' : //h:sync
						sync_usage();
						break;
					default : //-h 
						fileIO_usage();
						break;
				}			
				break;

			default: //-h
				fileIO_usage();
				break;
		} //endl-switch(ret)
		

	} //endl-while(getopt)

}//endl-main