【发布时间】:2013-02-05 21:11:45
【问题描述】:
这是我需要的: 我做了一个 C 程序,它分叉了 2 个孩子:P1,P2 我还制作了一些 bash 脚本。
我需要 P1 来运行 script1.sh,并且需要 P2 来运行 script2.sh。
目前我正在使用函数system("script_name.sh") 或system("script_name &") 使其异步,我不知道这是否是正确的选择,因为现在它们不能按我的意愿工作。顺便说一句:
script1.sh 这样做:
# it search a word in a dictionary and write in a file the exact line in which that
# word is in the dictionary, i.e. word="a" is in position "1" so
# it will write "1" in the first line of file.
while read line;
do
sleep 1
TRASFORMATA=$( echo "$line" | tr "[:upper:]" "[:lower:]" | sed -e 's/ //g' )
echo "WORDS TO SEARCH IN DICTIONARY : $TRASFORMATA:"
WORD=$( fgrep -w -i "$TRASFORMATA" "$dictfile" ) # got from dictionary
echo $WORD
if [ -z "$WORD" ] ## if it's not in dictionary
then
echo "$WORD not found!"
echo
else
echo "Word and relative line found.........."
##### if found, it write the relative line to a file #####
LINE1=$( fgrep -w -n "$WORD" "$dictfile" | sed s/:$WORD//g )
echo "$LINE1" >> "$FILE_OUTPUT"
fi
done < "$FILE_INPUT"
script2.sh 这样做:
# delete lines starting with letter 'z' FROM SAME FILE THAT USES script1.sh
sleep 1
while true;
do
sleep 2
echo "DELETING WORDS "
sed -i '/^z/d' "$FILE_OUTPUT"
done
它们处理相同的文件 (.txt)。 我想要的是同时运行它们并且它们必须轮流工作,我的意思是在P之后!已读取第一行及其 script1,P2 必须运行其 script2,与第二行、第三行等相同...
如何从 C 程序中做到这一点?重要的是每个进程都运行一个脚本,并且这些脚本必须并行交替执行。 通过这种方式,我达到了我的目标,即让 2 个进程一起产生一个输出,以便让三分之一的进程读取该输出并处理它!
我希望它很清楚
感谢您的帮助
【问题讨论】:
-
您的解释不清楚,因为 1) 将脚本作为“script &”“script2 &”运行将“异步”运行它们。您在解释的最后暗示它们实际上是为了“同步”2)该文件没有由 script1 编写的字母 z 行,那么 script2 如何删除它们 3)您为什么不在 python 中编写整个内容, perl 等等等等更好的东西??????
-
好吧,它们不必是“同步的”,因为如果我使用 system("script1") script2 必须等待 script1 完成,这不是我想要的,我没有写他们使用的文件内容,因为它是我的 c 程序的一部分,并且我包含带有 'z' 的单词。我选择 C 是因为我正在研究它,我想尝试这个东西