我編寫了一個程序,它應該創建新的進程(我使用fork(),接下來在子進程調用execl())並與它進行通信。這裏是我的服務器:將進程附加到新的終端(Mac OS)
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int main(int argc, char *argv[]) {
pid_t process;
process = fork();
if (process == 0) {
printf("The program will be executed %s...\n\n", argv[0]);
printf("Executing %s", argv[0]);
execl("hello", "Hello, World!", NULL);
return EXIT_SUCCESS;
}
else if (process < 0) {
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
waitpid(process, NULL, NULL);
return 0;
}
這裏是我的客戶:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i=0;
printf("%s\n",argv[0]);
printf("The program was executed and got a string : ");
while(argv[++i] != NULL)
printf("%s ",argv[i]);
return 0;
}
的問題是下一:在同一個終端我的客戶端和服務器顯示輸出。我希望他們在不同的終端上顯示輸出。那麼,我該怎麼做呢?
請注意,終端只是一個接口。他們不在相同的終端執行*。他們在同一臺機器上執行。你的意思是你想*看到*客戶端在另一個終端的輸出? – Arash
@Arash是的,我想看到它在另一個終端 –
看到這個問題:http://stackoverflow.com/questions/3445645/how-to-invoke-another-terminal-for-output-programmatically-in-c -in-linux –