2

I'm calling system("bash ../tools/bashScript \"This is an argument!\" &"), then I'm calling close(socketFD) directly after system returns, of course I've read that system's argument appended with a & will fork the command, so system returns immediately and I've checked that!

Now the problem is that I don't want the socket to stay connected while the command is being executed, but the weirdest thing is that the work flow of the program continues and close is somewhat seen and overlooked and once the command finishes the close takes effect!

I tried to call close twice after each others to make sure to delete every copy of the socketFD if created, still the same problem, I honestly don't know why close is being called but doesn't take effect until the command (the bash script) finishes!

Please advise!

4

1 回答 1

2

我对你在做什么有点困惑,但system实际上是fork. 这意味着两个进程都有套接字描述符的副本*,并且只有一个“副本”被关闭。另一个持续到system执行命令的分叉进程也终止。

为什么不在调用之前关闭套接字system

您也可以尝试shutdowndup'd 描述符上使用,例如:

shutdown(s,SHUT_RDWR);

*通过调用对文件和套接字描述符发生的事情的规则fork比“复制”要微妙一些,但这足以解释为什么它不适合你。

于 2013-05-13T20:40:30.783 回答