【发布时间】:2022-01-14 12:11:35
【问题描述】:
我正在尝试从 linux 系统上的控制台运行代码:三个简单的程序。这是我使用的代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include <chrono>
#include <thread>
int main(int argc , char *argv[])
{
if(argc != 2) {
fprintf(stderr,"Usage: cpu <string> \n");
exit(1);
}
char *str = argv[1];
while(1){
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
printf("%s\n",str);
}
return 0;
}
我用
编译了代码gcc -o cpu cpu.c -Wall
我想按照书上说的运行代码。
prompt> ./cpu A & ; ./cpu B & ; ./cpu C & ; ./cpu D &
[1] 7353
[2] 7354
[3] 7355
[4] 7356
A
B
D
C
A
B
D
C
A
C
B
D
...
现在我收到这样的错误
bob@bobvm:~/codes/OS_3_pieces$ ./cpu A & ; ./cpu B & ; ./cpu C & ; ./cpu D &
bash: syntax error near unexpected token `;'
我的操作系统环境是 Ubuntu 20。我猜原因是在 Ubuntu 系统中运行多进程的方式不同。但我没有找到。那么如何运行代码以得到与显示的书相同的输出呢?
【问题讨论】:
-
请移除你的通用头文件,不要使用 Spin 函数,而是使用 #include
#include std::this_thread::sleep_for(std::chrono::milliseconds(1000)) ;