2017-08-17 45 views
0

我試圖將Vxworks應用程序移植到Linux。爲了將其中一個串行設備的I/O重定向到標準I/O,它們使用ioTaskStdSet();在vxworks中。 但我無法像linux那樣在linux中找到api。在Linux中只有複製設備可用,這也不適用於我的應用程序。什麼是將串行設備的I/O重定向到Linux中的標準I/O的API?

任何人都可以幫我解決這個問題嗎?

+3

除'dup2()'以外? –

+1

我們需要[mcve]。目前還不清楚你在這裏嘗試了什麼。 –

回答

0

由於@Ignacio提到dup2正是用於此目的。例如這個節目標準輸出重定向到「回聲」文件:

#include <fcntl.h> 
#include <stdlib.h> 
#include <sys/stat.h> 
#include <unistd.h> 

int main(int argc, char *argv[]) { 
    int fd = open("echo", O_RDWR | O_CREAT, 00644); 
    if(fd == -1) 
     return 1; 

    if(dup2(fd, 1) == -1) 
     return 1; 
    close(fd); 

    system("echo Hi!"); 
    return 0; 
} 
+0

如果我想使用舊的文件描述符,我可以忽略關閉(fd)?我在多個線程中使用此複製,但它只在一個線程中工作。在第二個線程再次stdi/o重定向到第二個終端。我應該如何使用多線程將stdi/o重定向到多個終端? – jyothi

+0

@jyothi:如果你使用舊的文件描述符(假設你調用'write(fd,...)'),那麼爲什麼你需要複製一些東西?所以不,在'dup2'之後,你通常會調用'close'。關於線程和終端,我不確定要理解。我認爲你的問題可能非常有趣,但我們需要一些例子,如果你編輯你的帖子,這將是非常棒的。例如,當你說:「我在多個線程中使用此複製,但它只能在一個線程中工作」時,很難在看不到您的代碼的情況下爲您提供幫助。 – vonaka

+0

我目前正在使用4個端口工作在telnet客戶端應用程序上。這就是爲什麼對於我的應用程序要求,我想使用兩者。 – jyothi

0
int main() 
{ 
    for(i=0;i<4;i++) 
    { 
     sprintf(dev_name,"tsports%d",i); 
     fd[i] = open(dev,O_RDWR | O_SYNC); 
     pthread_create(tid[i],NULL,&thread_fun,(void *)fd[i]); 
    } 
    pthread_exit(NULL); 
} 
int thread_fun(void *chan) 
{ 
    int new_fd,old_fd; 
    old_fd = (int)chan; 

    new_fd = dup2(old_fd,0); 
    new_fd = dup2(old_fd,1); 
    ts_fd = old_fd; 
    tn(); 
    pthread_exit(NULL); 
} 
void tn() 
{ 
    printf("hello on terminal"); 
    while(1) 
    { 
      read(ts_fd,&ch,1); 
      /* command line */ 
      ........ 
      ........ 
      ........ 

      /* starts telnet client application on terminal port*/   
      ....... 
     ...... 
      ...... 
      ....... 
      sleep(1); 
    } 
} 

In the above code while taking input from terminal 1 it is again redirecting stdi/o to terminal 2 in second thread. 

所以我想線程特定STDI /即使重定向每個終端鄰。這在Vxworks中作爲IoTaskStdSet API可用。 是否可以在Linux中實現?