在linux中打印子变量值

时间:2011-03-02 04:02:54

标签: linux

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

2 个答案:

答案 0 :(得分:3)

如果您的意思是从父级操纵子进程变量,会忘记它。

除了一些良好实现的进程间通信(IPC)或一些kludgy“使用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;
}

此代码输出:

Parent: val = 99
Child:  val = 101
Parent: val = 98
Child:  val = 102
Parent: val = 97
Child:  val = 103
Parent: val = 96
Child:  val = 104
Parent: val = 95
Child:  val = 105

显示val变量对于两个进程是不同的。如果共享,您可能会看到99, 100, 99, 100, 99, 100, ...

答案 1 :(得分:0)

这是我申请类似需求的一种方式,但我不知道它是否与您的问题相符,因为它位于灰色区域的旁边,我想:

你可以在一个bash shell中实现这一点,假设你定义了父/子命令并将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.

我希望这可以帮助你开始或给你想法,成功地做你需要的。