C LANGUAGE
I/O redirection
pipe
linux
Author: WhoAmI
email: kccddb@gmail.com
Date: 20220726
Copyright: CC BY-NC-SA
Learn More →
Learn More →
Learn More →
IO 要注意 conggestion 問題 (高速公路 回堵現象)
Learn More →
Linux I/O 輸入與輸出重新導向,基礎概念教學, by G. T. Wang
dup, dup2, dup3 - duplicate a file descriptor
Traffic load 與 buffer size 的關係 (QoS)
/*
**沒有仔細檢查可能的錯誤! 例如 signal,...等
只是了解用法與現象**
Author: WhoAmI 20220726
*/
/*
redirect stdout to "myout.txt"
log child process to "myappend.txt"
tdup2.c
gcc tdup2.c -o tdup2
The exit() function causes normal process termination and the value
of status & 0377 is returned to the parent
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
static int runchild(int fw)
{
pid_t childpid;
char writebuf[128];
time_t t;
if((childpid = fork()) == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if(childpid == 0)
{
time(&t);
sprintf(writebuf,"Child: [%d]output$>pid=%d,fd=%d\n",t,getpid(),fw);
write(fw,writebuf,strlen(writebuf));
close(fw);
exit(EXIT_SUCCESS);
}
else
{
}
return(EXIT_SUCCESS);
}
int main(void)
{
int newfd0,newfd1, nbytes;
char string[128] = "Hello stdout!";
char readbuffer[128];
char writebuf[128];
int fw;
int status;
time_t current;
newfd1=10;
fw=open("myout.txt",O_CREAT |O_WRONLY,0666); // Octal 0666 =Binary 110 110 110
if(fw<0){
printf("fw=%d\n",fw);
perror("open");
exit(EXIT_FAILURE);
}
dup2(fw,1); //Be careful! stdout is silently closed before being reused.
time(¤t);
fprintf(stderr,"stderr:localtime:[%s]",ctime(¤t));
sprintf(readbuffer,"[%d:%d]output$>%s\n",current,getpid(),string);
write(1,readbuffer,strlen(readbuffer)); //write to "myout.txt"
if(close(1)<0){
perror("close(1)");
exit(EXIT_FAILURE);
}
if( close(fw)<0){
perror("close(fw)");
exit(EXIT_FAILURE);
}else{
fprintf(stderr,"stderr:We close %d\n",fw);
fw=open("myappend.txt",O_CREAT|O_APPEND|O_WRONLY,0666);
if(fw>0){
fprintf(stderr,"Open myappend.txt[%d]\n",fw);
write(fw,readbuffer,strlen(readbuffer)); //write to "myappend.txt"
runchild(fw);
time(¤t);
sprintf(writebuf,"Parent:[%d]output$>pid=%d\n",current,getpid());
write(fw,writebuf,strlen(writebuf));
close(fw);
waitpid(-1, &status, 0);
//WNOHANG : return immediately if no child has exited.
fprintf(stderr,"Get child status=%d\n",status & 0377);
}
}
exit(EXIT_SUCCESS);
}
************************************************
/*
redirect stdout to "myout.txt"
tout.c
gcc tout.c -o tout
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
/*
The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calendar time t into a null-terminated string of the form
"Wed Jun 30 21:49:08 1993\n"
*/
int main(void)
{
int newfd0,newfd1, nbytes;
pid_t childpid;
char string[] = "Hello stdio! From Child!\n";
char readbuffer[128];
int fw;
time_t current;
fw=open("myout.txt",O_CREAT |O_WRONLY);
if(fw<0){
printf("fw=%d\n",fw);
perror("open");
exit(0);
}
dup2(fw,1);
time(¤t);
fprintf(stderr,"stderr:localtime:[%s]",ctime(¤t));
sprintf(readbuffer,"localtime:[%s]virtual output$>%s",ctime(¤t),string);
write(1,readbuffer,strlen(readbuffer));
if(close(1)<0){
perror("close(1)");
}
if( close(fw)<0){
perror("close(fw)");
}else{
fprintf(stderr,"stderr:We close %d\n",fw);
}
exit(0);
}
***************************************************
Authors: CrazyDog, CrazyMonkeyemail: kccddb@gmail.comDate: 20230222
Mar 14, 2025Author: \mathcal{CrazyDog}, Ph.D. in Electrical Engineering, NCTUemail: kccddb@gmail.comDate: 20230910
Nov 4, 2024Author: WhoAmI Date: 20230523 email:kccddb@gmail.com Copyright: CC BY-NC-SA 《荀子‧勸學》不積跬步,無以至千里;不積小流,無以成江海。 I. Mutex variables MUST be declared with type pthread_mutex_t, and must be initialized before they can be used. II. 了解 pthread_mutex_lock / pthread_mutex_unlock 與可能問題和代價 III. 程式執行效率與startvation 等問題
Jul 11, 2024Author: WhoAmIemail: kccddb@gmail.com
Dec 6, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up