c - C 中指针和子进程的问题

标签 c pointers fork execl

每次运行此代码时,都会将 0 和 null 作为子进程的参数传入。我知道它与指针有关,但我似乎无法填充我的数组并传递参数。我已经看它太久了,我希望有人能发现一个愚蠢的错误。

/* exec example *
 * for CS-350   */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int  main(int argc, char *argv[])
{
    int pid,i;
    int sum;
    int total = 0;
    char  *procpath = "slave";
    char  *procname = "slave";

    int size = argc-1;

    int input[size];


    int k;
    for (k = 0; k < size; k++)
    {
        int m = (strtol(argv[k + 1], NULL, 10));
        // printf("%d", m);
        input[k] = m;
        printf("%d", input[k]);
    }
    // size = (size + 1) / 2;
    int count = 0;

    while (size > 1)
    {

        for (i = 0; i < size; i = i + 2)
        {
            // create a child process
            pid = fork();
            if (pid ==  0)
            {

                 // child process execution code--shows exec family calls
                if (i < argc - 1)
                {
                    execl(procpath, procname, &input[i], &input[i + 1], pid,(char *) 0);
                }
                else
                {
                    execl(procpath, procname, &input[i], 0, pid, (char *) 0);
                }
                // execvp(procname,argv);
                // if exec returns, means the call has failed.
                perror("execl failed to run slave program");
                exit(1);
            }
            else if (pid > 0 )
            {
                 / / print out command line arguments
                waitpid(-1, &sum);
            }
            else
            {
                printf("call to fork failed, no child\n");
                exit(-1);
            }
            sum = WEXITSTATUS(sum);

            printf("MASTER: partial sum =  %d, and the pid of my slave =      %d\n\n",sum,pid);

            int l;
            for (l = 0 ; l < sizeof(input) / sizeof(input[0]) ; l ++)
            {
                printf("\n%d\n", input[l]);
            }
            // printf("%d", input[i / 2]);
            input[(i + 1) / 2] = sum;
            // printf("%d", input[i / 2]);
        }
        size = (size + 1) / 2;
    }
    printf("MASTER: total sum =  %d\n",input[0]);
    exit(0);
}

/* exec example-- slave *
 * for CS-350           */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int  main(int argc, char *argv[])
{
    printf("hey whats up %s",argv[1]);
    int f = strtol(argv[1], NULL, 10);
    int s = strtol(argv[2],NULL,10);
    int sum = f + s;
    printf("I'm the slave my pid is %s\n",argv[3]);
    printf("SLAVE: first argumement = %d, second argument = %d, sum = %d\n", f, s, sum );

    return sum;
}

最佳答案

这是 execl 的声明:

int execl(const char *path, const char *arg, ...);

这是 arg... 的说明:

   The  const  char *arg and subsequent ellipses in the execl(), execlp(),
   and execle() functions can be thought of  as  arg0,  arg1,  ...,  argn.
   Together  they  describe  a list of one or more pointers to null-termi‐
   nated strings that represent the argument list available  to  the  exe‐
   cuted  program.  The first argument, by convention, should point to the
   filename associated with the file being executed.  The  list  of  argu‐
   ments  must be terminated by a null pointer, and, since these are vari‐
   adic functions, this pointer must be cast (char *) NULL.

因此,在您的代码中,您传递的 procpathprocname 是正确的,但您对 input 的使用不正确,execl 不接受指向 int 的指针,而是指向以 null 结尾的字符串的指针。

您需要将 input 更改为 char *input[size],然后在写入参数之前为 input 中的每个条目分配内存作为字符串放入其中,如下所示:

char *input [size];
/* .... */
int bytes = snprintf (NULL, 0, "%d", m);
input [k] = malloc (bytes + 1);
assert (input [k] != NULL);
snprintf (input [k], bytes + 1, "%d", m);

关于c - C 中指针和子进程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002906/

相关文章:

c++ - 指针可以用来遍历字符数组吗?

c - 生产者-消费者问题的无限循环

C - execvp() 进程间通信

c - 在函数中使用 char **,我们可以为传递给她的 char * 分配内存

c - 在C中使用指针?使困惑

c - 不围绕点移动和旋转三角形 - C Bgi 图形

c - 检测增加或减少的数据

c - 使用 (void *) 1 而不是 null 作为占位符是否有优势

c - 如何在xcode项目中设置包含路径

c++ - char* 转换和别名规则