在linux中打印子变量值

在linux中打印子变量值,linux,Linux,在linux中,我有一个父进程和一个子进程。我想操纵传递给子进程的变量,然后在屏幕上打印它们。如何打印子进程值 如果您的意思是从父进程操纵子进程变量,那就别提了 除非有一些实现良好的进程间通信(IPC)或一些“使用root读取/写入其他进程内存”的拙劣做法,否则这还没有完成 进程维护它们自己的内存空间,并且不会相互干扰——有一些方法可以在进程之间共享内存,但您通常必须努力做到这一点:简单的fork不会给您提供这一点 如果你的意思是从子对象,那么,子对象可以完全访问它的所有变量。只需根据需要使用和

在linux中,我有一个父进程和一个子进程。我想操纵传递给子进程的变量,然后在屏幕上打印它们。如何打印子进程值

如果您的意思是从父进程操纵子进程变量,那就别提了

除非有一些实现良好的进程间通信(IPC)或一些“使用root读取/写入其他进程内存”的拙劣做法,否则这还没有完成

进程维护它们自己的内存空间,并且不会相互干扰——有一些方法可以在进程之间共享内存,但您通常必须努力做到这一点:简单的fork不会给您提供这一点

如果你的意思是从子对象,那么,子对象可以完全访问它的所有变量。只需根据需要使用和/或修改它们,但这不会改变它们在父级中的值。如果您想将更改传递回父级,那么您必须再次使用某种IPC机制(甚至是类似的机制,如返回码或使用文件进行信息传输)


您可以在此处看到流程如何获得自己的变量副本:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>

int main (void) {
    int i, val = 100;
    switch (fork()) {
        case -1: { // Couldn't fork, just output error.
            printf ("Could not fork, error = %d\n", errno);
            break;
        }
        case 0: { // I am the child, increase val at t=1,3,5,7,...
            sleep (1);
            for (i = 0; i < 5; i++) {
                val++;
                printf ("Child:  val = %d\n", val);
                sleep (2);
            }
            break;
        }
        default: { // I am the parent, decrease val at t=0,2,4,6,...
            for (i = 0; i < 5; i++) {
                val--;
                printf ("Parent: val = %d\n", val);
                sleep (2);
            }
            break;
        }
    }
    return 0;
}

显示两个进程的
val
变量是不同的。如果它是共享的,您可能会看到
99100991009910099100,

这里是我申请类似需求的一种方法,但我不知道它是否符合您的问题,因为我猜它在灰色区域的旁边:

您可以在一个bashshell中实现这一点,假设您定义父/子命令并将一个pid关联到每个父命令,然后您可以在将父/子命令关联到一起后执行任何需要的操作。但是“用手”。例如,父命令可以存储您需要的任何类型的变量/文件,并且您可以在启动时与子进程一起获取/读取它(假设没有其他进程修改它们..非常危险,因此在这种情况下,最好将所有数据保存在同一个螺母中):

请注意,此代码仅用于说明我的上述答案,而不是开箱即用:

# define your parent/child commands  
for directory in "${SVN_PROJECTS_DIR[@]}"  
do  
# array push  
PARENT_CMD=( "${PARENT_CMD[@]}" "$parent_cmd" )  
CHILD_CMD=( "${CHILD_CMD[@]}" "$child_cmd" )  
done  

