linux 0.11 ÄÚºËѧϰ pipe.c
/*
* ¸ÃÎļþÖеÄÁ½¸öº¯Êýread_pipeºÍwrite_pipeÊÇÉϲ㺯Êý
* readºÍwriteµÄµ×²ãʵÏÖ
*/
/*
* linux/fs/pipe.c
*
* (C) 1991 Linus Torvalds
*/
#include <signal.h>
#include <linux/sched.h>
// ÄÚ´æ¹ÜÀíÍ·Îļþ¡£º¬ÓÐÒ³Ãæ´óС¶¨ÒåºÍһЩҳÃæÊͷź¯ÊýÔÐÍ
#include <linux/mm.h> /* for get_free_page */
#include <asm/segment.h>
/* pipeÀàËÆÓÚ¶ÓÁУ¬ÏòpipeµÄβ²¿Ð´ÈëÊý¾Ý£¬´ÓpipeµÄÍ·²¿¶ÁÈ¡Êý¾Ý */
/* ¹ÜµÀ¶Á²Ù×÷º¯Êý */
/* ²ÎÊýinode ÊǹܵÀ¶ÔÓ¦µÄi ½Úµã£¬buf ÊÇÊý¾Ý»º³åÇøÖ¸Õ룬count ÊǶÁÈ¡µÄ×Ö½ÚÊý */
/*
* read_pipe()º¯ÊýÓÃÓÚ¶ÁÈ¡¹ÜµÀÖеÄÊý¾Ý£¬Èç¹û¹ÜµÀÖÐûÓÐÊý¾ÝµÄ»°£¬
* »½ÐÑд¹ÜµÀ½ø³Ì£¬×Ô¼ºË¯Ãߣ¬Èç¹û¶Áµ½Êý¾Ý£¬²¢°ÑÊý¾Ý´«µ½Óû§»º³åÇø£¬
* µ±°Ñ¹ÜµÀÖÐËùÓеÄÊý¾Ý¶¼È¡×ߺó£¬Ò²Òª»½Ðѵȴýд¹ÜµÀµÄ½ø³Ì£¬²¢·µ»Ø
* ÒѶÁÊý¾Ý×Ö½ÚÊý
*/
int read_pipe(struct m_inode * inode, char * buf, int count)
{
int chars, size, read = 0;
while (count>0)
{
// Èôµ±Ç°¹ÜµÀÖÐûÓÐÊý¾Ý(size=0)£¬Ôò»½Ðѵȴý¸Ã½ÚµãµÄ½ø³Ì
while (!(size=PIPE_SIZE(*inode)))
{
wake_up(&inode->i_wait);
// Èç¹ûÒÑûÓÐд¹ÜµÀÕß
if (inode->i_count != 2) /* are there any writers? */
return read; // ·µ»ØÒѶÁ×Ö½ÚÊý
// ·ñÔòÔÚ¸Ãi ½ÚµãÉÏ˯Ãߣ¬µÈ´ýÐÅÏ¢
sleep_on(&inode->i_wait);
}
// È¡¹ÜµÀβµ½»º³åÇøÄ©¶ËµÄ×Ö½ÚÊýchars
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);
// ½«¹ÜµÀÖеÄÊý¾Ý¸´ÖƵ½Óû§»º³åÇøÖÐ
while (chars-->0)
put_fs_byte(((char *)inode->i_size)[size++],buf++);
}
// »½Ðѵȴý¸Ã¹ÜµÀi ½ÚµãµÄ½ø³Ì
wake_up(&inode->i_wait);
// ·µ»Ø¶ÁÈ¡µÄ×Ö½ÚÊý
return read;
Ïà¹ØÎĵµ£º
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH&nbs ......
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *thread1(void *);
void *thread2(void *);
int i=1;
main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_create(&t_a,NULL,thread1,(void *)NULL) ......
×ܽáÕ½ڣºµÚÒ»Õ É豸Çý¶¯³ÌÐò¼ò½é
µÚ¶þÕ ¹¹ÔìºÍÔËÐÐÄ£¿é
Ìáµ½Çý¶¯³ÌÐò¾Í²»µÃ²»ÏÈ˵ÏÂÁ½¸öÃû´Ê£ºÄں˿ռäºÍÓû§¿Õ¼ä¡£Óû§¿Õ¼ä¼´Óû§Ó¦ÓÃËùÔËÐеĿռ䣬Äں˿ռäÖµµÃÊDzÙ×÷ϵͳÄÚºËÔËÐеĿռ䡣Óû§¿Õ¼ä²»ÄÜÖ±½ÓÒýÓûòÕß²Ù×÷Äں˿ռäµÄÊý¾Ý£¬Äں˿ռäÒ²²»ÄÜÖ±½ ......
/*
* ¸ÃÎļþʵÏÖϵͳµ÷ÓÃread£¬writeºÍlseek¡£
*/
/*
* linux/fs/read_write.c
*
* (C) 1991 Linus Torvalds
*/
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/kernel.h>
#include < ......