bvk*_*256 4 c linux operating-system system-calls
我有以下代码草案.
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf( "usage: %i filename", argc );
pid_t pID = fork();
if (pID == 0) // child
{
// Code only executed by child process
printf("Child PID: %i", pID);
int file = open("/tmp/rtail", O_CREAT | O_WRONLY);
//Now we redirect standard output to the file using dup2
dup2(file,1);
char tmp[30];
sprintf(tmp, "cat `tail -f %s`", argv[1]);
}
else if (pID < 0) // failed to fork
{
printf("Failed to fork");
exit(1);
// Throw exception
}
else // parent
{
}
// Code executed by both parent and child.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何将命令行参数传递给子进程?例如,./app alch.txt我想要跑步
sprintf(tmp, "cat `tail -f %s`", argv[1]);
Run Code Online (Sandbox Code Playgroud)
生产
cat `tail -f alch.txt`
Run Code Online (Sandbox Code Playgroud)
在tmp.
| 归档时间: |
|
| 查看次数: |
4903 次 |
| 最近记录: |