c - 获取用户输入后使用 fork 函数

标签 c operating-system fork fgets

从 fgets() 函数获取用户输入并从用户输入获取 token 后,如何让 fork 函数工作?

我的代码:

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

/* the line can have at most 2000 words*/
void tokeniseLine(char *Line, char **Words, int *Wordn);

/* break line into words separated by whitespace, placing them in the
array words, and setting the count to Wordn */

void search&execute();

int main()
{
char Line[4000], *Words[2000], string[4000];
int Stops=0,Wordn=0;
char *end = "exit";

while(1)
{
printf("Enter program: ");
fgets(Line, 4000, stdin ); /* read a line of text here */

/* use of exitting begins when user enters 'exit' or when the program   finally locates/can't locate the user's requested file*/
if ( strcmp(Line, end) == 0 ){
exit(0);
       }
else
if ( strcmp(Line, end) != 0 ) {
printf("file successfully found.");
tokeniseLine(Line,Words,&Wordn);
search&execute();//using fork function to make process
 }
return 0;
 }

 void tokeniseLine(char *Line, char **Words, int *Wordn)
 {
 char *token;

  /* get the first token */
  token = strtok(Line, " \t\n");

  /* walk through other tokens */
  while( token != NULL ) 
  {    
   token = strtok(NULL, " \t\n");
   }
    return;
   }

    void search&execute()//this is the function which I wanted to work last   after the user input is tokenised
   {
    pid_t childpid; /* variable to store the child's pid */
    int retval;     /* child process: user-provided return code */
    int status;     /* parent process: child's exit status */

     /* only 1 int variable is needed because each process would have its
      own instance of the variable
      here, 2 int variables are used for clarity */

     /* now create new process */
      childpid = fork();

      if (childpid >= 0) /* fork succeeded */
     {
      if (childpid == 0) /* fork() returns 0 to the child process */
      {
        printf("CHILD: I am the child process!\n");
        printf("CHILD: Here's my PID: %d\n", getpid());
        printf("CHILD: My parent's PID is: %d\n", getppid());
        printf("CHILD: The value of my copy of childpid is: %d\n",childpid);
        printf("CHILD: Sleeping for 1 second...\n");
        sleep(1); /* sleep for 1 second */
        printf("CHILD: Enter an exit value (0 to 255): ");
        scanf(" %d", &retval);
        printf("CHILD: Goodbye!\n");    
        exit(retval); /* child exits with user-provided return code */
        }
        else /* fork() returns new pid to the parent process */
        {
        printf("PARENT: I am the parent process!\n");
        printf("PARENT: Here's my PID: %d\n", getpid());
        printf("PARENT: The value of my copy of childpid is %d\n",childpid);
        printf("PARENT: I will now wait for my child to exit.\n");
        wait(&status); /* wait for child to exit, and store its status */
        printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
        printf("PARENT: Goodbye!\n");             
        exit(0);  /* parent exits */       
    }
}
else /* fork returns -1 on failure */
{
    perror("fork"); /* display error message */
    exit(0); 
}

}

我尝试让 fork 函数返回 fork 值,但当我尝试添加用户输入时它不起作用。你如何解决这个问题?

最佳答案

当上述注释中指出的各种问题得到解决后,会发现存在大量未使用的变量和未使用的函数参数。清理它,并确保 printf() 格式以换行符结尾,会生成如下代码:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

void tokeniseLine(char *Line);
void search_execute(void);

int main(void)
{
    char Line[4000];
    char *end = "exit";

    while (1)
    {
        printf("Enter program: ");
        fgets(Line, 4000, stdin );

        if (strcmp(Line, end) == 0)
        {
            exit(0);
        }
        else if (strcmp(Line, end) != 0)
        {
            printf("file successfully found.\n");
            tokeniseLine(Line);
            search_execute();
        }
    }
    return 0;
}

void tokeniseLine(char *Line)
{
    char *token;

    token = strtok(Line, " \t\n");

    while (token != NULL)
    {
        printf("Token: [%s]\n", token);
        token = strtok(NULL, " \t\n");
    }
}

void search_execute(void)
{
    pid_t childpid;
    int retval;
    int status;

    childpid = fork();

    if (childpid >= 0)
    {
        if (childpid == 0)
        {
            printf("CHILD: I am the child process!\n");
            printf("CHILD: Here's my PID: %d\n", getpid());
            printf("CHILD: My parent's PID is: %d\n", getppid());
            printf("CHILD: The value of my copy of childpid is: %d\n", childpid);
            printf("CHILD: Sleeping for 1 second...\n");
            sleep(1);
            printf("CHILD: Enter an exit value (0 to 255): ");
            scanf(" %d", &retval);
            printf("CHILD: Goodbye!\n");
            exit(retval);
        }
        else
        {
            printf("PARENT: I am the parent process!\n");
            printf("PARENT: Here's my PID: %d\n", getpid());
            printf("PARENT: The value of my copy of childpid is %d\n", childpid);
            printf("PARENT: I will now wait for my child to exit.\n");
            wait(&status);
            printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
            printf("PARENT: Goodbye!\n");
            exit(0);
        }
    }
    else
    {
        perror("fork");
        exit(0);
    }
}

main() 中的循环是假的;对 search_execute() 的第一次调用使所有代码路径上的代码退出,因此 main() 中的循环永远不会重复。 else if (strcmp(Line, end) != 0) 实际上不需要 if 部分;如果前面的 if 失败,代码将最终出现在 else 后面的 block 中。

编译:

源文件名为x19.c:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror x19.c -o x19
$

示例输出:

$ ./x19
Enter program: periodic table
file successfully found.
Token: [periodic]
Token: [table]
PARENT: I am the parent process!
PARENT: Here's my PID: 4142
PARENT: The value of my copy of childpid is 4143
PARENT: I will now wait for my child to exit.
CHILD: I am the child process!
CHILD: Here's my PID: 4143
CHILD: My parent's PID is: 4142
CHILD: The value of my copy of childpid is: 0
CHILD: Sleeping for 1 second...
CHILD: Enter an exit value (0 to 255): 47
CHILD: Goodbye!
PARENT: Child's exit code is: 47
PARENT: Goodbye!
$

基本上,代码似乎或多或少正常工作,并且没有真正实质性的更改。有一些可以改进的地方。如果仍然存在问题,您需要更清楚地表明您看到的问题是什么。

关于c - 获取用户输入后使用 fork 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341894/

相关文章:

c - 段错误 - 如何从管道写入和读取链表

c - 如何在 C 中创建临时缓冲区?

c - 使用 printf scanf 进行 STDIN、STDOUT 重定向挂起

c - 系统调用应该在上下文切换之前完成吗

c++ - 窗口系统调用

header 中的 C++ 整数初始化

c - 无法让我的代理服务器向客户端发送http请求的信息 - Socket编程

c - C 中确定符号来源的工具

c - C 中未定义的 struct timeval 声明

c - 如何在循环中重用 MPI_Scatter 和 MPI_Gather