100 likes | 277 Views
제 32 강 : pipe file. pipe file. grep | who. write_count. 0. Data. PIPSIZ. read_count. user per process. (system) file table. inode table. u-ofile. inode. fd. 0 1 2 3 4. device. offset. dev_tab. i_size. f_offset.
E N D
제32강 : pipe file pipe file
grep | who write_count 0 Data PIPSIZ read_count
userper process (system) file table inode table u-ofile inode fd 0 1 2 3 4 device offset dev_tab i_size f_offset
16 bit (64 KB) for (offset, size) is too small for regular files userper process (system) file table inode table u-ofile inode fd 0 1 2 3 4 device offset dev_tab i_size0 i_size1 f_offset [0] f_offset [1]
16 bit (64 KB) for (offset, size) is too small for regular files But 16 bit is enough for pipe file (size, offset) Use 2nd word for other purpose userper process (system) file table inode table u-ofile inode fd 0 1 2 3 4 Pipe file 4096 Bytes offset PIPSIZ (7846, 7715) i_size0 i_size1 f_offset [0] f_offset [1] write count read_count (count MOD PIPSIZ) (count MOD PIPSIZ)
7758: readp(fp) 7759: int *fp; 7760: { 7761: register *rp, *ip; 7763: rp = fp; 7764: ip = rp->f_inode; 7765: loop: 7768: plock(ip); 7772: if(rp->f_offset[1] == ip->i_size1) { 7773: if(rp->f_offset[1] != 0) { 7774: rp->f_offset[1] = 0; 7775: ip->i_size1 = 0; 7776: if(ip->i_mode&IWRITE) { 7777: ip->i_mode =& ~IWRITE; 7778: wakeup(ip+1); 7779: } 7780: } 7781: 7786: prele(ip); 7787: if(ip->i_count < 2) 7788: return; 7789: ip->i_mode =| IREAD; 7790: sleep(ip+2, PPIPE); 7791: goto loop; 7792: } 7795: u.u_offset[0] = 0; 7796: u.u_offset[1] = rp->f_offset[1]; 7797: readi(ip); 7798: rp->f_offset[1] = u.u_offset[1]; 7799: prele(ip); 7800: } 7805: writep(fp) 7806: { 7807: register *rp, *ip, c; 7809: rp = fp; 7810: ip = rp->f_inode; 7811: c = u.u_count; 7812: loop: 7815: plock(ip); 7816: if(c == 0) { 7817: prele(ip); 7818: u.u_count = 0; 7819: return; 7820: } 7825: if(ip->i_count < 2) { 7826: prele(ip); 7827: u.u_error = EPIPE; 7828: psignal(u.u_procp, SIGPIPE); 7829: return; 7830: } 7835: if(ip->i_size1 == PIPSIZ) { 7836: ip->i_mode =| IWRITE; 7837: prele(ip); 7838: sleep(ip+1, PPIPE); 7839: goto loop; 7840: } 7844: u.u_offset[0] = 0; 7845: u.u_offset[1] = ip->i_size1; 7846: u.u_count = min(c, PIPSIZ-u.u_offset[1]); 7847: c =- u.u_count; 7848: writei(ip); 7849: prele(ip); 7850: if(ip->i_mode&IREAD) { 7851: ip->i_mode =& ~IREAD; 7852: wakeup(ip+2); 7853: } 7854: goto loop; 7855: }
write_count i_size1 0 Data PIPSIZ read_count f_offset[1] Also alternate use of i_mode : i_mode = IWRITE (writer is blocked) or IREAD(reader is blocked) i_size1: Write Counter f_offset[1]: Read Counter i_mode: Lock pipe file
7758: readp(fp) 7759: int *fp; 7760: { 7761: register *rp, *ip; 7763: rp = fp; 7764: ip = rp->f_inode; 7765: loop: 7768: plock(ip); 7772: if(rp->f_offset[1] == ip->i_size1) { 7773: if(rp->f_offset[1] != 0) { 7774: rp->f_offset[1] = 0; 7775: ip->i_size1 = 0; 7776: if(ip->i_mode&IWRITE) { 7777: ip->i_mode =& ~IWRITE; 7778: wakeup(ip+1); 7779: } 7780: } 7781: 7786: prele(ip); 7787: if(ip->i_count < 2) 7788: return; 7789: ip->i_mode =| IREAD; 7790: sleep(ip+2, PPIPE); 7791: goto loop; 7792: } 7795: u.u_offset[0] = 0; 7796: u.u_offset[1] = rp->f_offset[1]; 7797: readi(ip); 7798: rp->f_offset[1] = u.u_offset[1]; 7799: prele(ip); 7800: } 7758 (21-1) If reader’s counter reaches writer’s counter reader must sleep reset both counters to zero i_size1: Write Counter f_offset[1]: Read Counter i_mode: Lock pipe file After read-pipe is complete, update f_offset[1]
7805: writep(fp) 7806: { 7807: register *rp, *ip, c; 7809: rp = fp; 7810: ip = rp->f_inode; 7811: c = u.u_count; 7812: loop: 7815: plock(ip); 7816: if(c == 0) { 7817: prele(ip); 7818: u.u_count = 0; 7819: return; 7820: } 7825: if(ip->i_count < 2) { 7826: prele(ip); 7827: u.u_error = EPIPE; 7828: psignal(u.u_procp, SIGPIPE); 7829: return; 7830: } 7835: if(ip->i_size1 == PIPSIZ) { 7836: ip->i_mode =| IWRITE; 7837: prele(ip); 7838: sleep(ip+1, PPIPE); 7839: goto loop; 7840: } 7844: u.u_offset[0] = 0; 7845: u.u_offset[1] = ip->i_size1; 7846: u.u_count = min(c, PIPSIZ-u.u_offset[1]); 7847: c =- u.u_count; 7848: writei(ip); 7849: prele(ip); 7850: if(ip->i_mode&IREAD) { 7851: ip->i_mode =& ~IREAD; 7852: wakeup(ip+2); 7853: } 7854: goto loop; 7855: } 7800 (21-1) i_size1: Write Counter f_offset[1]: Read Counter i_mode: Lock pipe file If writer’s counter reaches max (pipe size) writer must sleep ( PIPSIZ – i_size1) PIPSIZ Data c= u.u_count /* amount of data */ After write-to-pipe is complete, writei() updates the size of pipe file writei() updates i_size1
IPC • Group of system calls (kernel service) for data exchange synchronization between different processes • Here, for pipe IPC – kernel support: • moved data between processes • Synchronized processes (block/wakeup) • Other IPC examples • pipe socket signal …. • Why so many? • Varying data size, synchronization requirements • (such as frequency, speed …..)