如何从fgets()函数获取用户输入并从用户输入中获取令牌后如何使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值,但是当我尝试添加用户输入时它不起作用。你是怎么解决的?
答案 0 :(得分:0)
当上述注释中确定的各种问题得到修复时,会发现大量未使用的变量和未使用的函数参数。清理它并确保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
之后的块中结束。
汇编:
源文件名为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!
$
基本上,代码似乎或多或少都有效,并且没有真正实质性的改变。可以做出一些改进。如果仍有问题,您需要更清楚地展示您看到的问题。