Transferred from: http://blog.chinaunix.net/uid-20682147-id-4981769.html
Cat term.c#include<stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h># include<sys/prctl.h> #include <unistd.h> #include <utmp.h> #include <signal.h> #include < pty.h> #include <fcntl.h>static void on_signal (int signo) {printf ("[%d] signal:%s \ n", Getpid (), Strsignal ( Signo));} int main () {int amaster = 0;char name[100];struct Termios termp; Termios.h (bits/termios.h) struct winsize winp; Term.h (bits/ioctl-types.h) pid_t pid = Forkpty (&amaster, name, &TERMP, &WINP), if (PID < 0) {perror ("fork Pty "); exit (1);} else if (0 = = pid) {//child processes that are part of the new terminal//subprocess () are not visible on the terminal of the parent process//The parent process is interrupted or hung out will send Sighupsignal (SIGHUP, on_signal) to the child process;p rintf (" Child:%d \ n ", Getpid ()); int save_fd = DUP (stdout_fileno); int fd = open (" 1.txt ", (O_rdwr | o_creat), 0644);d up2 (Fd,stdout_fileno), while (true) {sleep (1);p rintf ("Sub process printf!!! \ n ");} Exit (0);} else if (PID > 0) {///parent process used before terminal//If interrupt sends sighupprintf to process ("pid:%d/%d\n", Getpid (), PID);p rintf("Name:%s\n", name);p rintf ("Amaster:%d\n", Amaster);p rintf ("Win.row:%d\n", Winp.ws_row);p rintf ("Win.col:%d\n", Winp.ws_col);p rintf ("Win.xpixel:%d\n", Winp.ws_xpixel);p rintf ("Win.ypixel:%d\n", Winp.ws_ypixel); GetChar ();} return 0;}
GCC Term.c-o term-lutil
Tail-f 1.txt
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
[6598] Signal:hangup
Sub process printf!!!
Sub process printf!!!
Sub process printf!!!
Transferred from: http://blog.csdn.net/wangxvfeng101/article/details/11558697
Method 1: #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys /stat.h> #include <fcntl.h> int main () {fflush (stdout); Setvbuf (stdout,null,_ionbf,0); printf ("Test stdout\n"); int save_fd = DUP (Stdout_fileno); Save standard output File Descriptor Note: Be sure to copy a file descriptor with DUP. Don't use = like a handle under a WINODWS. int fd = open ("Test1.txt", (O_rdwr | o_creat), 0644); Dup2 (Fd,stdout_fileno); Replace the standard output printf ("Test file\n") with our newly opened file descriptor; Return to the standard output again. Two ways//Method 1 has the condition of saving standard output//dup2 (Save_fd,stdout_fileno); Method 2 does not save the standard output case int ttyfd = open ("/dev/tty", (O_RDWR), 0644); Dup2 (Ttyfd,stdout_fileno); printf ("Test tty\n"); } Method 2: #include <stdlib.h> #include <stdio.h> #include <unistd.h> int main () {Fflush (stdout); Setvbuf (stdout,null,_ionbf,0); printf ("Test stdout\n"); Freopen ("Test1.txt", "w", stdout); Note: Do not use this type of code stdout = fopen ("Test1.txt", "w"); So the output is weird. It is best to replace it with a function such as freopen. printf ("Test file\n"); Freopen ("/dev/tty", "w", stdout); printf ("Test tty\n"); }
- Here we can easily solve the problem of Prinft not output to the screen.
- There are only two cases.
- The 1th standard output is redirected.
- Problem with the 2nd output buffer. Is the first two lines of code in our two cases.
- Fflush (stdout); //Refresh the buffer to let it output immediately. Call it after printf and it will be output immediately.
- Setvbuf (stdout,null,_ionbf,0); //If you have a problem with the last method, use this function. The buffer is directly suppressed. It is directly output.
- Both of these functions are related to stream buffers. Specific use and description online there are many. I'll just say what a stream buffer is, and what it does. The operating system has set a buffer for reducing IO operations. Wait until the buffer is full and then go to Operation IO. This is to improve efficiency.
The code creates a new terminal and prints the output of the new terminal to a file for the mode.