我在工作中碰到了一个这样的问题,有两个程序,第一个程序会fork一个进程exec调用第二个程序,这样调用后,第一个程序还是继续执行父进程的。我要求第一个程序的父进程停止运行,直到第二个程序退出或运行到某个时候才继续运行。
下面是两个例子程序的代码
先运行gui程序,再运行player程序
./gui&在后台运行
./player
/*gui.c*/
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_SERVER "./fifoserver"
int main()
{
int fd;
char buf;
printf("GUI:Create Name Pipe/n");
umask(0);
//if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL))
if(mkfifo(FIFO_SERVER,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP))
{
printf("cannot create fifoserver/n");
}
printf("GUI:Open NamePipe .../n");
//=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
fd=open(FIFO_SERVER,O_RDWR,0);
//fd=open(FIFO_SERVER,O_RDONLY,0);
if(fd==-1)
{
printf("GUI:Open error/n");
return 0;
}
printf("GUI:Wait NamePipe Write.../n");
read(fd,(char *)(&buf),1); //程序会在这里停止,直到运行player程序
printf("GUI:player finished!/n");
unlink(FIFO_SERVER);
}
/*player.c*/
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_SERVER "./fifoserver"
int main()
{
int fd;
fd=open(FIFO_SERVER,O_WRONLY,0);
if(fd==-1)
{
printf("open for read error/n");
return 0;
}
write(fd,(char *)"OVER",5);//数据写入命名管道后,gui继续运行
close(fd);
}
下面是两个例子程序的代码
先运行gui程序,再运行player程序
./gui&在后台运行
./player
/*gui.c*/
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_SERVER "./fifoserver"
int main()
{
int fd;
char buf;
printf("GUI:Create Name Pipe/n");
umask(0);
//if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL))
if(mkfifo(FIFO_SERVER,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP))
{
printf("cannot create fifoserver/n");
}
printf("GUI:Open NamePipe .../n");
//=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
fd=open(FIFO_SERVER,O_RDWR,0);
//fd=open(FIFO_SERVER,O_RDONLY,0);
if(fd==-1)
{
printf("GUI:Open error/n");
return 0;
}
printf("GUI:Wait NamePipe Write.../n");
read(fd,(char *)(&buf),1); //程序会在这里停止,直到运行player程序
printf("GUI:player finished!/n");
unlink(FIFO_SERVER);
}
/*player.c*/
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_SERVER "./fifoserver"
int main()
{
int fd;
fd=open(FIFO_SERVER,O_WRONLY,0);
if(fd==-1)
{
printf("open for read error/n");
return 0;
}
write(fd,(char *)"OVER",5);//数据写入命名管道后,gui继续运行
close(fd);
}