- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
简介用户体验作为类(class)作业的一部分,我正在用 Linux 编写自己的 shell。我在将某些进程置于后台时遇到问题。我知道在命令末尾放置一个“&”可以使进程在后台运行,父进程(myShell)不必等待它。它与 ls -l &、firefox & 等一起工作正常,
问题
我真正关心的问题是 cat &。在这里,当我运行此命令时,cat 进程进入后台并返回 myShell(parent proc) 提示符,尽管我可以键入但 myshell 在几秒钟内被卡住。
这与阻塞输入等有什么关系吗,有什么建议吗?谢谢。
编辑:这是我的代码中的一个函数,它按照@minitech 的要求执行我的 shell 给出的命令
void executeCmd(char **cmdArgs, int inRedirectFd, int outRedirectFd, char *inFileName, char *outFileName, int bgProc, int inPipe, int outPipe, int *pipeFd1, int *pipeFd2){
int childPid;
childPid = fork();
if(childPid==0){
//writing the pipes before the redirection because the redirection can overwrite pipes
if(!inPipe && outPipe){
close(pipeFd1[0]);
dup2(pipeFd1[1], STDOUT_FILENO);
}
else if(inPipe && !outPipe){
close(pipeFd1[1]);
dup2(pipeFd1[0], STDIN_FILENO);
}
else if(inPipe && outPipe){
close(pipeFd1[1]);
close(pipeFd2[0]);
dup2(pipeFd1[0], STDIN_FILENO);
dup2(pipeFd2[1], STDOUT_FILENO);
}
if(outRedirectFd==1){
//token = strtok(NULL, " ");
int fd = open(outFileName, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
if(fd==-1){
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
}
dup2(fd, STDOUT_FILENO);
}
if(inRedirectFd==0){
//token = strtok(NULL," ");
int fd = open(inFileName, O_RDONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
if(fd == -1){
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
}
dup2(fd, STDIN_FILENO);
}
execvp(cmdArgs[0], cmdArgs);
printf("myShell: %s: %s\n", cmdArgs[0], strerror(errno));
_Exit(EXIT_FAILURE);
}
else{
if(!bgProc){
int retStatus;
waitpid(childPid, &retStatus, 0);
//waitpid(childPid, &retStatus, 0);
//printf("%d\n", retStatus);
}
else{
//printf("parentDesn't Wait");
}
}
抛出你能想到的任何其他建议。谢谢。
最佳答案
当 cat
在没有文件参数的情况下被调用时,它处理 stdin
。当您使用 cat &
时,它会进入后台并等待来自 stdin
的输入。由于您没有提供一种方法来指示 stdin
结束,因此它会一直等待。
如果您向 cat
提供文件,如 cat test.txt &
,它不会卡住您的 shell。
关于linux - 把猫放在后台卡住我的 shell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32984469/
有使用过 Linux 系统的小伙伴,肯定会使用过 cat 这个命令。当然,在 Linux 下,此猫非彼猫,这里的 cat 并不代表猫,而是单词 concatenate 的缩写。 cat 命令是一个
Closed. This question needs to be more focused。它当前不接受答案。
我知道我可以穿越 List秒 import cats.instances.list._ import cats.syntax.traverse._ def doMagic(item: A): M[B]
标准库在List上提供了unzip方法: scala>val l = List((1, "one"), (2, "two"), (3, "three"), (4, "four"), (5, "five
我正在经历https://www.scala-exercises.org/对于猫来说。我想我明白Apply.ap 是什么意思。但我看不到它有任何用途。 有什么区别: Apply[Option].map
我是 cat 和函数式编程方面的新手,我正在努力对 EitherT 等函数式数据类型进行单元测试。有示例代码: class Library[F[_]]() { def create(book:
基本上我试图理解这些命令之间的区别: cat >(tee f.txt) echo yolo > >(tee t.txt) 前两个命令具有完全相同的结果:打印“yolo”,然后终端返回控制权,这正是我
我正在经历https://www.scala-exercises.org/对于猫来说。我想我明白Apply.ap 是什么意思。但我看不到它有任何用途。 有什么区别: Apply[Option].map
我是 cat 和函数式编程方面的新手,我正在努力对 EitherT 等函数式数据类型进行单元测试。有示例代码: class Library[F[_]]() { def create(book:
给定: import cats.syntax.cartesian._ type M[X] = Future[Either[Error, X]] val ma: M[A] = ??? val mb: M
我想使用来自该存储库的 cats-saga:https://github.com/VladKopanev/cats-saga 但是我卡在了 OrderSagaCoordinator.scala L16
假设我有: val x1: Either[String, Int] = Right(1) val x2: Either[String, Float] = Left("Nope") val x3: Ei
我正在重新编码 printf,我从 printf 获得的行为与真正的 printf 略有不同,因为我使用字符串(即 malloc),而真正的 printf 使用写入函数。 做的时候: ./a.out
这个问题在这里已经有了答案: What is PECS (Producer Extends Consumer Super)? (16 个答案) 关闭 5 年前。 这不是一个重复的问题,因为我特别询问
好的,所以我会更具体一些(自从我上一个问题以来)。 基本上,我有一个分页页面 - 有 3 个显示类别,但每个分页页面仅显示 50 个项目。因此,您可以获取占据一页的所有单个类别,或不同类别的组合 -
我在“Abb/test”文件夹中有一些 c 文件和 common.txt,在“Abb”文件夹中有 temp.txt。我想在所有 c 文件的标题中复制 common.txt 的内容。我正在使用以下 un
我有以下脚本 #!/bin/bash set i=0; while read line do echo "$line"; $i INFO 2013-08-16 13:46:48,660 In
我正在尝试为猫寻找一个例子 EitherT.collectRight 。我有一个EitherT[Future, String, Event]当我这样做时collectRight,我明白了 Error:
我有这段代码可以编译并且工作正常 import cats.implicits._ Cartesian[ValidResponse].product( getName(map).toValida
我有两个相同的文件,但它们的大小不同。这让我想到文件中有特殊字符,即 ^M 在第六章中 :set list 不显示 ^M 字符但是 cat -A 确实显示字符。 此外,VI 仅在出现在行尾时才显示特殊
我是一名优秀的程序员,十分优秀!