pipe.c ( File view )
Description:linux 0.11 kernel source code, with English Notes, so that learning good things linux kernel...
- By 2010-08-31
- View(s):24
- Download(s):2
- Point(s): 1
/* * linux/fs/pipe.c * * (C) 1991 Linus Torvalds */ #include// źͷļźŷųźŽṹԼźŲԭ͡ #include // ȳͷļṹtask_structʼ0 ݣ // һЩйúͻȡǶʽຯ䡣 #include /* for get_free_page */ /* ʹеget_free_page */ // ڴͷļҳСһЩҳͷźԭ͡ #include // βͷļйضμĴǶʽຯ //// ܵ // inode ǹܵӦi ڵ㣬buf ݻָ룬count Ƕȡֽ int read_pipe (struct m_inode *inode, char *buf, int count) { int chars, size, read = 0; // ȡֽڼֵcount 0ѭִ² while (count > 0) { // ǰܵû(size=0)ѵȴýڵḶ̌ûдܵߣѶ // ֽ˳ڸi ڵ˯ߣȴϢ while (!(size = PIPE_SIZE (*inode))) { wake_up (&inode->i_wait); ܵ(PAGE_SIZE) ܵݳ(PIPE_SIZE) ָtail (i_zone[1]) ָhead (i_zone[0]) ָ(i_size) if (inode->i_count != 2) /* are there any writers? */ return read; sleep_on (&inode->i_wait); } // ȡܵβĩ˵ֽcharsڻҪȡֽcountcount // chars ڵǰܵкݵijsizesize chars = PAGE_SIZE - PIPE_TAIL (*inode); if (chars > count) chars = count; if (chars > size) chars = size; // ֽڼȥ˴οɶֽcharsۼѶֽ count -= chars; read += chars; // size ָܵβǰܵβָ루ǰchars ֽڣ size = PIPE_TAIL (*inode); PIPE_TAIL (*inode) += chars; PIPE_TAIL (*inode) &= (PAGE_SIZE - 1); // ܵеݸƵûСڹܵi ڵ㣬i_size ֶǹָܵ롣 while (chars-- > 0) put_fs_byte (((char *) inode->i_size)[size++], buf++); } // ѵȴùܵi ڵḶ̌ضȡֽ wake_up (&inode->i_wait); return read; } //// ܵд // inode ǹܵӦi ڵ㣬buf ݻָ룬count ǽдֽܵ int write_pipe (struct m_inode *inode, char *buf, int count) { int chars, size, written = 0; // дֽڼֵcount 0ѭִ² while (count > 0) { // ǰܵûѾ(size=0)ѵȴýڵḶ̌ûжܵߣ // SIGPIPE źţдֽ˳д0 ֽڣ-1ڸi ڵ // ˯ߣȴܵڳռ䡣 while (!(size = (PAGE_SIZE - 1) - PIPE_SIZE (*inode))) { wake_up (&inode->i_wait); if (inode->i_count != 2) { /* no readers */ current->signal |= (1 << (SIGPIPE - 1)); return written ? written : -1; } sleep_on (&inode->i_wait); } // ȡܵͷĩ˿ռֽcharsڻҪдֽcount // countchars ڵǰܵппռ䳤sizesize chars = PAGE_SIZE - PIPE_HEAD (*inode); if (chars > count) chars = count; if (chars > size) chars = size; // дֽڼȥ˴οдֽcharsۼдֽwritten count -= chars; written += chars; // size ָܵͷǰܵͷָ루ǰchars ֽڣ size = PIPE_HEAD (*inode); PIPE_HEAD (*inode) += chars; PIPE_HEAD (*inode) &= (PAGE_SIZE - 1); // ûchars ֽڵܵСڹܵi ڵ㣬i_size ֶǹָܵ롣 while (chars-- > 0) ((char *) inode->i_size)[size++] = get_fs_byte (buf++); } // ѵȴi ڵḶ̌дֽ˳ wake_up (&inode->i_wait); return written; } //// ܵϵͳú // fildes ָдһļ()ļָһܵi ڵ㡣fildes[0] // ڶܵݣfildes[1]ܵдݡ // ɹʱ0ʱ-1 int sys_pipe (unsigned long *fildes) { struct m_inode *inode; struct file *f[2]; int fd[2]; int i, j; // ϵͳļȡüֶΪ0 ֱüΪ1 j = 0; for (i = 0; j < 2 && i < NR_FILE; i++) if (!file_table[i].f_count) (f[j++] = i + file_table)->f_count++; // ֻһͷŸ(üλ) if (j == 1) f[0]->f_count = 0; // ûҵ-1 if (j < 2) return -1; // ȡõļṹֱһļʹ̵ļṹֱָָ // ļṹ j = 0; for (i = 0; j < 2 && i < NR_OPEN; i++) if (!current->filp[i]) { current->filp[fd[j] = i] = f[j]; j++; } // ֻһļͷŸþ if (j == 1) current->filp[fd[0]] = NULL; // ûҵоͷȡļṹλüֵ-1 if (j < 2) { f[0]->f_count = f[1]->f_count = 0; return -1; } // ܵi ڵ㣬Ϊܵ仺1 ҳڴ棩ɹӦͷļ // ṹ-1 if (!(inode = get_pipe_inode ())) { current->filp[fd[0]] = current->filp[fd[1]] = NULL; f[0]->f_count = f[1]->f_count = 0; return -1; } // ʼļṹָͬһi ڵ㣬дָ붼㡣1 ļṹļģʽΪ // 2 ļṹļģʽΪд f[0]->f_inode = f[1]->f_inode = inode; f[0]->f_pos = f[1]->f_pos = 0; f[0]->f_mode = 1; /* read */ f[1]->f_mode = 2; /* write */ // ļ鸴ƵӦûУ0˳ put_fs_long (fd[0], 0 + fildes); put_fs_long (fd[1], 1 + fildes); return 0; }
...
Expand> <Close
Sponsored links
File list
Tips: You can preview the content of files by clicking file names^_^Name | Size | Date |
---|---|---|
linux | 0.00 B | 0% 04-10-07 |
<boot> | 0.00 B | 04-10-07 15:01 |
bootsect.s | 12.49 kB | 08-01-04 21:38 |
head.s | 13.22 kB | 08-01-04 21:38 |
setup.s | 12.42 kB | 08-01-04 21:38 |
<fs> | 0.00 B | 04-10-07 15:01 |
bitmap.c | 8.46 kB | 02-09-04 13:12 |
block_dev.c | 3.95 kB | 02-09-04 13:12 |
buffer.c | 17.71 kB | 02-09-04 13:12 |
char_dev.c | 4.09 kB | 02-09-04 13:12 |
exec.c | 18.90 kB | 02-09-04 13:12 |
fcntl.c | 3.33 kB | 02-09-04 13:12 |
file_dev.c | 4.82 kB | 02-09-04 13:12 |
file_table.c | 209.00 B | 02-09-04 13:12 |
inode.c | 14.92 kB | 02-09-04 13:12 |
ioctl.c | 1.94 kB | 02-09-04 13:12 |
Makefile | 6.80 kB | 08-01-04 21:45 |
namei.c | 37.20 kB | 02-09-04 13:12 |
open.c | 10.00 kB | 02-09-04 13:12 |
pipe.c | 5.42 kB | 02-09-04 13:12 |
read_write.c | 5.86 kB | 02-09-04 13:12 |
stat.c | 2.69 kB | 02-09-04 13:12 |
super.c | 13.57 kB | 02-09-04 13:12 |
truncate.c | 2.46 kB | 02-09-04 13:12 |
<include> | 0.00 B | 04-10-07 15:01 |
a.out.h | 8.22 kB | 02-09-04 13:14 |
<asm> | 0.00 B | 04-10-07 15:01 |
io.h | 772.00 B | 02-09-04 13:14 |
memory.h | 1.03 kB | 08-01-04 22:13 |
segment.h | 2.50 kB | 02-09-04 13:14 |
system.h | 4.08 kB | 02-09-04 13:14 |
const.h | 589.00 B | 02-09-04 13:14 |
ctype.h | 1.68 kB | 02-09-04 13:14 |
errno.h | 2.30 kB | 02-09-04 13:14 |
fcntl.h | 3.30 kB | 02-09-04 13:14 |
<linux> | 0.00 B | 04-10-07 15:01 |
config.h | 2.16 kB | 02-09-04 13:14 |
fs.h | 9.72 kB | 02-09-04 13:14 |
hdreg.h | 2.94 kB | 02-09-04 13:14 |
head.h | 760.00 B | 02-09-04 13:14 |
kernel.h | 1.44 kB | 02-09-04 13:14 |
mm.h | 473.00 B | 02-09-04 13:14 |
sched.h | 13.25 kB | 02-09-04 13:14 |
sys.h | 5.41 kB | 02-09-04 13:14 |
tty.h | 3.93 kB | 02-09-04 13:14 |
signal.h | 4.01 kB | 02-09-04 13:14 |
stdarg.h | 1.77 kB | 02-09-04 13:14 |
stddef.h | 378.00 B | 02-09-04 13:14 |
string.h | 21.75 kB | 02-09-04 13:14 |
<sys> | 0.00 B | 04-10-07 15:01 |
stat.h | 2.37 kB | 02-09-04 13:14 |
times.h | 377.00 B | 02-09-04 13:14 |
types.h | 1.10 kB | 02-09-04 13:14 |
utsname.h | 423.00 B | 02-09-04 13:14 |
wait.h | 1.48 kB | 02-09-04 13:14 |
termios.h | 13.58 kB | 02-09-04 13:14 |
time.h | 1.81 kB | 02-09-04 13:14 |
unistd.h | 9.21 kB | 02-09-04 13:14 |
utime.h | 392.00 B | 02-09-04 13:14 |
<init> | 0.00 B | 04-10-07 15:01 |
main.c | 12.52 kB | 02-09-04 13:12 |
<kernel> | 0.00 B | 04-10-07 15:01 |
asm.s | 5.10 kB | 08-01-04 22:48 |
<blk_drv> | 0.00 B | 04-10-07 15:01 |
blk.h | 5.69 kB | 02-09-04 13:14 |
floppy.c | 23.49 kB | 02-09-04 13:12 |
hd.c | 17.14 kB | 02-09-04 13:12 |
ll_rw_blk.c | 7.59 kB | 02-09-04 13:12 |
Makefile | 4.25 kB | 08-01-04 22:53 |
ramdisk.c | 6.13 kB | 02-09-04 13:12 |
<chr_drv> | 0.00 B | 04-10-07 15:01 |
console.c | 30.91 kB | 02-09-04 13:12 |
keyboard.S | 21.04 kB | 08-01-04 22:59 |
Makefile | 4.81 kB | 08-01-04 22:59 |
rs_io.s | 5.75 kB | 08-01-04 22:59 |
serial.c | 2.91 kB | 02-09-04 13:12 |
tty_io.c | 18.40 kB | 02-09-04 13:12 |
tty_ioctl.c | 10.70 kB | 02-09-04 13:12 |
exit.c | 8.08 kB | 02-09-04 13:12 |
fork.c | 6.78 kB | 15-07-07 22:10 |
<math> | 0.00 B | 04-10-07 15:01 |
Makefile | 3.21 kB | 08-01-04 23:01 |
math_emulate.c | 2.08 kB | 02-09-04 13:12 |
mktime.c | 2.72 kB | 02-09-04 13:12 |
panic.c | 952.00 B | 02-09-04 13:12 |
printk.c | 1.75 kB | 02-09-04 13:12 |
sched.c | 18.57 kB | 16-07-07 21:23 |
signal.c | 5.61 kB | 02-09-04 13:12 |
sys.c | 7.62 kB | 02-09-04 13:12 |
system_call.s | 12.53 kB | 08-01-04 22:48 |
traps.c | 8.29 kB | 04-10-07 14:57 |
vsprintf.c | 9.96 kB | 02-09-04 13:12 |
<lib> | 0.00 B | 04-10-07 15:01 |
close.c | 397.00 B | 02-09-04 13:12 |
ctype.c | 1.72 kB | 02-09-04 13:12 |
dup.c | 401.00 B | 02-09-04 13:12 |
errno.c | 66.00 B | 02-09-04 13:12 |
execve.c | 607.00 B | 02-09-04 13:12 |
Makefile | 4.83 kB | 08-01-04 22:30 |
malloc.c | 13.50 kB | 02-09-04 13:12 |
open.c | 1.22 kB | 02-09-04 13:12 |
setsid.c | 382.00 B | 02-09-04 13:12 |
string.c | 199.00 B | 02-09-04 13:12 |
wait.c | 774.00 B | 02-09-04 13:12 |
write.c | 545.00 B | 02-09-04 13:12 |
_exit.c | 616.00 B | 02-09-04 13:12 |
Makefile | 8.58 kB | 08-01-04 22:21 |
<mm> | 0.00 B | 04-10-07 15:01 |
Makefile | 2.91 kB | 08-01-04 22:34 |
memory.c | 25.10 kB | 02-09-04 13:12 |
page.s | 842.00 B | 08-01-04 22:34 |
<tools> | 0.00 B | 04-10-07 15:01 |
build.c | 8.12 kB | 02-09-04 13:12 |
Sponsored links