Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!unisoft!rembo From: rembo@unisoft.UUCP (Tony Rems) Newsgroups: comp.unix.programmer Subject: Re: how to put a program into a .plan file Message-ID: <3149@unisoft.UUCP> Date: 5 Oct 90 22:36:32 GMT References: <978@bbt.UUCP> <1990Sep26.201406.12663@athena.mit.edu> <1990Sep28.154733.26346@cbnewsj.att.com> Reply-To: rembo@unisoft.UUCP (Tony Rems) Lines: 50 In article <1990Sep28.154733.26346@cbnewsj.att.com> dwex@cbnewsj.att.com (david.e.wexelblat) writes: >In article <1990Sep26.201406.12663@athena.mit.edu>, jik@athena.mit.edu (Jonathan I. Kamens) writes: >> In article <978@bbt.UUCP>, rgs@bbt.UUCP (steinbeiser) writes: >> |> Ok, how do you check the status of the file to see if it has >> |> been open by someone else? Its not obvious how (or if) stat() would do this. >> |> Also, would the C program have to be running constantly in the background >> |> someplace? >> >> 1. You open the pipe for write, and do a select() on it, and when you get an >> OK to write from select(), you know that someone has opened the other end. >> > >Or, in System V, quoting from the open(2) man page: > > When opening a FIFO with O_RDONLY or O_WRONLY set: > > If O_NDELAY is set: > [ not relevant ] > > If O_NDELAY is clear: > An open for reading-only will block until a process > opens the file for writing. An open for writing-only > will block until a process opens the file for reading. > >So all you have to do is make .plan a FIFO, and have a program open >it for writing (the program will have to be in an infinite loop, though). > There are a bunch of ways to handle this in C. Doing a select is really overkill. The simplest way is to have a loop that looks something like: make fifo; loop forever; open FIFO for writing /* This will block until someon opens it for reading */ fork() if in child: exec a program if in parent close FIFO wait for child; I have a program called plan which takes one argument, the name of a program to exec as your plan. This version inherits your environment on the exec. If anyone would like a copy of the source, drop me a note. -Tony