我目前潜入创建一个backgrounding工作C用&.我需要实现非阻塞waitpid才能使其正常工作.我知道.此外,如果&在命令行末尾输入,我已经抓住了这个条件.我只是不确定如何准确地将进程作为后台作业发送,并将其实现为执行,而另一个提示是提示下一个命令.
任何事情都会有所帮助,谢谢.
struct bgprocess{
int pid;
struct bgprocess * next;
struct bgprocess * prev;
};
struct bgprocess * bgprocess1;
bgprocess1 = malloc(sizeof(struct bgprocess));
bgprocess1->prev = NULL;
bgprocess1->next = NULL;
bgprocess1->pid = NULL;
struct bgprocess * current;
current = bgprocess1;
do{
int bgreturn = 0;
while (current != NULL){
if (waitpid(current->pid, &bgreturn, WNOHANG)){
printf("Child exited");
current->prev->next = current->next;
current->next->prev = current->prev;
current->prev = NULL;
current->next = NULL;
free(current);
}
current = current->next;
} …Run Code Online (Sandbox Code Playgroud) 我的问题听起来与此相同,但事实并非如此:
我知道如何 fork() 但不知道如何将进程发送到后台。我的程序应该像一个支持管道和后台进程的简单命令 unix shell 一样工作。我可以做管道和叉,但我不知道如何&像程序的最后一行一样将进程发送到后台:
~>./a.out uname
SunOS
^C
my:~>./a.out uname &
Run Code Online (Sandbox Code Playgroud)
如何实现后台进程?
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define TIMEOUT (20)
int main(int argc, char *argv[])
{
pid_t pid;
if(argc > 1 && strncmp(argv[1], "-help", strlen(argv[1])) == 0)
{
fprintf(stderr, "Usage: Prog [CommandLineArgs]\n\nRunSafe takes as arguments:\nthe program to be run (Prog) and its command line arguments (CommandLineArgs) (if any)\n\nRunSafe will execute Prog with its …Run Code Online (Sandbox Code Playgroud)