1*61186Sbostic.\" Copyright (c) 1980, 1991, 1993 2*61186Sbostic.\" The Regents of the University of California. All rights reserved. 320151Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520151Smckusick.\" 6*61186Sbostic.\" @(#)vfork.2 8.1 (Berkeley) 06/04/93 747208Scael.\" 847208Scael.Dd 947208Scael.Dt VFORK 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm vfork 1347208Scael.Nd spawn new process in a virtual memory efficient way 1447208Scael.Sh SYNOPSIS 1547208Scael.Fd #include <unistd.h> 1647208Scael.Ft int 1747208Scael.Fn vfork void 1847208Scael.Sh DESCRIPTION 1947208Scael.Fn Vfork 2020151Smckusickcan be used to create new processes without fully copying the address 2120151Smckusickspace of the old process, which is horrendously inefficient in a paged 2220151Smckusickenvironment. It is useful when the purpose of 2347208Scael.Xr fork 2 2420151Smckusickwould have been to create a new system context for an 2547208Scael.Xr execve . 2647208Scael.Fn Vfork 2720151Smckusickdiffers from 2847208Scael.Xr fork 2920151Smckusickin that the child borrows the parent's memory and thread of 3020151Smckusickcontrol until a call to 3147208Scael.Xr execve 2 3220151Smckusickor an exit (either by a call to 3347208Scael.Xr exit 2 3420151Smckusickor abnormally.) 3520151SmckusickThe parent process is suspended while the child is using its resources. 3647208Scael.Pp 3747208Scael.Fn Vfork 3820151Smckusickreturns 0 in the child's context and (later) the pid of the child in 3920151Smckusickthe parent's context. 4047208Scael.Pp 4147208Scael.Fn Vfork 4220151Smckusickcan normally be used just like 4347208Scael.Xr fork . 4420151SmckusickIt does not work, however, to return while running in the childs context 4523829Ssechrestfrom the procedure that called 4647208Scael.Fn vfork 4720151Smckusicksince the eventual return from 4847208Scael.Fn vfork 4920151Smckusickwould then return to a no longer existent stack frame. 5020151SmckusickBe careful, also, to call 5147208Scael.Xr _exit 5220151Smckusickrather than 5347208Scael.Xr exit 5420151Smckusickif you can't 5547208Scael.Xr execve , 5620151Smckusicksince 5747208Scael.Xr exit 5820151Smckusickwill flush and close standard I/O channels, and thereby mess up the 5920151Smckusickparent processes standard I/O data structures. 6020151Smckusick(Even with 6147208Scael.Xr fork 6220151Smckusickit is wrong to call 6347208Scael.Xr exit 6420151Smckusicksince buffered data would then be flushed twice.) 6547208Scael.Sh SEE ALSO 6647208Scael.Xr fork 2 , 6747208Scael.Xr execve 2 , 6847208Scael.Xr sigvec 2 , 6947208Scael.Xr wait 2 , 7047208Scael.Sh DIAGNOSTICS 7120151SmckusickSame as for 7247208Scael.Xr fork . 7347208Scael.Sh BUGS 7420152SmckusickThis system call will be eliminated when proper system sharing 7520152Smckusickmechanisms are implemented. 7620152SmckusickUsers should not depend on the memory 7720151Smckusicksharing semantics of 7847208Scael.Xr vfork 7920152Smckusickas it will, in that case, be made synonymous to 8047208Scael.Xr fork . 8147208Scael.Pp 8220151SmckusickTo avoid a possible deadlock situation, 8323829Ssechrestprocesses that are children in the middle 8420151Smckusickof a 8547208Scael.Fn vfork 8647208Scaelare never sent 8747208Scael.Dv SIGTTOU 8847208Scaelor 8947208Scael.Dv SIGTTIN 9047208Scaelsignals; rather, 9120151Smckusickoutput or 9247208Scael.Xr ioctl 2 9347208Scaelcalls 9420151Smckusickare allowed 9520151Smckusickand input attempts result in an end-of-file indication. 9647208Scael.Sh HISTORY 9747208ScaelThe 9847208Scael.Nm 9947208Scaelfunction call appeared in 10047208Scael.Bx 3.0 . 101