【发布时间】:2015-03-30 08:49:00
【问题描述】:
从 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 值,但是当我尝试添加用户输入时它不起作用。你如何解决这个问题?
【问题讨论】:
-
这里
tokeniseLine()函数的作用是什么? -
“有fork函数返回fork值”是什么意思?
-
1.它应该做什么? 2. 它有什么作用?
-
你显示的代码是否编译?
-
它只编译到它打印出来的点:文件成功找到。
标签: c operating-system fork fgets