# Run the parent commands and associate a child command to the parent pid  
for (( index = 0 ; index < ${#PARENT_CMD[@]} ; index++ ))  
do  
# Run the parent cmd
(eval "${PARENT_CMD[$index]}") &
# Store the parent pid into a specific array
# we actually need the pid only but all parent pids will be needed at the next stage
PARENT_PID=( "${PARENT_PID[@]}" "$!" )

# Associate the child cmd to the parent pid
CHILD_CMD_[$!]="${CHILD_CMD[index]}"
done

# Then RUN CHILD THREADS when parent pid's end
while :; do

# Scan running parent pid's
for pid in "${PARENT_PID[@]}"
do
    # Gracefully closed parent pid => run child pid
    if wait "$pid"; then
    # start child pid IF parent_pid was not already tagged as starting a child
        in_array "${pid}" "${TAGGED_PARENT_PID[@]}"
        RET=$?

        # if RET=1 => parent pid was not in tagged pid's array
        if [ "${RET}" -eq 1  ] ;then
            # Run child process (if it was not already started)
            (eval "${CHILD_CMD_[$pid]}") &
            RET=$?

            # Abort on error (do not want to global track this)
            [[ "X$RET" != "X0" ]] && (( errors++ )) && exit 1

            # Store the parent pid in TAGGED_PARENT_PID array sothat you know if a child process has been started for the corresponding parent pid
            TAGGED_PARENT_PID=( "${TAGGED_PARENT_PID[@]}" "$pid" )

            # Store also the child pid in child_pid array
            CHILD_PID=( "${CHILD_PID[@]}" "$!" )

            echo "=> $! child pid has been started"
            echo "=> cmd: ${CHILD_CMD_[$pid]}"

            # Trac started children processes for further treatments
            (( started_children++ ))
        fi

    # Errored parent pid (bad exit code)
    else
        # assuming you've checked elsewhere that parent's pid is not still alive
        echo "parent pid ended with a bad exit status code"
        (( errors++ ))
    fi

done # end FOR
# GRACEFUL EXIT CONDITIONS
# COND 1: WHILE exit if all children have been effectively started
[[ "$started_children" -eq  "${#CHILD_CMD[@]}" ]] && echo "All children processes have been started" && break
# COND 2: blabla

# BAD EXIT CONDITIONS
# you have to plan them, like timeout or error exits
done # end WHILE


# At this stage, your parent processes are finished (gracefully) and your children processes have been started. So you could get whatever variable or file you need, making sure they are inherited from the parent process. You can also use locked files to ensure no other process(es) are touching your parent file.
#定义父/子命令
对于“${SVN_PROJECTS_DIR[@]}”中的目录
做
#阵列推送
PARENT_CMD=(“${PARENT_CMD[@]}”“$PARENT_CMD”)
CHILD_CMD=(“${CHILD_CMD[@]}”“$CHILD_CMD”)
完成
#运行父命令并将子命令与父pid关联
对于((index=0;index<${PARENT_CMD[@]};index++)
做
#运行父cmd
(eval“${PARENT_CMD[$index]}”)&
#将父pid存储到特定数组中
#实际上,我们只需要pid,但在下一阶段将需要所有父pid
父项_-PID=(“${PARENT _-PID[@]}”“$!”)
#将子cmd与父pid关联
CHILD_CMD_[$!]=“${CHILD_CMD[index]}”
完成
#然后在父pid结束时运行子线程
while:;做
#扫描正在运行的父pid
对于“${PARENT_pid[@]}”中的pid
做
#优雅关闭的父pid=>运行子pid
如果等待“$pid”;然后
#如果父pid尚未标记为启动子pid,则启动子pid
在数组“${pid}”${taged_PARENT_pid[@]}”中
RET=$?
#如果RET=1=>父pid不在标记pid的数组中
如果[“${RET}”-等式1];然后
#运行子进程(如果尚未启动)
(eval“${CHILD\u CMD\[$pid]}”)&
RET=$?
#错误时中止(不希望全局跟踪此)
[[“X$RET”!=“X0”]&&((错误++)&&退出1
#将父pid存储在标记的\u parent\u pid数组中,以便知道是否已为相应的父pid启动子进程
TAGGED_PARENT_PID=(“${TAGGED_PARENT_PID[@]}”“$PID”)
#还将子pid存储在子pid数组中
CHILD_-PID=(“${CHILD_-PID[@]}”“$!”)
echo“=>$!子pid已启动”
echo“=>cmd:${CHILD\u cmd\[$pid]}”
#Trac开始对儿童进行进一步治疗
((启动了_children++)
fi
#错误的父pid(错误的退出代码)
其他的
#假设您已在其他地方检查父级的pid是否仍然有效
echo“父pid以错误的退出状态代码结束”
((错误++))
fi
结束
#优美退出条件
#条件1:如果所有子项都已有效启动,则退出
[[“$started_children”-eq“${CHILD_CMD[@]}”]&&echo“所有子进程都已启动”&&break
#条件2:布拉布拉
#恶劣的出口条件
#您必须计划它们,比如超时或错误退出
完成#中途结束
#在这个阶段,您的父进程已经完成(正常地),您的子进程已经启动。因此,您可以获得所需的任何变量或文件,确保它们是从父进程继承的。您还可以使用锁定的文件来确保没有其他进程接触到您的父文件。
我希望这可能会帮助你开始或给你的想法,成功地做你需要的

# define your parent/child commands  
for directory in "${SVN_PROJECTS_DIR[@]}"  
do  
# array push  
PARENT_CMD=( "${PARENT_CMD[@]}" "$parent_cmd" )  
CHILD_CMD=( "${CHILD_CMD[@]}" "$child_cmd" )  
done  

# Run the parent commands and associate a child command to the parent pid  
for (( index = 0 ; index < ${#PARENT_CMD[@]} ; index++ ))  
do  
# Run the parent cmd
(eval "${PARENT_CMD[$index]}") &
# Store the parent pid into a specific array
# we actually need the pid only but all parent pids will be needed at the next stage
PARENT_PID=( "${PARENT_PID[@]}" "$!" )

# Associate the child cmd to the parent pid
CHILD_CMD_[$!]="${CHILD_CMD[index]}"
done

# Then RUN CHILD THREADS when parent pid's end
while :; do

# Scan running parent pid's
for pid in "${PARENT_PID[@]}"
do
    # Gracefully closed parent pid => run child pid
    if wait "$pid"; then
    # start child pid IF parent_pid was not already tagged as starting a child
        in_array "${pid}" "${TAGGED_PARENT_PID[@]}"
        RET=$?

        # if RET=1 => parent pid was not in tagged pid's array
        if [ "${RET}" -eq 1  ] ;then
            # Run child process (if it was not already started)
            (eval "${CHILD_CMD_[$pid]}") &
            RET=$?

            # Abort on error (do not want to global track this)
            [[ "X$RET" != "X0" ]] && (( errors++ )) && exit 1

            # Store the parent pid in TAGGED_PARENT_PID array sothat you know if a child process has been started for the corresponding parent pid
            TAGGED_PARENT_PID=( "${TAGGED_PARENT_PID[@]}" "$pid" )

            # Store also the child pid in child_pid array
            CHILD_PID=( "${CHILD_PID[@]}" "$!" )

            echo "=> $! child pid has been started"
            echo "=> cmd: ${CHILD_CMD_[$pid]}"

            # Trac started children processes for further treatments
            (( started_children++ ))
        fi

    # Errored parent pid (bad exit code)
    else
        # assuming you've checked elsewhere that parent's pid is not still alive
        echo "parent pid ended with a bad exit status code"
        (( errors++ ))
    fi

done # end FOR
# GRACEFUL EXIT CONDITIONS
# COND 1: WHILE exit if all children have been effectively started
[[ "$started_children" -eq  "${#CHILD_CMD[@]}" ]] && echo "All children processes have been started" && break
# COND 2: blabla

# BAD EXIT CONDITIONS
# you have to plan them, like timeout or error exits
done # end WHILE


# At this stage, your parent processes are finished (gracefully) and your children processes have been started. So you could get whatever variable or file you need, making sure they are inherited from the parent process. You can also use locked files to ensure no other process(es) are touching your parent file.